2023-10-30 16:58:57 +00:00
|
|
|
package walletconnect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2024-05-23 12:19:00 +00:00
|
|
|
"strconv"
|
2023-10-30 16:58:57 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2023-10-30 16:58:57 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2024-05-23 12:19:00 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2023-11-24 15:27:05 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-30 16:58:57 +00:00
|
|
|
)
|
|
|
|
|
2024-05-23 12:19:00 +00:00
|
|
|
func getSessionJSONFor(chains []int, expiry int) string {
|
|
|
|
chainsStr := "["
|
|
|
|
for i, chain := range chains {
|
|
|
|
chainsStr += `"eip155:` + strconv.Itoa(chain) + `"`
|
|
|
|
if i != len(chains)-1 {
|
|
|
|
chainsStr += ","
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chainsStr += "]"
|
|
|
|
expiryStr := strconv.Itoa(expiry)
|
|
|
|
|
|
|
|
return `{
|
|
|
|
"expiry": ` + expiryStr + `,
|
|
|
|
"namespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"accounts": [
|
|
|
|
"eip155:1:0x7F47C2e18a4BBf5487E6fb082eC2D9Ab0E6d7240",
|
|
|
|
"eip155:10:0x7F47C2e18a4BBf5487E6fb082eC2D9Ab0E6d7240",
|
|
|
|
"eip155:42161:0x7F47C2e18a4BBf5487E6fb082eC2D9Ab0E6d7240"
|
|
|
|
],
|
|
|
|
"chains": ` + chainsStr + `,
|
|
|
|
"events": [
|
|
|
|
"accountsChanged",
|
|
|
|
"chainChanged"
|
|
|
|
],
|
|
|
|
"methods": [
|
|
|
|
"eth_sendTransaction",
|
|
|
|
"personal_sign"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"optionalNamespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"chains": [],
|
|
|
|
"events": [],
|
|
|
|
"methods": [],
|
|
|
|
"rpcMap": {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"pairingTopic": "50fba141cdb5c015493c2907c46bacf9f7cbd7c8e3d4e97df891f18dddcff69c",
|
|
|
|
"peer": {
|
|
|
|
"metadata": {
|
|
|
|
"description": "Test Dapp Description",
|
|
|
|
"icons": [ "https://test.org/test.png"],
|
|
|
|
"name": "Test Dapp",
|
|
|
|
"url": "https://dapp.test.org"
|
|
|
|
},
|
|
|
|
"publicKey": "1234567890aeb6081cabed26faf48919162fd70cc66d639f118a60507ae0463d"
|
|
|
|
},
|
|
|
|
"relay": { "protocol": "irn"},
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"chains": [
|
|
|
|
"eip155:1"
|
|
|
|
],
|
|
|
|
"events": [
|
|
|
|
"chainChanged",
|
|
|
|
"accountsChanged"
|
|
|
|
],
|
|
|
|
"methods": [
|
|
|
|
"eth_sendTransaction",
|
|
|
|
"personal_sign"
|
|
|
|
],
|
|
|
|
"rpcMap": {
|
|
|
|
"1": "https://mainnet.infura.io/v3/099fc58e0de9451d80b18d7c74caa7c1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"self": {
|
|
|
|
"metadata": {
|
|
|
|
"description": "Test Wallet Description",
|
|
|
|
"icons": [
|
|
|
|
"https://wallet.test.org/test.svg"
|
|
|
|
],
|
|
|
|
"name": "Test Wallet",
|
|
|
|
"url": "http://localhost"
|
|
|
|
},
|
|
|
|
"publicKey": "da4a87d5f0f54951afe870ebf020cf03f8a3522fbd219398c3fa159a37e16d54"
|
|
|
|
},
|
|
|
|
"topic": "e39e1f435a46b5ee6b31484d1751cfbc35be1275653af2ea340974a7592f1a19"
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
func Test_sessionProposalValidity(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
sessionProposalJSON string
|
|
|
|
expectedValidity bool
|
|
|
|
}{
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#11-proposal-namespaces-does-not-include-an-optional-namespace
|
|
|
|
{
|
|
|
|
name: "proposal-namespaces-does-not-include-an-optional-namespace",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"eip155:10": {
|
|
|
|
"methods": ["personal_sign"],
|
|
|
|
"events": ["accountsChanged", "chainChanged"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: true,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#12-proposal-namespaces-must-not-have-chains-empty
|
|
|
|
{
|
|
|
|
name: "proposal-namespaces-must-not-have-chains-empty",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"cosmos": {
|
|
|
|
"chains": [],
|
|
|
|
"methods": ["cosmos_signDirect"],
|
|
|
|
"events": ["someCosmosEvent"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: false,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#13-chains-might-be-omitted-if-the-caip-2-is-defined-in-the-index
|
|
|
|
{
|
|
|
|
name: "chains-might-be-omitted-if-the-caip-2-is-defined-in-the-index",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"chains": ["eip155:1", "eip155:137"],
|
|
|
|
"methods": ["eth_sendTransaction", "eth_signTransaction", "eth_sign"],
|
|
|
|
"events": ["accountsChanged", "chainChanged"]
|
|
|
|
},
|
|
|
|
"eip155:10": {
|
|
|
|
"methods": ["personal_sign"],
|
|
|
|
"events": ["accountsChanged", "chainChanged"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: true,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#14-chains-must-be-caip-2-compliant
|
|
|
|
{
|
|
|
|
name: "chains-must-be-caip-2-compliant",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"chains": ["42"],
|
|
|
|
"methods": ["eth_sign"],
|
|
|
|
"events": ["accountsChanged"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: false,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#15-proposal-namespace-methods-and-events-may-be-empty
|
|
|
|
{
|
|
|
|
name: "proposal-namespace-methods-and-events-may-be-empty",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"chains": ["eip155:1"],
|
|
|
|
"methods": [],
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: true,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#16-all-chains-in-the-namespace-must-contain-the-namespace-prefix
|
|
|
|
{
|
|
|
|
name: "all-chains-in-the-namespace-must-contain-the-namespace-prefix",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"chains": ["eip155:1", "eip155:137", "cosmos:cosmoshub-4"],
|
|
|
|
"methods": ["eth_sendTransaction"],
|
|
|
|
"events": ["accountsChanged", "chainChanged"]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"optionalNamespaces": {
|
|
|
|
"eip155:42161": {
|
|
|
|
"methods": ["personal_sign"],
|
|
|
|
"events": ["accountsChanged", "chainChanged"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: false,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#17-namespace-key-must-comply-with-caip-2-specification
|
|
|
|
{
|
|
|
|
name: "namespace-key-must-comply-with-caip-2-specification",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"": {
|
|
|
|
"chains": [":1"],
|
|
|
|
"methods": ["personalSign"],
|
|
|
|
"events": []
|
|
|
|
},
|
|
|
|
"**": {
|
|
|
|
"chains": ["**:1"],
|
|
|
|
"methods": ["personalSign"],
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: false,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#18-all-namespaces-must-be-valid
|
|
|
|
{
|
|
|
|
name: "all-namespaces-must-be-valid",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {
|
|
|
|
"eip155": {
|
|
|
|
"chains": ["eip155:1"],
|
|
|
|
"methods": ["personalSign"],
|
|
|
|
"events": []
|
|
|
|
},
|
|
|
|
"cosmos": {
|
|
|
|
"chains": [],
|
|
|
|
"methods": [],
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: false,
|
|
|
|
},
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#19-proposal-namespaces-may-be-empty
|
|
|
|
{
|
|
|
|
name: "proposal-namespaces-may-be-empty",
|
|
|
|
sessionProposalJSON: `{
|
|
|
|
"params": {
|
|
|
|
"requiredNamespaces": {}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expectedValidity: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var sessionProposal SessionProposal
|
|
|
|
err := json.Unmarshal([]byte(tt.sessionProposalJSON), &sessionProposal)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-05-23 12:19:00 +00:00
|
|
|
validRes := sessionProposal.ValidateProposal()
|
2023-11-24 15:27:05 +00:00
|
|
|
if tt.expectedValidity {
|
2024-05-23 12:19:00 +00:00
|
|
|
assert.True(t, validRes)
|
2023-11-24 15:27:05 +00:00
|
|
|
} else {
|
2024-05-23 12:19:00 +00:00
|
|
|
assert.False(t, validRes)
|
2023-11-24 15:27:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:19:00 +00:00
|
|
|
func Test_supportedChainInSession(t *testing.T) {
|
2023-10-30 16:58:57 +00:00
|
|
|
type args struct {
|
2024-05-23 12:19:00 +00:00
|
|
|
sessionProposal Session
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2024-05-23 12:19:00 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedChains []uint64
|
2023-10-30 16:58:57 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-05-23 12:19:00 +00:00
|
|
|
name: "supported_chain",
|
2023-10-30 16:58:57 +00:00
|
|
|
args: args{
|
2024-05-23 12:19:00 +00:00
|
|
|
sessionProposal: Session{
|
|
|
|
Namespaces: map[string]Namespace{
|
|
|
|
"eip155": {
|
|
|
|
Chains: []string{"eip155:1", "eip155:2", "eip155:3", "eip155:4", "eip155:5"},
|
|
|
|
},
|
|
|
|
},
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-23 12:19:00 +00:00
|
|
|
expectedChains: []uint64{1, 2, 3, 4, 5},
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-05-23 12:19:00 +00:00
|
|
|
gotChains := supportedChainsInSession(tt.args.sessionProposal)
|
|
|
|
if !reflect.DeepEqual(gotChains, tt.expectedChains) {
|
|
|
|
t.Errorf("supportedChainInSessionProposal() gotChains = %v, want %v", gotChains, tt.expectedChains)
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
func Test_caip10Accounts(t *testing.T) {
|
2023-10-30 16:58:57 +00:00
|
|
|
type args struct {
|
2023-11-24 15:27:05 +00:00
|
|
|
accounts []*accounts.Account
|
|
|
|
chains []uint64
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2023-11-24 15:27:05 +00:00
|
|
|
want []string
|
2023-10-30 16:58:57 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-11-24 15:27:05 +00:00
|
|
|
name: "generate_caip10_accounts",
|
2023-10-30 16:58:57 +00:00
|
|
|
args: args{
|
2023-11-24 15:27:05 +00:00
|
|
|
accounts: []*accounts.Account{
|
2023-10-30 16:58:57 +00:00
|
|
|
{
|
|
|
|
Address: types.HexToAddress("0x1"),
|
|
|
|
Type: accounts.AccountTypeWatch,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Address: types.HexToAddress("0x2"),
|
|
|
|
Type: accounts.AccountTypeSeed,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
chains: []uint64{1, 2},
|
|
|
|
},
|
|
|
|
want: []string{
|
|
|
|
"eip155:1:0x0000000000000000000000000000000000000001",
|
|
|
|
"eip155:2:0x0000000000000000000000000000000000000001",
|
|
|
|
"eip155:1:0x0000000000000000000000000000000000000002",
|
|
|
|
"eip155:2:0x0000000000000000000000000000000000000002",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty_addresses",
|
|
|
|
args: args{
|
2023-11-24 15:27:05 +00:00
|
|
|
accounts: []*accounts.Account{},
|
|
|
|
chains: []uint64{1, 2},
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty_chains",
|
|
|
|
args: args{
|
2023-11-24 15:27:05 +00:00
|
|
|
accounts: []*accounts.Account{
|
|
|
|
{
|
|
|
|
Address: types.HexToAddress("0x1"),
|
|
|
|
Type: accounts.AccountTypeWatch,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Address: types.HexToAddress("0x2"),
|
|
|
|
Type: accounts.AccountTypeSeed,
|
|
|
|
},
|
2023-10-30 16:58:57 +00:00
|
|
|
},
|
|
|
|
chains: []uint64{},
|
|
|
|
},
|
|
|
|
want: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-11-24 15:27:05 +00:00
|
|
|
if got := caip10Accounts(tt.args.accounts, tt.args.chains); !reflect.DeepEqual(got, tt.want) {
|
2023-10-30 16:58:57 +00:00
|
|
|
t.Errorf("caip10Accounts() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-05-23 12:19:00 +00:00
|
|
|
|
|
|
|
// Test_AddSession validates that the new added session is active (not expired and not disconnected)
|
|
|
|
func Test_AddSession(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
// Add session for testnet
|
|
|
|
expiry := 1716581732
|
|
|
|
sessionJSON := getSessionJSONFor([]int{11155111}, expiry)
|
|
|
|
networks := []params.Network{
|
|
|
|
{ChainID: 1, IsTest: false},
|
|
|
|
{ChainID: 11155111, IsTest: true},
|
|
|
|
}
|
|
|
|
err := AddSession(db, networks, sessionJSON)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
dapps, err := GetActiveDapps(db, int64(expiry-1), true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(dapps))
|
|
|
|
}
|