mirror of
https://github.com/status-im/status-go.git
synced 2025-02-02 18:05:07 +00:00
c67d4030ac
author shashankshampi <shashank.sanket1995@gmail.com> 1729780155 +0530 committer shashankshampi <shashank.sanket1995@gmail.com> 1730274350 +0530 test: Code Migration from status-cli-tests fix_: functional tests (#5979) * fix_: generate on test-functional * chore(test)_: fix functional test assertion --------- Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com> feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977) * feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766) The original GH issue https://github.com/status-im/status-mobile/issues/21113 came from a request from the Legal team. We must show to Status v1 users the new terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2 from the stores. The solution we use is to create a flag in the accounts table, named hasAcceptedTerms. The flag will be set to true on the first account ever created in v2 and we provide a native call in mobile/status.go#AcceptTerms, which allows the client to persist the user's choice in case they are upgrading (from v1 -> v2, or from a v2 older than this PR). This solution is not the best because we should store the setting in a separate table, not in the accounts table. Related Mobile PR https://github.com/status-im/status-mobile/pull/21124 * fix(test)_: Compare addresses using uppercased strings --------- Co-authored-by: Icaro Motta <icaro.ldm@gmail.com> test_: restore account (#5960) feat_: `LogOnPanic` linter (#5969) * feat_: LogOnPanic linter * fix_: add missing defer LogOnPanic * chore_: make vendor * fix_: tests, address pr comments * fix_: address pr comments fix(ci)_: remove workspace and tmp dir This ensures we do not encounter weird errors like: ``` + ln -s /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907 /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go ln: failed to create symbolic link '/home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go': File exists script returned exit code 1 ``` Signed-off-by: Jakub Sokołowski <jakub@status.im> chore_: enable windows and macos CI build (#5840) - Added support for Windows and macOS in CI pipelines - Added missing dependencies for Windows and x86-64-darwin - Resolved macOS SDK version compatibility for darwin-x86_64 The `mkShell` override was necessary to ensure compatibility with the newer macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures because of the missing libs and frameworks. OverrideSDK creates a mapping from the default SDK in all package categories to the requested SDK (11.0). fix(contacts)_: fix trust status not being saved to cache when changed (#5965) Fixes https://github.com/status-im/status-desktop/issues/16392 cleanup added logger and cleanup review comments changes fix_: functional tests (#5979) * fix_: generate on test-functional * chore(test)_: fix functional test assertion --------- Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com> feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977) * feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766) The original GH issue https://github.com/status-im/status-mobile/issues/21113 came from a request from the Legal team. We must show to Status v1 users the new terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2 from the stores. The solution we use is to create a flag in the accounts table, named hasAcceptedTerms. The flag will be set to true on the first account ever created in v2 and we provide a native call in mobile/status.go#AcceptTerms, which allows the client to persist the user's choice in case they are upgrading (from v1 -> v2, or from a v2 older than this PR). This solution is not the best because we should store the setting in a separate table, not in the accounts table. Related Mobile PR https://github.com/status-im/status-mobile/pull/21124 * fix(test)_: Compare addresses using uppercased strings --------- Co-authored-by: Icaro Motta <icaro.ldm@gmail.com> test_: restore account (#5960) feat_: `LogOnPanic` linter (#5969) * feat_: LogOnPanic linter * fix_: add missing defer LogOnPanic * chore_: make vendor * fix_: tests, address pr comments * fix_: address pr comments chore_: enable windows and macos CI build (#5840) - Added support for Windows and macOS in CI pipelines - Added missing dependencies for Windows and x86-64-darwin - Resolved macOS SDK version compatibility for darwin-x86_64 The `mkShell` override was necessary to ensure compatibility with the newer macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures because of the missing libs and frameworks. OverrideSDK creates a mapping from the default SDK in all package categories to the requested SDK (11.0). fix(contacts)_: fix trust status not being saved to cache when changed (#5965) Fixes https://github.com/status-im/status-desktop/issues/16392 test_: remove port bind chore(wallet)_: move route execution code to separate module chore_: replace geth logger with zap logger (#5962) closes: #6002 feat(telemetry)_: add metrics for message reliability (#5899) * feat(telemetry)_: track message reliability Add metrics for dial errors, missed messages, missed relevant messages, and confirmed delivery. * fix_: handle error from json marshal chore_: use zap logger as request logger iterates: status-im/status-desktop#16536 test_: unique project per run test_: use docker compose v2, more concrete project name fix(codecov)_: ignore folders without tests Otherwise Codecov reports incorrect numbers when making changes. https://docs.codecov.com/docs/ignoring-paths Signed-off-by: Jakub Sokołowski <jakub@status.im> test_: verify schema of signals during init; fix schema verification warnings (#5947) fix_: update defaultGorushURL (#6011) fix(tests)_: use non-standard port to avoid conflicts We have observed `nimbus-eth2` build failures reporting this port: ```json { "lvl": "NTC", "ts": "2024-10-28 13:51:32.308+00:00", "msg": "REST HTTP server could not be started", "topics": "beacnde", "address": "127.0.0.1:5432", "reason": "(98) Address already in use" } ``` https://ci.status.im/job/nimbus-eth2/job/platforms/job/linux/job/x86_64/job/main/job/PR-6683/3/ Signed-off-by: Jakub Sokołowski <jakub@status.im> fix_: create request logger ad-hoc in tests Fixes `TestCall` failing when run concurrently. chore_: configure codecov (#6005) * chore_: configure codecov * fix_: after_n_builds
780 lines
20 KiB
Go
780 lines
20 KiB
Go
package ens
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"math/big"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
"github.com/multiformats/go-multibase"
|
|
"github.com/multiformats/go-multihash"
|
|
"github.com/pkg/errors"
|
|
"github.com/wealdtech/go-ens/v3"
|
|
"github.com/wealdtech/go-multicodec"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
"github.com/status-im/status-go/account"
|
|
gocommon "github.com/status-im/status-go/common"
|
|
"github.com/status-im/status-go/contracts"
|
|
"github.com/status-im/status-go/contracts/registrar"
|
|
"github.com/status-im/status-go/contracts/resolver"
|
|
"github.com/status-im/status-go/contracts/snt"
|
|
"github.com/status-im/status-go/logutils"
|
|
"github.com/status-im/status-go/params"
|
|
"github.com/status-im/status-go/rpc"
|
|
"github.com/status-im/status-go/services/utils"
|
|
wcommon "github.com/status-im/status-go/services/wallet/common"
|
|
"github.com/status-im/status-go/transactions"
|
|
)
|
|
|
|
const StatusDomain = "stateofus.eth"
|
|
|
|
func NewAPI(rpcClient *rpc.Client, accountsManager *account.GethManager, pendingTracker *transactions.PendingTxTracker, config *params.NodeConfig, appDb *sql.DB, timeSource func() time.Time, syncUserDetailFunc *syncUsernameDetail) *API {
|
|
return &API{
|
|
contractMaker: &contracts.ContractMaker{
|
|
RPCClient: rpcClient,
|
|
},
|
|
accountsManager: accountsManager,
|
|
pendingTracker: pendingTracker,
|
|
config: config,
|
|
addrPerChain: make(map[uint64]common.Address),
|
|
db: NewEnsDatabase(appDb),
|
|
|
|
quit: make(chan struct{}),
|
|
timeSource: timeSource,
|
|
syncUserDetailFunc: syncUserDetailFunc,
|
|
}
|
|
}
|
|
|
|
type URI struct {
|
|
Scheme string
|
|
Host string
|
|
Path string
|
|
}
|
|
|
|
// use this to avoid using messenger directly to avoid circular dependency (protocol->ens->protocol)
|
|
type syncUsernameDetail func(context.Context, *UsernameDetail) error
|
|
|
|
type API struct {
|
|
contractMaker *contracts.ContractMaker
|
|
accountsManager *account.GethManager
|
|
pendingTracker *transactions.PendingTxTracker
|
|
config *params.NodeConfig
|
|
|
|
addrPerChain map[uint64]common.Address
|
|
addrPerChainMutex sync.Mutex
|
|
|
|
quitOnce sync.Once
|
|
quit chan struct{}
|
|
|
|
db *Database
|
|
syncUserDetailFunc *syncUsernameDetail
|
|
|
|
timeSource func() time.Time
|
|
}
|
|
|
|
func (api *API) Stop() {
|
|
api.quitOnce.Do(func() {
|
|
close(api.quit)
|
|
})
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (api *API) Add(ctx context.Context, chainID uint64, username string) error {
|
|
ud := &UsernameDetail{Username: username, ChainID: chainID, Clock: api.unixTime()}
|
|
err := api.db.AddEnsUsername(ud)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return (*api.syncUserDetailFunc)(ctx, ud)
|
|
}
|
|
|
|
func (api *API) Remove(ctx context.Context, chainID uint64, username string) error {
|
|
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
|
|
}
|
|
|
|
func (api *API) GetRegistrarAddress(ctx context.Context, chainID uint64) (common.Address, error) {
|
|
return api.usernameRegistrarAddr(ctx, chainID)
|
|
}
|
|
|
|
func (api *API) Resolver(ctx context.Context, chainID uint64, username string) (*common.Address, error) {
|
|
err := ValidateENSUsername(username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
registry, err := api.contractMaker.NewRegistry(chainID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
|
|
resolver, err := registry.Resolver(callOpts, NameHash(username))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &resolver, nil
|
|
}
|
|
|
|
func (api *API) GetName(ctx context.Context, chainID uint64, address common.Address) (string, error) {
|
|
backend, err := api.contractMaker.RPCClient.EthClient(chainID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ens.ReverseResolve(backend, address)
|
|
}
|
|
|
|
func (api *API) OwnerOf(ctx context.Context, chainID uint64, username string) (*common.Address, error) {
|
|
err := ValidateENSUsername(username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
registry, err := api.contractMaker.NewRegistry(chainID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
|
|
owner, err := registry.Owner(callOpts, NameHash(username))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &owner, nil
|
|
}
|
|
|
|
func (api *API) ContentHash(ctx context.Context, chainID uint64, username string) ([]byte, error) {
|
|
err := ValidateENSUsername(username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resolverAddress, err := api.Resolver(ctx, chainID, username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resolver, err := api.contractMaker.NewPublicResolver(chainID, resolverAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
|
|
contentHash, err := resolver.Contenthash(callOpts, NameHash(username))
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
return contentHash, nil
|
|
}
|
|
|
|
func (api *API) PublicKeyOf(ctx context.Context, chainID uint64, username string) (string, error) {
|
|
err := ValidateENSUsername(username)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resolverAddress, err := api.Resolver(ctx, chainID, username)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resolver, err := api.contractMaker.NewPublicResolver(chainID, resolverAddress)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
|
|
pubKey, err := resolver.Pubkey(callOpts, NameHash(username))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return "0x04" + hex.EncodeToString(pubKey.X[:]) + hex.EncodeToString(pubKey.Y[:]), nil
|
|
}
|
|
|
|
func (api *API) AddressOf(ctx context.Context, chainID uint64, username string) (*common.Address, error) {
|
|
err := ValidateENSUsername(username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resolverAddress, err := api.Resolver(ctx, chainID, username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resolver, err := api.contractMaker.NewPublicResolver(chainID, resolverAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
|
|
addr, err := resolver.Addr(callOpts, NameHash(username))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &addr, nil
|
|
}
|
|
|
|
func (api *API) usernameRegistrarAddr(ctx context.Context, chainID uint64) (common.Address, error) {
|
|
logutils.ZapLogger().Info("obtaining username registrar address")
|
|
api.addrPerChainMutex.Lock()
|
|
defer api.addrPerChainMutex.Unlock()
|
|
addr, ok := api.addrPerChain[chainID]
|
|
if ok {
|
|
return addr, nil
|
|
}
|
|
|
|
registryAddr, err := api.OwnerOf(ctx, chainID, StatusDomain)
|
|
if err != nil {
|
|
return common.Address{}, err
|
|
}
|
|
|
|
api.addrPerChain[chainID] = *registryAddr
|
|
|
|
go func() {
|
|
defer gocommon.LogOnPanic()
|
|
registry, err := api.contractMaker.NewRegistry(chainID)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
logs := make(chan *resolver.ENSRegistryWithFallbackNewOwner)
|
|
|
|
sub, err := registry.WatchNewOwner(&bind.WatchOpts{}, logs, nil, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case <-api.quit:
|
|
logutils.ZapLogger().Info("quitting ens contract subscription")
|
|
sub.Unsubscribe()
|
|
return
|
|
case err := <-sub.Err():
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("ens contract subscription error: " + err.Error())
|
|
}
|
|
return
|
|
case vLog := <-logs:
|
|
api.addrPerChainMutex.Lock()
|
|
api.addrPerChain[chainID] = vLog.Owner
|
|
api.addrPerChainMutex.Unlock()
|
|
}
|
|
}
|
|
}()
|
|
|
|
return *registryAddr, nil
|
|
}
|
|
|
|
func (api *API) ExpireAt(ctx context.Context, chainID uint64, username string) (string, error) {
|
|
registryAddr, err := api.usernameRegistrarAddr(ctx, chainID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
registrar, err := api.contractMaker.NewUsernameRegistrar(chainID, registryAddr)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
|
|
expTime, err := registrar.GetExpirationTime(callOpts, UsernameToLabel(username))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return fmt.Sprintf("%x", expTime), nil
|
|
}
|
|
|
|
func (api *API) Price(ctx context.Context, chainID uint64) (string, error) {
|
|
registryAddr, err := api.usernameRegistrarAddr(ctx, chainID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
registrar, err := api.contractMaker.NewUsernameRegistrar(chainID, registryAddr)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
callOpts := &bind.CallOpts{Context: ctx, Pending: false}
|
|
price, err := registrar.GetPrice(callOpts)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return fmt.Sprintf("%x", price), nil
|
|
}
|
|
|
|
func (api *API) Release(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, password string, username string) (string, error) {
|
|
registryAddr, err := api.usernameRegistrarAddr(ctx, chainID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
registrar, err := api.contractMaker.NewUsernameRegistrar(chainID, registryAddr)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
txOpts := txArgs.ToTransactOpts(utils.GetSigner(chainID, api.accountsManager, api.config.KeyStoreDir, txArgs.From, password))
|
|
tx, err := registrar.Release(txOpts, UsernameToLabel(username))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = api.pendingTracker.TrackPendingTransaction(
|
|
wcommon.ChainID(chainID),
|
|
tx.Hash(),
|
|
common.Address(txArgs.From),
|
|
registryAddr,
|
|
transactions.ReleaseENS,
|
|
transactions.AutoDelete,
|
|
"",
|
|
)
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
|
|
return "", err
|
|
}
|
|
|
|
err = api.Remove(ctx, chainID, fullDomainName(username))
|
|
|
|
if err != nil {
|
|
logutils.ZapLogger().Warn("Releasing ENS username: transaction successful, but removing failed")
|
|
}
|
|
|
|
return tx.Hash().String(), nil
|
|
}
|
|
|
|
func (api *API) ReleasePrepareTxCallMsg(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string) (ethereum.CallMsg, error) {
|
|
registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
|
|
data, err := registrarABI.Pack("release", UsernameToLabel(username))
|
|
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
|
|
}
|
|
|
|
func (api *API) ReleasePrepareTx(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string) (interface{}, error) {
|
|
callMsg, err := api.ReleasePrepareTxCallMsg(ctx, chainID, txArgs, username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return toCallArg(callMsg), nil
|
|
}
|
|
|
|
func (api *API) ReleaseEstimate(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string) (uint64, error) {
|
|
registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
data, err := registrarABI.Pack("release", UsernameToLabel(username))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
ethClient, err := api.contractMaker.RPCClient.EthClient(chainID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
registryAddr, err := api.usernameRegistrarAddr(ctx, chainID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
estimate, err := ethClient.EstimateGas(ctx, ethereum.CallMsg{
|
|
From: common.Address(txArgs.From),
|
|
To: ®istryAddr,
|
|
Value: big.NewInt(0),
|
|
Data: data,
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return estimate + 1000, nil
|
|
}
|
|
|
|
func (api *API) Register(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, password string, username string, pubkey string) (string, error) {
|
|
snt, err := api.contractMaker.NewSNT(chainID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
priceHex, err := api.Price(ctx, chainID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
price := new(big.Int)
|
|
price.SetString(priceHex, 16)
|
|
|
|
registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
x, y := ExtractCoordinates(pubkey)
|
|
extraData, err := registrarABI.Pack("register", UsernameToLabel(username), common.Address(txArgs.From), x, y)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
registryAddr, err := api.usernameRegistrarAddr(ctx, chainID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
txOpts := txArgs.ToTransactOpts(utils.GetSigner(chainID, api.accountsManager, api.config.KeyStoreDir, txArgs.From, password))
|
|
tx, err := snt.ApproveAndCall(
|
|
txOpts,
|
|
registryAddr,
|
|
price,
|
|
extraData,
|
|
)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = api.pendingTracker.TrackPendingTransaction(
|
|
wcommon.ChainID(chainID),
|
|
tx.Hash(),
|
|
common.Address(txArgs.From),
|
|
registryAddr,
|
|
transactions.RegisterENS,
|
|
transactions.AutoDelete,
|
|
"",
|
|
)
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
|
|
return "", err
|
|
}
|
|
|
|
err = api.Add(ctx, chainID, fullDomainName(username))
|
|
if err != nil {
|
|
logutils.ZapLogger().Warn("Registering ENS username: transaction successful, but adding failed")
|
|
}
|
|
|
|
return tx.Hash().String(), nil
|
|
}
|
|
|
|
func (api *API) RegisterPrepareTxCallMsg(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string, pubkey string) (ethereum.CallMsg, error) {
|
|
priceHex, err := api.Price(ctx, chainID)
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
price := new(big.Int)
|
|
price.SetString(priceHex, 16)
|
|
|
|
registrarABI, err := abi.JSON(strings.NewReader(registrar.UsernameRegistrarABI))
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
|
|
x, y := ExtractCoordinates(pubkey)
|
|
extraData, err := registrarABI.Pack("register", UsernameToLabel(username), common.Address(txArgs.From), x, y)
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
|
|
sntABI, err := abi.JSON(strings.NewReader(snt.SNTABI))
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
|
|
registryAddr, err := api.usernameRegistrarAddr(ctx, chainID)
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
|
|
data, err := sntABI.Pack("approveAndCall", registryAddr, price, extraData)
|
|
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
|
|
}
|
|
|
|
func (api *API) RegisterPrepareTx(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string, pubkey string) (interface{}, error) {
|
|
callMsg, err := api.RegisterPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return toCallArg(callMsg), nil
|
|
}
|
|
|
|
func (api *API) RegisterEstimate(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string, pubkey string) (uint64, error) {
|
|
ethClient, err := api.contractMaker.RPCClient.EthClient(chainID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
callMsg, err := api.RegisterPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
estimate, err := ethClient.EstimateGas(ctx, callMsg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return estimate + 1000, nil
|
|
}
|
|
|
|
func (api *API) SetPubKey(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, password string, username string, pubkey string) (string, error) {
|
|
err := ValidateENSUsername(username)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resolverAddress, err := api.Resolver(ctx, chainID, username)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resolver, err := api.contractMaker.NewPublicResolver(chainID, resolverAddress)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
x, y := ExtractCoordinates(pubkey)
|
|
txOpts := txArgs.ToTransactOpts(utils.GetSigner(chainID, api.accountsManager, api.config.KeyStoreDir, txArgs.From, password))
|
|
tx, err := resolver.SetPubkey(txOpts, NameHash(username), x, y)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = api.pendingTracker.TrackPendingTransaction(
|
|
wcommon.ChainID(chainID),
|
|
tx.Hash(),
|
|
common.Address(txArgs.From),
|
|
*resolverAddress,
|
|
transactions.SetPubKey,
|
|
transactions.AutoDelete,
|
|
"",
|
|
)
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
|
|
return "", err
|
|
}
|
|
|
|
err = api.Add(ctx, chainID, fullDomainName(username))
|
|
|
|
if err != nil {
|
|
logutils.ZapLogger().Warn("Registering ENS username: transaction successful, but adding failed")
|
|
}
|
|
|
|
return tx.Hash().String(), nil
|
|
}
|
|
|
|
func (api *API) SetPubKeyPrepareTxCallMsg(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string, pubkey string) (ethereum.CallMsg, error) {
|
|
err := ValidateENSUsername(username)
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
x, y := ExtractCoordinates(pubkey)
|
|
|
|
resolverABI, err := abi.JSON(strings.NewReader(resolver.PublicResolverABI))
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
|
|
data, err := resolverABI.Pack("setPubkey", NameHash(username), x, y)
|
|
if err != nil {
|
|
return ethereum.CallMsg{}, err
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (api *API) SetPubKeyPrepareTx(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string, pubkey string) (interface{}, error) {
|
|
callMsg, err := api.SetPubKeyPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return toCallArg(callMsg), nil
|
|
}
|
|
|
|
func (api *API) SetPubKeyEstimate(ctx context.Context, chainID uint64, txArgs transactions.SendTxArgs, username string, pubkey string) (uint64, error) {
|
|
ethClient, err := api.contractMaker.RPCClient.EthClient(chainID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
callMsg, err := api.SetPubKeyPrepareTxCallMsg(ctx, chainID, txArgs, username, pubkey)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
estimate, err := ethClient.EstimateGas(ctx, callMsg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return estimate + 1000, nil
|
|
}
|
|
|
|
func (api *API) ResourceURL(ctx context.Context, chainID uint64, username string) (*URI, error) {
|
|
scheme := "https"
|
|
contentHash, err := api.ContentHash(ctx, chainID, username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(contentHash) == 0 {
|
|
return &URI{}, nil
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
parsedURL, _ := url.Parse(params.IpfsGatewayURL)
|
|
// Remove scheme from the url
|
|
host := parsedURL.Hostname() + parsedURL.Path + str
|
|
return &URI{scheme, host, ""}, nil
|
|
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
|
|
}
|
|
|
|
return &URI{scheme, string(decodedMHash.Digest), ""}, nil
|
|
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) + "/"
|
|
return &URI{scheme, "swarm-gateways.net", path}, nil
|
|
default:
|
|
return nil, fmt.Errorf("unknown codec name %s", codecName)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func fullDomainName(username string) string {
|
|
return username + "." + StatusDomain
|
|
}
|