feat: use register ens api

This commit is contained in:
Anthony Laibe 2022-01-03 14:01:10 +01:00 committed by Anthony Laibe
parent 50993fda1c
commit 0928e15d02
6 changed files with 122 additions and 134 deletions

View File

@ -1,17 +1,14 @@
import sequtils
import strformat
import strutils
import nimcrypto
import json
import json_serialization
import tables
import stew/byteutils
import unicode
import algorithm
import web3/[ethtypes, conversions], stew/byteutils, stint
import web3/[ethtypes, conversions], stint
import chronicles, libp2p/[multihash, multicodec, cid]
import ./statusgo_backend/eth as eth
import ./statusgo_backend/wallet
import ./statusgo_backend/accounts as status_accounts
import ./statusgo_backend/settings as status_settings
@ -90,151 +87,101 @@ proc getPrice*(): Stuint[256] =
let res = status_ens.price(chainId)
return fromHex(Stuint[256], res.result.getStr)
proc label*(username:string): string =
var node:array[32, byte] = keccak_256.digest(username.toLower()).data
result = "0x" & node.toHex()
proc namehash*(ensName:string): string =
let name = ensName.toLower()
var node:array[32, byte]
node.fill(0)
var parts = name.split(".")
for i in countdown(parts.len - 1,0):
let elem = keccak_256.digest(parts[i]).data
var concatArrays: array[64, byte]
concatArrays[0..31] = node
concatArrays[32..63] = elem
node = keccak_256.digest(concatArrays).data
result = "0x" & node.toHex()
proc releaseEstimateGas*(username: string, address: string, success: var bool): int =
let
label = fromHex(FixedBytes[32], label(username))
network = status_settings.getCurrentNetwork().toNetwork()
ensUsernamesContract = contracts.findContract(network.chainId, "ens-usernames")
release = Release(label: label)
chainId = status_settings.getCurrentNetwork().toChainId()
txData = transactions.buildTransaction(parseAddress(address), 0.u256)
var tx = transactions.buildTokenTransaction(parseAddress(address), ensUsernamesContract.address, "", "")
try:
let response = ensUsernamesContract.methods["release"].estimateGas(tx, release, success)
if success:
result = fromHex[int](response)
except rpc_response.RpcException as e:
error "Could not estimate gas for ens release", err=e.msg
let resp = status_ens.releaseEstimate(chainId, txData, username)
result = resp.result.getInt
success = true
except:
success = false
result = 0
proc release*(username: string, address: string, gas, gasPrice, password: string, success: var bool): string =
let
label = fromHex(FixedBytes[32], label(username))
network = status_settings.getCurrentNetwork().toNetwork()
ensUsernamesContract = contracts.findContract(network.chainId, "ens-usernames")
release = Release(label: label)
chainId = status_settings.getCurrentNetwork().toChainId()
txData = transactions.buildTransaction(
parseAddress(address), 0.u256, gas, gasPrice
)
var tx = transactions.buildTokenTransaction(parseAddress(address), ensUsernamesContract.address, "", "")
try:
result = ensUsernamesContract.methods["release"].send(tx, release, password, success)
if success:
let resp = status_ens.release(chainId, txData, password, username)
result = resp.result.getStr
success = true
let ensUsernamesContract = contracts.findContract(chainId, "ens-usernames")
trackPendingTransaction(result, address, $ensUsernamesContract.address, PendingTransactionType.ReleaseENS, username)
except rpc_response.RpcException as e:
error "Could not estimate gas for ens release", err=e.msg
except:
success = false
result = "failed to release the username"
proc getExpirationTime*(username: string, success: var bool): int =
let chainId = status_settings.getCurrentNetwork().toChainId()
let res = status_ens.expireAt(chainId, username)
return fromHex[int](res.result.getStr)
proc extractCoordinates*(pubkey: string):tuple[x: string, y:string] =
result = ("0x" & pubkey[4..67], "0x" & pubkey[68..131])
proc registerUsernameEstimateGas*(username: string, address: string, pubKey: string, success: var bool): int =
let
label = fromHex(FixedBytes[32], label(username))
coordinates = extractCoordinates(pubkey)
x = fromHex(FixedBytes[32], coordinates.x)
y = fromHex(FixedBytes[32], coordinates.y)
network = status_settings.getCurrentNetwork().toNetwork()
ensUsernamesContract = contracts.findContract(network.chainId, "ens-usernames")
sntContract = contracts.findErc20Contract(network.chainId, network.sntSymbol())
price = getPrice()
chainId = status_settings.getCurrentNetwork().toChainId()
txData = transactions.buildTransaction(parseAddress(address), 0.u256)
let
register = Register(label: label, account: parseAddress(address), x: x, y: y)
registerAbiEncoded = ensUsernamesContract.methods["register"].encodeAbi(register)
approveAndCallObj = ApproveAndCall[132](to: ensUsernamesContract.address, value: price, data: DynamicBytes[132].fromHex(registerAbiEncoded))
approveAndCallAbiEncoded = sntContract.methods["approveAndCall"].encodeAbi(approveAndCallObj)
var tx = transactions.buildTokenTransaction(parseAddress(address), sntContract.address, "", "")
let response = sntContract.methods["approveAndCall"].estimateGas(tx, approveAndCallObj, success)
if success:
result = fromHex[int](response)
try:
let resp = status_ens.registerEstimate(chainId, txData, username, pubkey)
result = resp.result.getInt
success = true
except:
success = false
result = 0
proc registerUsername*(username, pubKey, address, gas, gasPrice: string, isEIP1559Enabled: bool, maxPriorityFeePerGas: string, maxFeePerGas: string, password: string, success: var bool): string =
let
label = fromHex(FixedBytes[32], label(username))
coordinates = extractCoordinates(pubkey)
x = fromHex(FixedBytes[32], coordinates.x)
y = fromHex(FixedBytes[32], coordinates.y)
network = status_settings.getCurrentNetwork().toNetwork()
ensUsernamesContract = contracts.findContract(network.chainId, "ens-usernames")
sntContract = contracts.findErc20Contract(network.chainId, network.sntSymbol)
price = getPrice()
chainId = network.chainId
txData = transactions.buildTransaction(
parseAddress(address), 0.u256, gas, gasPrice, isEIP1559Enabled, maxPriorityFeePerGas, maxFeePerGas
)
let
register = Register(label: label, account: parseAddress(address), x: x, y: y)
registerAbiEncoded = ensUsernamesContract.methods["register"].encodeAbi(register)
approveAndCallObj = ApproveAndCall[132](to: ensUsernamesContract.address, value: price, data: DynamicBytes[132].fromHex(registerAbiEncoded))
var tx = transactions.buildTokenTransaction(parseAddress(address), sntContract.address, gas, gasPrice, isEIP1559Enabled, maxPriorityFeePerGas, maxFeePerGas)
result = sntContract.methods["approveAndCall"].send(tx, approveAndCallObj, password, success)
if success:
trackPendingTransaction(result, address, $sntContract.address, PendingTransactionType.RegisterENS, username & domain)
try:
let resp = status_ens.register(chainId, txData, password, username, pubkey)
result = resp.result.getStr
success = true
let sntContract = contracts.findErc20Contract(chainId, network.sntSymbol())
trackPendingTransaction(result, address, $sntContract.address, PendingTransactionType.RegisterEns, username & domain)
except:
success = false
result = "failed to register the username"
proc setPubKeyEstimateGas*(username: string, address: string, pubKey: string, success: var bool): int =
var hash = namehash(username)
hash.removePrefix("0x")
let
label = fromHex(FixedBytes[32], "0x" & hash)
x = fromHex(FixedBytes[32], "0x" & pubkey[4..67])
y = fromHex(FixedBytes[32], "0x" & pubkey[68..131])
network = status_settings.getCurrentNetwork().toNetwork()
resolverContract = contracts.findContract(network.chainId, "ens-resolver")
setPubkey = SetPubkey(label: label, x: x, y: y)
resolverAddress = resolver(hash)
var tx = transactions.buildTokenTransaction(parseAddress(address), parseAddress(resolverAddress), "", "")
chainId = status_settings.getCurrentNetwork().toChainId()
txData = transactions.buildTransaction(parseAddress(address), 0.u256)
try:
let response = resolverContract.methods["setPubkey"].estimateGas(tx, setPubkey, success)
if success:
result = fromHex[int](response)
except rpc_response.RpcException as e:
raise
let resp = status_ens.setPubKeyEstimate(chainId, txData, username, pubkey)
result = resp.result.getInt
success = true
except:
success = false
result = 0
proc setPubKey*(username, pubKey, address, gas, gasPrice: string, isEIP1559Enabled: bool, maxPriorityFeePerGas: string, maxFeePerGas: string, password: string, success: var bool): string =
var hash = namehash(username)
hash.removePrefix("0x")
let
label = fromHex(FixedBytes[32], "0x" & hash)
x = fromHex(FixedBytes[32], "0x" & pubkey[4..67])
y = fromHex(FixedBytes[32], "0x" & pubkey[68..131])
network = status_settings.getCurrentNetwork().toNetwork()
resolverContract = contracts.findContract(network.chainId, "ens-resolver")
setPubkey = SetPubkey(label: label, x: x, y: y)
resolverAddress = resolver(hash)
var tx = transactions.buildTokenTransaction(parseAddress(address), parseAddress(resolverAddress), gas, gasPrice, isEIP1559Enabled, maxPriorityFeePerGas, maxFeePerGas)
chainId = status_settings.getCurrentNetwork().toChainId()
txData = transactions.buildTransaction(
parseAddress(address), 0.u256, gas, gasPrice, isEIP1559Enabled, maxPriorityFeePerGas, maxFeePerGas
)
try:
result = resolverContract.methods["setPubkey"].send(tx, setPubkey, password, success)
if success:
let resp = status_ens.setPubKey(chainId, txData, password, username, pubkey)
result = resp.result.getStr
success = true
let resolverAddress = resolver(username)
trackPendingTransaction(result, $address, resolverAddress, PendingTransactionType.SetPubKey, username)
except rpc_response.RpcException as e:
raise
except:
success = false
result = "failed to set the pubkey"
proc statusRegistrarAddress*():string =
let network = status_settings.getCurrentNetwork().toNetwork()

