From 776b24f5dee8611ae7419d885e2b87db9a96b5bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matev=C5=BE=20Jekovec?= Date: Fri, 22 Sep 2023 13:19:12 +0200 Subject: [PATCH] client-sdk: Migrate ResolveAddress to Oasis CLI --- client-sdk/go/helpers/address.go | 67 -------------------------- client-sdk/go/helpers/address_test.go | 69 --------------------------- 2 files changed, 136 deletions(-) diff --git a/client-sdk/go/helpers/address.go b/client-sdk/go/helpers/address.go index 768a92bf8d..e1cc0e2f38 100644 --- a/client-sdk/go/helpers/address.go +++ b/client-sdk/go/helpers/address.go @@ -7,69 +7,15 @@ import ( ethCommon "github.com/ethereum/go-ethereum/common" "golang.org/x/crypto/sha3" - staking "github.com/oasisprotocol/oasis-core/go/staking/api" - "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/secp256k1" - "github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rewards" - "github.com/oasisprotocol/oasis-sdk/client-sdk/go/testing" "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types" ) const ( addressPrefixOasis = "oasis1" addressPrefixEth = "0x" - - addressExplicitSeparator = ":" - addressExplicitParaTime = "paratime" - addressExplicitPool = "pool" - addressExplicitTest = "test" - - poolRewards = "rewards" ) -// ResolveAddress resolves a string address into the corresponding account address. -func ResolveAddress(net *config.Network, address string) (*types.Address, *ethCommon.Address, error) { - if addr, ethAddr, _ := ResolveEthOrOasisAddress(address); addr != nil { - return addr, ethAddr, nil - } - - switch { - case strings.Contains(address, addressExplicitSeparator): - subs := strings.SplitN(address, addressExplicitSeparator, 2) - switch kind, data := subs[0], subs[1]; kind { - case addressExplicitParaTime: - // ParaTime. - pt := net.ParaTimes.All[data] - if pt == nil { - return nil, nil, fmt.Errorf("paratime '%s' does not exist", data) - } - - addr := types.NewAddressFromConsensus(staking.NewRuntimeAddress(pt.Namespace())) - return &addr, nil, nil - case addressExplicitPool: - // Pool. - switch data { - case poolRewards: - // Reward pool address. - return &rewards.RewardPoolAddress, nil, nil - default: - return nil, nil, fmt.Errorf("unsupported pool kind: %s", data) - } - case addressExplicitTest: - // Test key. - if testKey, ok := testing.TestAccounts[data]; ok { - return &testKey.Address, testKey.EthAddress, nil - } - return nil, nil, fmt.Errorf("unsupported test account: %s", data) - default: - // Unsupported kind. - return nil, nil, fmt.Errorf("unsupported explicit address kind: %s", kind) - } - default: - return nil, nil, fmt.Errorf("unsupported address format") - } -} - // ResolveEthOrOasisAddress decodes the given oasis bech32-encoded or ethereum hex-encoded // address and returns the corresponding ethereum address object and/or account address. // If the encoding is not valid, returns error. If the format is not known, does nothing. @@ -94,19 +40,6 @@ func ResolveEthOrOasisAddress(address string) (*types.Address, *ethCommon.Addres return nil, nil, nil } -// ParseTestAccountAddress extracts test account name from "test:some_test_account" format or -// returns an empty string, if the format doesn't match. -func ParseTestAccountAddress(name string) string { - if strings.Contains(name, addressExplicitSeparator) { - subs := strings.SplitN(name, addressExplicitSeparator, 2) - if subs[0] == addressExplicitTest { - return subs[1] - } - } - - return "" -} - // EthAddressFromPubKey takes public key, extracts the ethereum address and returns it. func EthAddressFromPubKey(pk secp256k1.PublicKey) ethCommon.Address { h := sha3.NewLegacyKeccak256() diff --git a/client-sdk/go/helpers/address_test.go b/client-sdk/go/helpers/address_test.go index 566416a491..59ab9f82e1 100644 --- a/client-sdk/go/helpers/address_test.go +++ b/client-sdk/go/helpers/address_test.go @@ -6,78 +6,9 @@ import ( ethCommon "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" - "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/secp256k1" ) -func TestResolveAddress(t *testing.T) { - require := require.New(t) - - net := config.Network{ - ParaTimes: config.ParaTimes{ - All: map[string]*config.ParaTime{ - "pt1": { - ID: "0000000000000000000000000000000000000000000000000000000000000000", - }, - }, - }, - } - - for _, tc := range []struct { - address string - expectedAddr string - expectedEthAddr string - }{ - {"", "", ""}, - {"oasis1", "", ""}, - {"oasis1blah", "", ""}, - {"oasis1qqzh32kr72v7x55cjnjp2me0pdn579u6as38kacz", "oasis1qqzh32kr72v7x55cjnjp2me0pdn579u6as38kacz", ""}, - {"0x", "", ""}, - {"0xblah", "", ""}, - {"0x60a6321eA71d37102Dbf923AAe2E08d005C4e403", "oasis1qpaqumrpewltmh9mr73hteycfzveus2rvvn8w5sp", "0x60a6321eA71d37102Dbf923AAe2E08d005C4e403"}, - {"paratime:", "", ""}, - {"paratime:invalid", "", ""}, - {"paratime:pt1", "oasis1qqdn25n5a2jtet2s5amc7gmchsqqgs4j0qcg5k0t", ""}, - {"pool:", "", ""}, - {"pool:invalid", "", ""}, - {"pool:rewards", "oasis1qp7x0q9qahahhjas0xde8w0v04ctp4pqzu5mhjav", ""}, - {"test:alice", "oasis1qrec770vrek0a9a5lcrv0zvt22504k68svq7kzve", ""}, - {"test:dave", "oasis1qrk58a6j2qn065m6p06jgjyt032f7qucy5wqeqpt", "0xDce075E1C39b1ae0b75D554558b6451A226ffe00"}, - {"test:frank", "oasis1qqnf0s9p8z79zfutszt0hwlh7w7jjrfqnq997mlw", ""}, - {"test:invalid", "", ""}, - {"invalid:", "", ""}, - } { - addr, ethAddr, err := ResolveAddress(&net, tc.address) - if len(tc.expectedAddr) > 0 { - require.NoError(err, tc.address) - require.EqualValues(tc.expectedAddr, addr.String(), tc.address) - if len(tc.expectedEthAddr) > 0 { - require.EqualValues(tc.expectedEthAddr, ethAddr.String()) - } - } else { - require.Error(err, tc.address) - } - } -} - -func TestParseTestAccountAddress(t *testing.T) { - require := require.New(t) - - for _, tc := range []struct { - address string - expected string - }{ - {"test:abc", "abc"}, - {"testabc", ""}, - {"testing:abc", ""}, - {"oasis1qqzh32kr72v7x55cjnjp2me0pdn579u6as38kacz", ""}, - {"", ""}, - } { - testName := ParseTestAccountAddress(tc.address) - require.EqualValues(tc.expected, testName, tc.address) - } -} - func TestEthAddressFromPubKey(t *testing.T) { for _, pk := range []struct { pubkey string