Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/freeze account #85

Merged
merged 110 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
f7b5b48
update gomod for guarded transactions support
AdoAdoAdo Jul 11, 2022
e5db1ac
blockchain, disabled: fixes after update libs
AdoAdoAdo Jul 12, 2022
659790c
disabled: added globalSettingsHandler
AdoAdoAdo Jul 12, 2022
0751371
rename apply signature, add guardian signing
AdoAdoAdo Jul 12, 2022
f921b88
blockchain, builders: add support for guarded transactions
AdoAdoAdo Jul 19, 2022
06df8b5
add example for guarded transaction
AdoAdoAdo Aug 4, 2022
09e8b55
add funding wallet and send fund transactions for example actors
AdoAdoAdo Aug 5, 2022
2d2ef08
fixes options
AdoAdoAdo Aug 12, 2022
64c171b
add gas limit option
AdoAdoAdo Aug 19, 2022
2b3acbf
move exampleGuardedTransactions to cmd/cli
AdoAdoAdo Aug 19, 2022
5bcd7be
more flags
dragos-rebegea Aug 19, 2022
9eaf4d2
fixes after review
dragos-rebegea Aug 19, 2022
f18c07f
Merge pull request #84 from ElrondNetwork/more-flags
AdoAdoAdo Aug 19, 2022
90cdb19
fix flag description
AdoAdoAdo Aug 22, 2022
813a8e7
remove extra copy obj and un-export method
AdoAdoAdo Aug 22, 2022
4136ef7
update workflows
AdoAdoAdo Aug 22, 2022
168a562
Merge pull request #77 from ElrondNetwork/guarded-transactions
AdoAdoAdo Aug 22, 2022
04b8d30
Merge branch 'feat/freeze-account' into freeze-account-feat-example-cli
AdoAdoAdo Aug 22, 2022
dc120c9
Options with omitempty for backwards compatibility
AdoAdoAdo Aug 22, 2022
fcf9b1a
remove unused field
AdoAdoAdo Aug 22, 2022
403b348
update dependencies and fix conflicts
AdoAdoAdo Sep 1, 2022
4113e86
make proxy configurable, add comments
AdoAdoAdo Sep 1, 2022
38c3433
remove unused field
AdoAdoAdo Sep 1, 2022
b209cb4
remove unused constant
AdoAdoAdo Sep 1, 2022
6792419
added todo comment for getting data for signing from core
ssd04 Sep 2, 2022
afff372
Merge pull request #89 from ElrondNetwork/EN-12950-refactor-tx-get-da…
AdoAdoAdo Sep 9, 2022
1f60460
merge guardian and setguardian flags
AdoAdoAdo Sep 20, 2022
c514fb5
Merge branch 'feat/freeze-account' into freeze-account-feat-example-cli
AdoAdoAdo Sep 20, 2022
d6fd85b
Merge pull request #82 from ElrondNetwork/freeze-account-feat-example…
AdoAdoAdo Sep 20, 2022
4df3a7c
exported GetGuardianData
sstanculeanu Sep 27, 2022
d155226
updated blockchain/interface Proxy
sstanculeanu Sep 27, 2022
1e8a859
fix after review, use structures from core
sstanculeanu Sep 27, 2022
44e3750
moved check for nil address where it is supposed to be on GetAccount …
sstanculeanu Oct 21, 2022
0432a4c
Merge pull request #98 from ElrondNetwork/get_guardian_data
AdoAdoAdo Oct 21, 2022
d331d62
add transaction sig verification logic
AdoAdoAdo Nov 10, 2022
7ec6af9
txcheck: add unit tests
AdoAdoAdo Nov 11, 2022
3840b1d
txcheck: fix txcheck and refactor unit tests
AdoAdoAdo Nov 11, 2022
d535cf2
rename variable
AdoAdoAdo Nov 11, 2022
bf246ff
replace magic number with const
AdoAdoAdo Nov 11, 2022
6c4da81
remove interface definition
AdoAdoAdo Nov 11, 2022
4a11eee
add comment on exported function
AdoAdoAdo Nov 14, 2022
fdece99
Merge pull request #101 from ElrondNetwork/transaction-sig-verifier
AdoAdoAdo Nov 14, 2022
0bfaa93
added Pretty on address struct
sstanculeanu Dec 6, 2022
a4c6e2f
fixed condition
sstanculeanu Dec 6, 2022
aa2e9a7
Merge pull request #104 from ElrondNetwork/address_pretty
AdoAdoAdo Dec 7, 2022
3081b7e
update dependencies
AdoAdoAdo Dec 7, 2022
824534f
Merge branch 'update-sdk-rc/v1.4.0' into rename-freeze-account
AdoAdoAdo Dec 7, 2022
9aee3d3
update dependencies version
AdoAdoAdo Dec 7, 2022
3d0311f
update go version
AdoAdoAdo Dec 8, 2022
3c6e840
Merge pull request #105 from ElrondNetwork/rename-freeze-account
dragos-rebegea Dec 8, 2022
d80132e
EN-13537: native authentication server
dragos-rebegea Dec 7, 2022
a206a53
EN-13537: fixes after review
dragos-rebegea Dec 8, 2022
f7604c0
EN-13537: fix priceAggregator example
dragos-rebegea Dec 8, 2022
cc7202e
EN-13537: server validation also returns addressHandler
dragos-rebegea Dec 9, 2022
8612732
EN-13537: return address as string
dragos-rebegea Dec 9, 2022
ce35aec
EN-13537: create AuthServerStub
dragos-rebegea Dec 9, 2022
bfc5d08
EN-13537: encode host
dragos-rebegea Dec 9, 2022
866bd3b
EN-13537: decode with padding trim
dragos-rebegea Dec 12, 2022
95b254c
EN-13537: remove host
dragos-rebegea Dec 12, 2022
4ef22e6
EN-13537: fixes after review
dragos-rebegea Dec 12, 2022
4f18bf1
updated dependencies
sstanculeanu Dec 13, 2022
af7d195
EN-13537: fixes after review
dragos-rebegea Dec 13, 2022
8428c4c
EN-13537: add host back
dragos-rebegea Dec 13, 2022
5359002
EN-13537: server.Validate only validates the already decoded token
dragos-rebegea Dec 14, 2022
5fa996a
EN-13537: update authServerStub
dragos-rebegea Dec 14, 2022
17859d0
EN-13537: authTokenStub
dragos-rebegea Dec 14, 2022
74cfe28
EN-13537: make client sign also his address
dragos-rebegea Dec 14, 2022
f9c8021
Merge pull request #108 from ElrondNetwork/update_dependencies
dragos-rebegea Dec 19, 2022
04a64af
update gomod and add guardian service uid for cli
AdoAdoAdo Dec 20, 2022
d4bb528
Merge branch 'feat/freeze-account' into EN-13537
dragos-rebegea Dec 22, 2022
9f00126
EN-13537: fix after rebase
dragos-rebegea Dec 22, 2022
346cf0f
Merge pull request #106 from ElrondNetwork/EN-13537
dragos-rebegea Dec 22, 2022
79181a7
Merge branch 'feat/freeze-account' into guardian-service-uid
AdoAdoAdo Dec 22, 2022
5937435
Merge pull request #111 from ElrondNetwork/guardian-service-uid
AdoAdoAdo Dec 22, 2022
d73e4cf
Merge branch 'main' into merge-main-freeze-account
AdoAdoAdo Dec 22, 2022
1ac9c26
fixes after merge
AdoAdoAdo Dec 22, 2022
3f497fa
fixes after merge
AdoAdoAdo Dec 22, 2022
fbbb849
more fixes after merge
AdoAdoAdo Dec 23, 2022
eb3a58f
fixes after merge, adapt cli
AdoAdoAdo Dec 23, 2022
7b31195
fix authentication tests
dragos-rebegea Dec 23, 2022
48f57a0
Merge pull request #113 from ElrondNetwork/fix-authentication-tests
AdoAdoAdo Dec 23, 2022
1fc5676
fix linter
AdoAdoAdo Dec 23, 2022
673e2ec
Merge pull request #112 from ElrondNetwork/merge-main-freeze-account
AdoAdoAdo Dec 27, 2022
b583f5c
fixed path to get guardian data
sstanculeanu Jan 6, 2023
5b6f3b1
Merge pull request #117 from multiversx/fix_getguardiandata_path
sstanculeanu Jan 9, 2023
e75aaa6
Merge remote-tracking branch 'origin/main' into merge_main_into_feat_…
sstanculeanu Feb 3, 2023
915169b
fixes after merge
sstanculeanu Feb 3, 2023
fb6fb08
Merge pull request #123 from multiversx/merge_main_into_feat_freeze_a…
sstanculeanu Feb 3, 2023
c61979b
MX-13883: native auth server use api for fetching the block
dragos-rebegea Feb 20, 2023
e5f2ed6
MX-13883: fix after review
dragos-rebegea Feb 20, 2023
9f2daff
MX-13883: fix after review
dragos-rebegea Feb 20, 2023
0b08dd8
MX-13883: fixes after review
dragos-rebegea Feb 20, 2023
cdfb304
Merge pull request #124 from multiversx/MX-13883
AdoAdoAdo Feb 20, 2023
0b14d35
MX-13884: native auth support legacy signable messages
dragos-rebegea Feb 20, 2023
c98d16c
Merge pull request #125 from multiversx/MX-13884
AdoAdoAdo Feb 21, 2023
2578db5
update dependencies for guardians
AdoAdoAdo Mar 9, 2023
3615d5b
Merge pull request #130 from multiversx/update-dependencies
AdoAdoAdo Mar 9, 2023
6a74d51
update mx-chain-go
sstanculeanu Mar 10, 2023
a170112
Merge pull request #131 from multiversx/update_mx-chain-go
sstanculeanu Mar 13, 2023
01b842c
added cacher for block timestamp to avoid API calls
sstanculeanu Mar 28, 2023
fdb9b55
Merge pull request #132 from multiversx/cache_block_timestamp_in_auth…
sstanculeanu Mar 30, 2023
00c2972
Merge branch 'main' into feat/freeze-account
iulianpascalau May 26, 2023
b856e98
- fixes after merge
iulianpascalau May 26, 2023
694fe49
- fixes after review
iulianpascalau May 26, 2023
1cb3d54
Merge pull request #140 from multiversx/merge-main-guardians-2023.05.26
iulianpascalau May 29, 2023
c44e392
Merge branch 'main' into merge-main-freeze-account
AdoAdoAdo Jun 2, 2023
81c00ee
Merge pull request #142 from multiversx/merge-main-freeze-account
AdoAdoAdo Jun 2, 2023
1a6be8c
Merge branch 'main' into feat/freeze-account
AdoAdoAdo Dec 15, 2023
7e57c29
fix after merge
AdoAdoAdo Dec 15, 2023
a630871
Merge pull request #161 from multiversx/merge-master-freeze-acc
AdoAdoAdo Dec 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
name: Unit
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
- name: Set up Go 1.17.6
uses: actions/setup-go@v3
with:
go-version: 1.20.7
id: go

- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Get dependencies
run: |
Expand Down
8 changes: 5 additions & 3 deletions aggregator/fetchers/fetchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/multiversx/mx-sdk-go/aggregator"
"github.com/multiversx/mx-sdk-go/aggregator/mock"
"github.com/multiversx/mx-sdk-go/authentication"
"github.com/multiversx/mx-sdk-go/authentication/native"
"github.com/multiversx/mx-sdk-go/blockchain"
"github.com/multiversx/mx-sdk-go/blockchain/cryptoProvider"
"github.com/multiversx/mx-sdk-go/core"
Expand Down Expand Up @@ -69,16 +70,17 @@ func createAuthClient() (authentication.AuthClient, error) {

keyGen := signing.NewKeyGenerator(ed25519.NewEd25519())
holder, _ := cryptoProvider.NewCryptoComponentsHolder(keyGen, privateKeyBytes)
args := authentication.ArgsNativeAuthClient{
args := native.ArgsNativeAuthClient{
Signer: cryptoProvider.NewSigner(),
ExtraInfo: nil,
ExtraInfo: struct{}{},
Proxy: proxy,
CryptoComponentsHolder: holder,
TokenExpiryInSeconds: 60 * 60 * 24,
Host: "oracle",
TokenHandler: native.NewAuthTokenHandler(),
}

authClient, err := authentication.NewNativeAuthClient(args)
authClient, err := native.NewNativeAuthClient(args)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion aggregator/graphqlResponseGetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/url"
"testing"

"github.com/multiversx/mx-sdk-go/authentication/mock"
"github.com/multiversx/mx-sdk-go/authentication/native/mock"
"github.com/stretchr/testify/require"
)

Expand Down
2 changes: 1 addition & 1 deletion aggregator/notifees/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// TxBuilder defines the component able to build & sign a transaction
type TxBuilder interface {
ApplySignature(cryptoHolder core.CryptoComponentsHolder, tx *transaction.FrontendTransaction) error
ApplyUserSignature(cryptoHolder core.CryptoComponentsHolder, tx *transaction.FrontendTransaction) error
IsInterfaceNil() bool
}

Expand Down
2 changes: 1 addition & 1 deletion aggregator/notifees/mxNotifee.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (en *mxNotifee) PriceChanged(ctx context.Context, priceChanges []*aggregato
return err
}

err = en.txBuilder.ApplySignature(en.cryptoHolder, tx)
err = en.txBuilder.ApplyUserSignature(en.cryptoHolder, tx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion aggregator/notifees/mxNotifee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func TestMxNotifee_PriceChanged(t *testing.T) {
},
}
args.TxBuilder = &testsCommon.TxBuilderStub{
ApplySignatureCalled: func(cryptoHolder core.CryptoComponentsHolder, tx *transaction.FrontendTransaction) error {
ApplyUserSignatureCalled: func(cryptoHolder core.CryptoComponentsHolder, tx *transaction.FrontendTransaction) error {
return expectedErr
},
}
Expand Down
48 changes: 43 additions & 5 deletions authentication/errors.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
package authentication

import "errors"
import (
"errors"
"fmt"
"net/http"
)

// ErrNilTxSigner signals that a nil transaction signer was provided
var ErrNilTxSigner = errors.New("nil transaction signer")
// ErrNilTokenHandler signals that a nil token handler has been provided
var ErrNilTokenHandler = errors.New("nil token handler")

// ErrNilProxy signals that a nil proxy was provided
var ErrNilProxy = errors.New("nil proxy")
// ErrNilSigner signals that a nil signer has been provided
var ErrNilSigner = errors.New("nil signer")

// ErrNilSignature signals that the token has a nil signature
var ErrNilSignature = errors.New("nil token signature")

// ErrNilAddress signals that the token has a nil address
var ErrNilAddress = errors.New("nil token address")

// ErrNilBody signals that the token has a nil body
var ErrNilBody = errors.New("nil token body")

// ErrTokenExpired signals that the provided token is expired
var ErrTokenExpired = errors.New("token expired")

// ErrNilCryptoComponentsHolder signals that a nil cryptoComponentsHolder has been provided
var ErrNilCryptoComponentsHolder = errors.New("nil cryptoComponentsHolder")

// ErrNilHttpClientWrapper signals that a nil http client wrapper was provided
var ErrNilHttpClientWrapper = errors.New("nil http client wrapper")

// ErrHTTPStatusCodeIsNotOK signals that the returned HTTP status code is not OK
var ErrHTTPStatusCodeIsNotOK = errors.New("HTTP status code is not OK")

// ErrNilCacher signals that a nil cacher has been provided
var ErrNilCacher = errors.New("nil cacher")

// ErrInvalidValue signals that an invalid value has been provided
var ErrInvalidValue = errors.New("invalid value")

// CreateHTTPStatusError creates an error with the provided http status code and error
func CreateHTTPStatusError(httpStatusCode int, err error) error {
if err == nil {
err = ErrHTTPStatusCodeIsNotOK
}

return fmt.Errorf("%w, returned http status: %d, %s",
err, httpStatusCode, http.StatusText(httpStatusCode))
}
36 changes: 36 additions & 0 deletions authentication/interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
package authentication

import "context"

// AuthClient defines the behavior of an authentication client
type AuthClient interface {
GetAccessToken() (string, error)
IsInterfaceNil() bool
}

// AuthServer defines the behavior of an authentication server
type AuthServer interface {
Validate(accessToken AuthToken) error
IsInterfaceNil() bool
}

// AuthTokenHandler defines the behavior of an authentication token handler
type AuthTokenHandler interface {
Decode(accessToken string) (AuthToken, error)
Encode(authToken AuthToken) (string, error)
GetUnsignedToken(authToken AuthToken) []byte
GetSignableMessage(address, unsignedToken []byte) []byte
GetSignableMessageLegacy(address, unsignedToken []byte) []byte
IsInterfaceNil() bool
}

// AuthToken defines the behavior of an authentication token
type AuthToken interface {
GetTtl() int64
GetAddress() []byte
GetHost() []byte
GetSignature() []byte
GetBlockHash() string
GetExtraInfo() []byte
IsInterfaceNil() bool
}

// HttpClientWrapper defines the behavior of http client able to make http requests
type HttpClientWrapper interface {
GetHTTP(ctx context.Context, endpoint string) ([]byte, int, error)
PostHTTP(ctx context.Context, endpoint string, data []byte) ([]byte, int, error)
IsInterfaceNil() bool
}
70 changes: 40 additions & 30 deletions authentication/native.go → authentication/native/client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package authentication
package native

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"time"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-sdk-go/authentication"
"github.com/multiversx/mx-sdk-go/builders"
"github.com/multiversx/mx-sdk-go/core"
"github.com/multiversx/mx-sdk-go/workflows"
Expand All @@ -16,60 +16,64 @@ import (
// ArgsNativeAuthClient is the DTO used in the native auth client constructor
type ArgsNativeAuthClient struct {
Signer builders.Signer
ExtraInfo interface{}
ExtraInfo struct{}
Proxy workflows.ProxyHandler
CryptoComponentsHolder core.CryptoComponentsHolder
TokenExpiryInSeconds uint64
TokenHandler authentication.AuthTokenHandler
TokenExpiryInSeconds int64
Host string
}

type nativeAuthClient struct {
type authClient struct {
signer builders.Signer
encodedExtraInfo string
extraInfo []byte
proxy workflows.ProxyHandler
tokenExpiryInSeconds uint64
cryptoComponentsHolder core.CryptoComponentsHolder
encodedHost string
tokenExpiryInSeconds int64
host []byte
token string
tokenHandler authentication.AuthTokenHandler
tokenExpire time.Time
getTimeHandler func() time.Time
}

// NewNativeAuthClient will create a new native client able to create authentication tokens
func NewNativeAuthClient(args ArgsNativeAuthClient) (*nativeAuthClient, error) {
func NewNativeAuthClient(args ArgsNativeAuthClient) (*authClient, error) {
if check.IfNil(args.Signer) {
return nil, ErrNilTxSigner
return nil, authentication.ErrNilSigner
}

extraInfoBytes, err := json.Marshal(args.ExtraInfo)
if err != nil {
return nil, fmt.Errorf("%w while marshaling args.ExtraInfo", err)
return nil, fmt.Errorf("%w while marshaling args.extraInfo", err)
}

if check.IfNil(args.Proxy) {
return nil, ErrNilProxy
return nil, workflows.ErrNilProxy
}

if check.IfNil(args.CryptoComponentsHolder) {
return nil, ErrNilCryptoComponentsHolder
if check.IfNil(args.TokenHandler) {
return nil, authentication.ErrNilTokenHandler
}

encodedHost := base64.StdEncoding.EncodeToString([]byte(args.Host))
encodedExtraInfo := base64.StdEncoding.EncodeToString(extraInfoBytes)
if check.IfNil(args.CryptoComponentsHolder) {
return nil, authentication.ErrNilCryptoComponentsHolder
}

return &nativeAuthClient{
return &authClient{
signer: args.Signer,
encodedExtraInfo: encodedExtraInfo,
extraInfo: extraInfoBytes,
proxy: args.Proxy,
cryptoComponentsHolder: args.CryptoComponentsHolder,
encodedHost: encodedHost,
host: []byte(args.Host),
tokenHandler: args.TokenHandler,
tokenExpiryInSeconds: args.TokenExpiryInSeconds,
getTimeHandler: time.Now,
}, nil
}

// GetAccessToken returns an access token used for authentication into different MultiversX services
func (nac *nativeAuthClient) GetAccessToken() (string, error) {
func (nac *authClient) GetAccessToken() (string, error) {
now := nac.getTimeHandler()
noToken := nac.tokenExpire.IsZero()
tokenExpired := now.After(nac.tokenExpire)
Expand All @@ -82,7 +86,7 @@ func (nac *nativeAuthClient) GetAccessToken() (string, error) {
return nac.token, nil
}

func (nac *nativeAuthClient) createNewToken() error {
func (nac *authClient) createNewToken() error {
nonce, err := nac.proxy.GetLatestHyperBlockNonce(context.Background())
if err != nil {
return err
Expand All @@ -93,24 +97,30 @@ func (nac *nativeAuthClient) createNewToken() error {
return err
}

token := fmt.Sprintf("%s.%s.%d.%s", nac.encodedHost, lastHyperblock.Hash, nac.tokenExpiryInSeconds, nac.encodedExtraInfo)
token := &AuthToken{
ttl: nac.tokenExpiryInSeconds,
host: nac.host,
extraInfo: nac.extraInfo,
blockHash: lastHyperblock.Hash,
address: []byte(nac.cryptoComponentsHolder.GetBech32()),
}

signature, err := nac.signer.SignMessage([]byte(token), nac.cryptoComponentsHolder.GetPrivateKey())
unsignedToken := nac.tokenHandler.GetUnsignedToken(token)
signableMessage := nac.tokenHandler.GetSignableMessage(token.GetAddress(), unsignedToken)
token.signature, err = nac.signer.SignMessage(signableMessage, nac.cryptoComponentsHolder.GetPrivateKey())
if err != nil {
return err
}

encodedToken := base64.StdEncoding.EncodeToString([]byte(token))

encodedSignature := base64.StdEncoding.EncodeToString(signature)

encodedAddress := base64.StdEncoding.EncodeToString([]byte(nac.cryptoComponentsHolder.GetBech32()))
nac.token = fmt.Sprintf("%s.%s.%s", encodedAddress, encodedToken, encodedSignature)
nac.token, err = nac.tokenHandler.Encode(token)
if err != nil {
return err
}
nac.tokenExpire = nac.getTimeHandler().Add(time.Duration(nac.tokenExpiryInSeconds))
return nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (nac *nativeAuthClient) IsInterfaceNil() bool {
func (nac *authClient) IsInterfaceNil() bool {
return nac == nil
}
Loading