2021-12-21 15:05:09 +00:00
|
|
|
package ens
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-20 10:34:30 +00:00
|
|
|
"database/sql"
|
2021-12-21 15:05:09 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2022-01-14 11:17:31 +00:00
|
|
|
"math/big"
|
2022-08-11 14:27:25 +00:00
|
|
|
"net/url"
|
2022-01-14 11:17:31 +00:00
|
|
|
"strings"
|
2023-04-26 15:37:18 +00:00
|
|
|
"time"
|
2021-12-21 15:05:09 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/multiformats/go-multibase"
|
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/wealdtech/go-multicodec"
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
2021-12-21 15:05:09 +00:00
|
|
|
|
2022-01-14 11:17:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2021-12-21 15:05:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-02-10 14:19:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-01-14 11:17:31 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2022-02-02 22:50:55 +00:00
|
|
|
"github.com/status-im/status-go/contracts/registrar"
|
|
|
|
"github.com/status-im/status-go/contracts/resolver"
|
|
|
|
"github.com/status-im/status-go/contracts/snt"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2022-01-14 11:17:31 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2021-12-21 15:05:09 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2024-11-05 11:05:23 +00:00
|
|
|
"github.com/status-im/status-go/services/ens/ensresolver"
|
2023-01-12 15:17:21 +00:00
|
|
|
"github.com/status-im/status-go/services/utils"
|
2023-08-01 18:50:30 +00:00
|
|
|
wcommon "github.com/status-im/status-go/services/wallet/common"
|
2024-11-05 21:28:24 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/wallettypes"
|
2022-01-14 11:17:31 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2021-12-21 15:05:09 +00:00
|
|
|
)
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
func NewAPI(rpcClient *rpc.Client, accountsManager *account.GethManager, pendingTracker *transactions.PendingTxTracker, config *params.NodeConfig, appDb *sql.DB, timeSource func() time.Time, syncUserDetailFunc *syncUsernameDetail) *API {
|
2021-12-21 15:05:09 +00:00
|
|
|
return &API{
|
2024-11-05 11:05:23 +00:00
|
|
|
ensResolver: ensresolver.NewEnsResolver(rpcClient),
|
|
|
|
|
2022-01-14 11:17:31 +00:00
|
|
|
accountsManager: accountsManager,
|
2023-08-01 18:50:30 +00:00
|
|
|
pendingTracker: pendingTracker,
|
2022-01-14 11:17:31 +00:00
|
|
|
config: config,
|
2023-01-20 10:34:30 +00:00
|
|
|
db: NewEnsDatabase(appDb),
|
2022-08-23 12:29:30 +00:00
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
timeSource: timeSource,
|
|
|
|
syncUserDetailFunc: syncUserDetailFunc,
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
type URI struct {
|
2021-12-21 15:05:09 +00:00
|
|
|
Scheme string
|
|
|
|
Host string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
// use this to avoid using messenger directly to avoid circular dependency (protocol->ens->protocol)
|
|
|
|
type syncUsernameDetail func(context.Context, *UsernameDetail) error
|
|
|
|
|
2021-12-21 15:05:09 +00:00
|
|
|
type API struct {
|
2024-11-05 11:05:23 +00:00
|
|
|
ensResolver *ensresolver.EnsResolver
|
2022-01-14 11:17:31 +00:00
|
|
|
accountsManager *account.GethManager
|
2023-08-01 18:50:30 +00:00
|
|
|
pendingTracker *transactions.PendingTxTracker
|
2022-01-14 11:17:31 +00:00
|
|
|
config *params.NodeConfig
|
2022-08-23 12:29:30 +00:00
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
db *Database
|
|
|
|
syncUserDetailFunc *syncUsernameDetail
|
|
|
|
|
|
|
|
timeSource func() time.Time
|
2022-08-23 12:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) Stop() {
|
2024-11-05 11:05:23 +00:00
|
|
|
api.ensResolver.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) EnsResolver() *ensresolver.EnsResolver {
|
|
|
|
return api.ensResolver
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 15:37:18 +00:00
|
|
|
func (api *API) unixTime() uint64 {
|
|
|
|
return uint64(api.timeSource().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) GetEnsUsernames(ctx context.Context) ([]*UsernameDetail, error) {
|
|
|
|
removed := false
|
|
|
|
return api.db.GetEnsUsernames(&removed)
|
2023-01-20 10:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) Add(ctx context.Context, chainID uint64, username string) error {
|
2023-04-26 15:37:18 +00:00
|
|
|
ud := &UsernameDetail{Username: username, ChainID: chainID, Clock: api.unixTime()}
|
|
|
|
err := api.db.AddEnsUsername(ud)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return (*api.syncUserDetailFunc)(ctx, ud)
|
2023-01-20 10:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) Remove(ctx context.Context, chainID uint64, username string) error {
|
2023-04-26 15:37:18 +00:00
|
|
|
ud := &UsernameDetail{Username: username, ChainID: chainID, Clock: api.unixTime()}
|
|
|
|
affected, err := api.db.RemoveEnsUsername(ud)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if affected {
|
|
|
|
return (*api.syncUserDetailFunc)(ctx, ud)
|
|
|
|
}
|
|
|
|
return nil
|
2023-01-20 10:34:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 16:54:44 +00:00
|
|
|
func (api *API) GetRegistrarAddress(ctx context.Context, chainID uint64) (common.Address, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.GetRegistrarAddress(ctx, chainID)
|
2022-04-04 16:54:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:05:09 +00:00
|
|
|
func (api *API) Resolver(ctx context.Context, chainID uint64, username string) (*common.Address, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.Resolver(ctx, chainID, username)
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 10:07:44 +00:00
|
|
|
func (api *API) GetName(ctx context.Context, chainID uint64, address common.Address) (string, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.GetName(ctx, chainID, address)
|
2022-10-24 10:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 15:05:09 +00:00
|
|
|
func (api *API) OwnerOf(ctx context.Context, chainID uint64, username string) (*common.Address, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.OwnerOf(ctx, chainID, username)
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) ContentHash(ctx context.Context, chainID uint64, username string) ([]byte, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.ContentHash(ctx, chainID, username)
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:07:33 +00:00
|
|
|
func (api *API) PublicKeyOf(ctx context.Context, chainID uint64, username string) (string, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.PublicKeyOf(ctx, chainID, username)
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) AddressOf(ctx context.Context, chainID uint64, username string) (*common.Address, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.AddressOf(ctx, chainID, username)
|
2022-08-23 12:29:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:07:33 +00:00
|
|
|
func (api *API) ExpireAt(ctx context.Context, chainID uint64, username string) (string, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.ExpireAt(ctx, chainID, username)
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:07:33 +00:00
|
|
|
func (api *API) Price(ctx context.Context, chainID uint64) (string, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.Price(ctx, chainID)
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `Release` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) Release(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, password string, username string) (string, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
registryAddr, err := api.ensResolver.GetRegistrarAddress(ctx, chainID)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-11-20 07:28:18 +00:00
|
|
|
signFn := utils.VerifyPasswordAndGetSigner(chainID, api.accountsManager, api.config.KeyStoreDir, txArgs.From, password)
|
2024-11-05 11:05:23 +00:00
|
|
|
tx, err := api.ensResolver.Release(ctx, chainID, registryAddr, txArgs, username, signFn)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
err = api.pendingTracker.TrackPendingTransaction(
|
|
|
|
wcommon.ChainID(chainID),
|
|
|
|
tx.Hash(),
|
|
|
|
common.Address(txArgs.From),
|
2024-03-14 08:39:06 +00:00
|
|
|
registryAddr,
|
2023-08-01 18:50:30 +00:00
|
|
|
transactions.ReleaseENS,
|
|
|
|
transactions.AutoDelete,
|
2024-03-14 08:39:06 +00:00
|
|
|
"",
|
2023-08-01 18:50:30 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
|
2023-08-01 18:50:30 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2023-01-20 10:34:30 +00:00
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
err = api.Remove(ctx, chainID, wcommon.FullDomainName(username))
|
2023-01-20 10:34:30 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Warn("Releasing ENS username: transaction successful, but removing failed")
|
2023-01-20 10:34:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 11:17:31 +00:00
|
|
|
return tx.Hash().String(), nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `ReleasePrepareTxCallMsg` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) ReleasePrepareTxCallMsg(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string) (ethereum.CallMsg, error) {
|
2023-11-29 11:34:16 +00:00
|
|
|
registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
|
|
|
|
if err != nil {
|
|
|
|
return ethereum.CallMsg{}, err
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
data, err := registrarABI.Pack("release", wcommon.UsernameToLabel(username))
|
2023-11-29 11:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return ethereum.CallMsg{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sntAddress, err := snt.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return ethereum.CallMsg{}, err
|
|
|
|
}
|
|
|
|
return ethereum.CallMsg{
|
|
|
|
From: common.Address(txArgs.From),
|
|
|
|
To: &sntAddress,
|
|
|
|
Value: big.NewInt(0),
|
|
|
|
Data: data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) ReleasePrepareTx(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string) (interface{}, error) {
|
2023-11-29 11:34:16 +00:00
|
|
|
callMsg, err := api.ReleasePrepareTxCallMsg(ctx, chainID, txArgs, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toCallArg(callMsg), nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `ReleaseEstimate` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) ReleaseEstimate(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string) (uint64, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
callMsg, err := api.ReleasePrepareTxCallMsg(ctx, chainID, txArgs, username)
|
2022-02-02 22:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.ReleaseEstimate(ctx, chainID, callMsg)
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `Register` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) Register(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, password string, username string, pubkey string) (string, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
registryAddr, err := api.ensResolver.GetRegistrarAddress(ctx, chainID)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-11-20 07:28:18 +00:00
|
|
|
signFn := utils.VerifyPasswordAndGetSigner(chainID, api.accountsManager, api.config.KeyStoreDir, txArgs.From, password)
|
2022-01-14 11:17:31 +00:00
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
tx, err := api.ensResolver.Register(ctx, chainID, registryAddr, txArgs, username, pubkey, signFn)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
err = api.pendingTracker.TrackPendingTransaction(
|
|
|
|
wcommon.ChainID(chainID),
|
|
|
|
tx.Hash(),
|
|
|
|
common.Address(txArgs.From),
|
2024-03-14 08:39:06 +00:00
|
|
|
registryAddr,
|
2023-08-01 18:50:30 +00:00
|
|
|
transactions.RegisterENS,
|
|
|
|
transactions.AutoDelete,
|
2024-03-14 08:39:06 +00:00
|
|
|
"",
|
2023-08-01 18:50:30 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
|
2023-08-01 18:50:30 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2023-01-20 10:34:30 +00:00
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
err = api.Add(ctx, chainID, wcommon.FullDomainName(username))
|
2023-01-20 10:34:30 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Warn("Registering ENS username: transaction successful, but adding failed")
|
2023-01-20 10:34:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 11:17:31 +00:00
|
|
|
return tx.Hash().String(), nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `RegisterPrepareTxCallMsg` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) RegisterPrepareTxCallMsg(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string, pubkey string) (ethereum.CallMsg, error) {
|
2022-01-14 11:17:31 +00:00
|
|
|
priceHex, err := api.Price(ctx, chainID)
|
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
price := new(big.Int)
|
|
|
|
price.SetString(priceHex, 16)
|
|
|
|
|
|
|
|
registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
|
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
x, y := wcommon.ExtractCoordinates(pubkey)
|
|
|
|
extraData, err := registrarABI.Pack("register", wcommon.UsernameToLabel(username), common.Address(txArgs.From), x, y)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
sntABI, err := abi.JSON(strings.NewReader(snt.SNTABI))
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
registryAddr, err := api.ensResolver.GetRegistrarAddress(ctx, chainID)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 12:29:30 +00:00
|
|
|
data, err := sntABI.Pack("approveAndCall", registryAddr, price, extraData)
|
2022-02-02 22:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return ethereum.CallMsg{}, err
|
|
|
|
}
|
2022-02-10 14:19:53 +00:00
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
sntAddress, err := snt.ContractAddress(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return ethereum.CallMsg{}, err
|
|
|
|
}
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{
|
2022-01-14 11:17:31 +00:00
|
|
|
From: common.Address(txArgs.From),
|
2022-02-02 22:50:55 +00:00
|
|
|
To: &sntAddress,
|
2022-01-14 11:17:31 +00:00
|
|
|
Value: big.NewInt(0),
|
|
|
|
Data: data,
|
2022-02-10 14:19:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `RegisterPrepareTx` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) RegisterPrepareTx(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string, pubkey string) (interface{}, error) {
|
2022-02-10 14:19:53 +00:00
|
|
|
callMsg, err := api.RegisterPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toCallArg(callMsg), nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `RegisterEstimate` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) RegisterEstimate(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string, pubkey string) (uint64, error) {
|
2022-02-10 14:19:53 +00:00
|
|
|
callMsg, err := api.RegisterPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.RegisterEstimate(ctx, chainID, callMsg)
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `SetPubKey` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) SetPubKey(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, password string, username string, pubkey string) (string, error) {
|
2022-01-14 11:17:31 +00:00
|
|
|
resolverAddress, err := api.Resolver(ctx, chainID, username)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-11-20 07:28:18 +00:00
|
|
|
signFn := utils.VerifyPasswordAndGetSigner(chainID, api.accountsManager, api.config.KeyStoreDir, txArgs.From, password)
|
2024-11-05 11:05:23 +00:00
|
|
|
tx, err := api.ensResolver.SetPubKey(ctx, chainID, resolverAddress, txArgs, username, pubkey, signFn)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
err = api.pendingTracker.TrackPendingTransaction(
|
|
|
|
wcommon.ChainID(chainID),
|
|
|
|
tx.Hash(),
|
|
|
|
common.Address(txArgs.From),
|
2024-03-14 08:39:06 +00:00
|
|
|
*resolverAddress,
|
2023-08-01 18:50:30 +00:00
|
|
|
transactions.SetPubKey,
|
|
|
|
transactions.AutoDelete,
|
2024-03-14 08:39:06 +00:00
|
|
|
"",
|
2023-08-01 18:50:30 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
|
2023-08-01 18:50:30 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
err = api.Add(ctx, chainID, wcommon.FullDomainName(username))
|
2023-01-20 10:34:30 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Warn("Registering ENS username: transaction successful, but adding failed")
|
2023-01-20 10:34:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 11:17:31 +00:00
|
|
|
return tx.Hash().String(), nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `SetPubKeyPrepareTxCallMsg` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) SetPubKeyPrepareTxCallMsg(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string, pubkey string) (ethereum.CallMsg, error) {
|
2024-11-05 11:05:23 +00:00
|
|
|
err := wcommon.ValidateENSUsername(username)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
2024-11-05 11:05:23 +00:00
|
|
|
x, y := wcommon.ExtractCoordinates(pubkey)
|
2022-01-14 11:17:31 +00:00
|
|
|
|
|
|
|
resolverABI, err := abi.JSON(strings.NewReader(resolver.PublicResolverABI))
|
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
data, err := resolverABI.Pack("setPubkey", wcommon.NameHash(username), x, y)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
2022-02-10 14:19:53 +00:00
|
|
|
return ethereum.CallMsg{}, err
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 14:19:53 +00:00
|
|
|
resolverAddress, err := api.Resolver(ctx, chainID, username)
|
|
|
|
if err != nil {
|
|
|
|
return ethereum.CallMsg{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethereum.CallMsg{
|
|
|
|
From: common.Address(txArgs.From),
|
|
|
|
To: resolverAddress,
|
|
|
|
Value: big.NewInt(0),
|
|
|
|
Data: data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `SetPubKeyPrepareTx` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) SetPubKeyPrepareTx(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string, pubkey string) (interface{}, error) {
|
2022-02-10 14:19:53 +00:00
|
|
|
callMsg, err := api.SetPubKeyPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toCallArg(callMsg), nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
// Deprecated: `SetPubKeyEstimate` was used before introducing a new, uniform, sending flow that uses router.
|
|
|
|
// Releasing ens username should start from calling `wallet_getSuggestedRoutesAsync`
|
|
|
|
// TODO: remove once mobile switches to a new sending flow.
|
2024-11-05 21:28:24 +00:00
|
|
|
func (api *API) SetPubKeyEstimate(ctx context.Context, chainID uint64, txArgs wallettypes.SendTxArgs, username string, pubkey string) (uint64, error) {
|
2022-02-10 14:19:53 +00:00
|
|
|
callMsg, err := api.SetPubKeyPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
2022-01-14 11:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:05:23 +00:00
|
|
|
return api.ensResolver.SetPubKeyEstimate(ctx, chainID, callMsg)
|
2022-01-14 11:17:31 +00:00
|
|
|
}
|
2021-12-21 15:05:09 +00:00
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func (api *API) ResourceURL(ctx context.Context, chainID uint64, username string) (*URI, error) {
|
2021-12-21 15:05:09 +00:00
|
|
|
scheme := "https"
|
|
|
|
contentHash, err := api.ContentHash(ctx, chainID, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(contentHash) == 0 {
|
2022-03-28 10:10:40 +00:00
|
|
|
return &URI{}, nil
|
2021-12-21 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, codec, err := multicodec.RemoveCodec(contentHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codecName, err := multicodec.Name(codec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch codecName {
|
|
|
|
case "ipfs-ns":
|
|
|
|
thisCID, err := cid.Parse(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse CID")
|
|
|
|
}
|
|
|
|
str, err := thisCID.StringOfBase(multibase.Base32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to obtain base36 representation")
|
|
|
|
}
|
2022-08-11 14:27:25 +00:00
|
|
|
|
|
|
|
parsedURL, _ := url.Parse(params.IpfsGatewayURL)
|
|
|
|
// Remove scheme from the url
|
|
|
|
host := parsedURL.Hostname() + parsedURL.Path + str
|
2022-03-28 10:10:40 +00:00
|
|
|
return &URI{scheme, host, ""}, nil
|
2021-12-21 15:05:09 +00:00
|
|
|
case "ipns-ns":
|
|
|
|
id, offset := binary.Uvarint(data)
|
|
|
|
if id == 0 {
|
|
|
|
return nil, fmt.Errorf("unknown CID")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, _, err := multicodec.RemoveCodec(data[offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decodedMHash, err := multihash.Decode(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
return &URI{scheme, string(decodedMHash.Digest), ""}, nil
|
2021-12-21 15:05:09 +00:00
|
|
|
case "swarm-ns":
|
|
|
|
id, offset := binary.Uvarint(data)
|
|
|
|
if id == 0 {
|
|
|
|
return nil, fmt.Errorf("unknown CID")
|
|
|
|
}
|
|
|
|
data, _, err := multicodec.RemoveCodec(data[offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decodedMHash, err := multihash.Decode(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path := "/bzz:/" + hex.EncodeToString(decodedMHash.Digest) + "/"
|
2022-03-28 10:10:40 +00:00
|
|
|
return &URI{scheme, "swarm-gateways.net", path}, nil
|
2021-12-21 15:05:09 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown codec name %s", codecName)
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 14:19:53 +00:00
|
|
|
|
|
|
|
func toCallArg(msg ethereum.CallMsg) interface{} {
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"from": msg.From,
|
|
|
|
"to": msg.To,
|
|
|
|
}
|
|
|
|
if len(msg.Data) > 0 {
|
|
|
|
arg["data"] = hexutil.Bytes(msg.Data)
|
|
|
|
}
|
|
|
|
if msg.Value != nil {
|
|
|
|
arg["value"] = (*hexutil.Big)(msg.Value)
|
|
|
|
}
|
|
|
|
if msg.Gas != 0 {
|
|
|
|
arg["gas"] = hexutil.Uint64(msg.Gas)
|
|
|
|
}
|
|
|
|
if msg.GasPrice != nil {
|
|
|
|
arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|