View File

@ -1,4 +1,4 @@
import json, json_serialization, chronicles, nimcrypto
import json, json_serialization, chronicles
import ./core, ./utils
import ./response_type
@ -136,9 +136,6 @@ proc storeAccounts*(id, hashedPassword: string): RpcResponse[JsonNode] {.raises:
error "error doing rpc request", methodName = "storeAccounts", exception=e.msg
raise newException(RpcException, e.msg)
proc hashPassword*(password: string): string =
result = "0x" & $keccak_256.digest(password)
proc saveAccount*(
address: string,
name: string,

View File

@ -1,6 +1,8 @@
import json
import json, web3/[conversions, ethtypes]
import ./core, ./response_type
import ../types/transaction
import ./conversions as conv
import ./utils
export response_type
proc resolver*(chainId: int, username: string): RpcResponse[JsonNode] {.raises: [Exception].} =
@ -40,3 +42,39 @@ proc price*(chainId: int): RpcResponse[JsonNode] {.raises: [Exception].} =
proc resourceURL*(chainId: int, username: string): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [chainId, username]
return core.callPrivateRPC("ens_resourceURL", payload)
proc register*(
chainId: int, txData: TransactionData, password: string, username: string, pubkey: string
): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [chainId, txData, hashPassword(password), username, pubkey]
return core.callPrivateRPC("ens_register", payload)
proc registerEstimate*(
chainId: int, txData: TransactionData, username: string, pubkey: string
): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [chainId, txData, username, pubkey]
return core.callPrivateRPC("ens_registerEstimate", payload)
proc release*(
chainId: int, txData: TransactionData, password: string, username: string
): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [chainId, txData, hashPassword(password), username]
return core.callPrivateRPC("ens_release", payload)
proc releaseEstimate*(
chainId: int, txData: TransactionData, username: string
): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [chainId, txData, username]
return core.callPrivateRPC("ens_releaseEstimate", payload)
proc setPubKey*(
chainId: int, txData: TransactionData, password: string, username: string, pubkey: string
): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [chainId, txData, hashPassword(password), username, pubkey]
return core.callPrivateRPC("ens_setPubKey", payload)
proc setPubKeyEstimate*(
chainId: int, txData: TransactionData, username: string, pubkey: string
): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [chainId, txData, username, pubkey]
return core.callPrivateRPC("ens_setPubKeyEstimate", payload)

View File

@ -1,7 +1,7 @@
import json, json_serialization, strformat, chronicles
import status_go
import accounts
import core
import ./utils
import ./core
logScope:
topics = "provider"

View File

@ -1,3 +1,5 @@
import nimcrypto
proc isWakuEnabled(): bool =
true # TODO:
@ -5,3 +7,7 @@ proc prefix*(methodName: string, isExt:bool = true): string =
result = if isWakuEnabled(): "waku" else: "shh"
result = result & (if isExt: "ext_" else: "_")
result = result & methodName
proc hashPassword*(password: string): string =
result = "0x" & $keccak_256.digest(password)

2
vendor/status-go vendored

@ -1 +1 @@
Subproject commit 9d8ec4432ebc0530788a87512a7e257efc465e75
Subproject commit 22bb09a94f702c20d017644156c0997f0c8f0a0e