2023-10-30 16:58:57 +00:00
|
|
|
package walletconnect
|
|
|
|
|
|
|
|
import (
|
2023-11-06 19:04:42 +00:00
|
|
|
"encoding/json"
|
2023-10-30 16:58:57 +00:00
|
|
|
"errors"
|
2023-11-24 15:27:05 +00:00
|
|
|
"fmt"
|
2023-10-30 16:58:57 +00:00
|
|
|
"strconv"
|
2023-11-24 15:27:05 +00:00
|
|
|
"strings"
|
2023-10-30 16:58:57 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
|
|
|
)
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
const (
|
|
|
|
SupportedEip155Namespace = "eip155"
|
2023-10-30 16:58:57 +00:00
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
ProposeUserPairEvent = walletevent.EventType("WalletConnectProposeUserPair")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrorInvalidSessionProposal = errors.New("invalid session proposal")
|
|
|
|
ErrorNamespaceNotSupported = errors.New("namespace not supported")
|
|
|
|
ErrorChainsNotSupported = errors.New("chains not supported")
|
|
|
|
ErrorInvalidParamsCount = errors.New("invalid params count")
|
|
|
|
ErrorInvalidAddressMsgIndex = errors.New("invalid address and/or msg index (must be 0 or 1)")
|
|
|
|
ErrorMethodNotSupported = errors.New("method not supported")
|
|
|
|
)
|
2023-10-30 16:58:57 +00:00
|
|
|
|
2023-11-19 17:29:17 +00:00
|
|
|
type Topic string
|
|
|
|
|
2023-10-30 16:58:57 +00:00
|
|
|
type Namespace struct {
|
|
|
|
Methods []string `json:"methods"`
|
|
|
|
Chains []string `json:"chains"` // CAIP-2 format e.g. ["eip155:1"]
|
|
|
|
Events []string `json:"events"`
|
|
|
|
Accounts []string `json:"accounts,omitempty"` // CAIP-10 format e.g. ["eip155:1:0x453...228"]
|
|
|
|
}
|
|
|
|
|
|
|
|
type Metadata struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Icons []string `json:"icons"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
VerifyURL string `json:"verifyUrl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Proposer struct {
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
Metadata Metadata `json:"metadata"`
|
|
|
|
}
|
|
|
|
|
2023-11-19 17:29:17 +00:00
|
|
|
type Verified struct {
|
|
|
|
VerifyURL string `json:"verifyUrl"`
|
|
|
|
Validation string `json:"validation"`
|
|
|
|
Origin string `json:"origin"`
|
|
|
|
IsScam bool `json:"isScam,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:58:57 +00:00
|
|
|
type VerifyContext struct {
|
2023-11-19 17:29:17 +00:00
|
|
|
Verified Verified `json:"verified"`
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Params struct {
|
2023-11-24 15:27:05 +00:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
PairingTopic Topic `json:"pairingTopic"`
|
|
|
|
Expiry int64 `json:"expiry"`
|
|
|
|
RequiredNamespaces map[string]Namespace `json:"requiredNamespaces"`
|
|
|
|
OptionalNamespaces map[string]Namespace `json:"optionalNamespaces"`
|
|
|
|
Proposer Proposer `json:"proposer"`
|
|
|
|
Verify VerifyContext `json:"verifyContext"`
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SessionProposal struct {
|
2023-11-19 17:29:17 +00:00
|
|
|
ID int64 `json:"id"`
|
2023-10-30 16:58:57 +00:00
|
|
|
Params Params `json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PairSessionResponse struct {
|
2023-11-24 15:27:05 +00:00
|
|
|
SessionProposal SessionProposal `json:"sessionProposal"`
|
|
|
|
SupportedNamespaces map[string]Namespace `json:"supportedNamespaces"`
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 19:04:42 +00:00
|
|
|
type RequestParams struct {
|
|
|
|
Request struct {
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params []json.RawMessage `json:"params"`
|
|
|
|
} `json:"request"`
|
|
|
|
ChainID string `json:"chainId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SessionRequest struct {
|
|
|
|
ID int64 `json:"id"`
|
2023-11-19 17:29:17 +00:00
|
|
|
Topic Topic `json:"topic"`
|
2023-11-06 19:04:42 +00:00
|
|
|
Params RequestParams `json:"params"`
|
|
|
|
Verify VerifyContext `json:"verifyContext"`
|
|
|
|
}
|
|
|
|
|
2023-11-19 17:29:17 +00:00
|
|
|
type SessionDelete struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Topic Topic `json:"topic"`
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
type Session struct {
|
|
|
|
Acknowledged bool `json:"acknowledged"`
|
|
|
|
Controller string `json:"controller"`
|
|
|
|
Expiry int64 `json:"expiry"`
|
|
|
|
Namespaces map[string]Namespace `json:"namespaces"`
|
|
|
|
OptionalNamespaces map[string]Namespace `json:"optionalNamespaces"`
|
|
|
|
PairingTopic Topic `json:"pairingTopic"`
|
|
|
|
Peer Proposer `json:"peer"`
|
|
|
|
Relay json.RawMessage `json:"relay"`
|
|
|
|
RequiredNamespaces map[string]Namespace `json:"requiredNamespaces"`
|
|
|
|
Self Proposer `json:"self"`
|
|
|
|
Topic Topic `json:"topic"`
|
|
|
|
}
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
// Valid namespace
|
|
|
|
func (n *Namespace) Valid(namespaceName string, chainID *uint64) bool {
|
|
|
|
if chainID == nil {
|
|
|
|
if len(n.Chains) == 0 {
|
|
|
|
log.Warn("namespace doesn't refer to any chain")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, caip2Str := range n.Chains {
|
|
|
|
resolvedNamespaceName, _, err := parseCaip2ChainID(caip2Str)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("namespace chain not in caip2 format", "chain", caip2Str, "error", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if resolvedNamespaceName != namespaceName {
|
|
|
|
log.Warn("namespace name doesn't match", "namespace", namespaceName, "chain", caip2Str)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid params
|
|
|
|
func (p *Params) Valid() bool {
|
|
|
|
for key, ns := range p.RequiredNamespaces {
|
|
|
|
var chainID *uint64
|
|
|
|
if strings.Contains(key, ":") {
|
|
|
|
resolvedNamespaceName, cID, err := parseCaip2ChainID(key)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("params validation failed CAIP-2", "str", key, "error", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
key = resolvedNamespaceName
|
|
|
|
chainID = &cID
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isValidNamespaceName(key) {
|
|
|
|
log.Warn("invalid namespace name", "namespace", key)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ns.Valid(key, chainID) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid session propsal
|
|
|
|
// https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces#controller-side-validation-of-incoming-proposal-namespaces-wallet
|
|
|
|
func (p *SessionProposal) Valid() bool {
|
|
|
|
return p.Params.Valid()
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:58:57 +00:00
|
|
|
func sessionProposalToSupportedChain(caipChains []string, supportsChain func(uint64) bool) (chains []uint64, eipChains []string) {
|
|
|
|
chains = make([]uint64, 0, 1)
|
|
|
|
eipChains = make([]string, 0, 1)
|
|
|
|
for _, caip2Str := range caipChains {
|
2023-11-24 15:27:05 +00:00
|
|
|
_, chainID, err := parseCaip2ChainID(caip2Str)
|
2023-10-30 16:58:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Failed parsing CAIP-2", "str", caip2Str, "error", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !supportsChain(chainID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
eipChains = append(eipChains, caip2Str)
|
|
|
|
chains = append(chains, chainID)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
func caip10Accounts(accounts []*accounts.Account, chains []uint64) []string {
|
|
|
|
addresses := make([]string, 0, len(accounts)*len(chains))
|
|
|
|
for _, acc := range accounts {
|
2023-10-30 16:58:57 +00:00
|
|
|
for _, chainID := range chains {
|
2023-11-24 15:27:05 +00:00
|
|
|
addresses = append(addresses, fmt.Sprintf("%s:%s:%s", SupportedEip155Namespace, strconv.FormatUint(chainID, 10), acc.Address.Hex()))
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-24 15:27:05 +00:00
|
|
|
return addresses
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|