From 0928e15d0213a112885c8d2b40f6a66b0ed91f7a Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Mon, 3 Jan 2022 14:01:10 +0100 Subject: [PATCH] feat: use register ens api --- status/ens.nim | 193 ++++++++--------------- status/statusgo_backend_new/accounts.nim | 5 +- status/statusgo_backend_new/ens.nim | 44 +++++- status/statusgo_backend_new/provider.nim | 4 +- status/statusgo_backend_new/utils.nim | 8 +- vendor/status-go | 2 +- 6 files changed, 122 insertions(+), 134 deletions(-) diff --git a/status/ens.nim b/status/ens.nim index aed439d..5fe0bad 100644 --- a/status/ens.nim +++ b/status/ens.nim @@ -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) - - var tx = transactions.buildTokenTransaction(parseAddress(address), ensUsernamesContract.address, "", "") + chainId = status_settings.getCurrentNetwork().toChainId() + txData = transactions.buildTransaction(parseAddress(address), 0.u256) + 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) - - var tx = transactions.buildTokenTransaction(parseAddress(address), ensUsernamesContract.address, "", "") + chainId = status_settings.getCurrentNetwork().toChainId() + txData = transactions.buildTransaction( + parseAddress(address), 0.u256, gas, gasPrice + ) + try: - result = ensUsernamesContract.methods["release"].send(tx, release, password, success) - if success: - 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 + 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: + 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() - - 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) + chainId = status_settings.getCurrentNetwork().toChainId() + txData = transactions.buildTransaction(parseAddress(address), 0.u256) + + 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() - - 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) + chainId = network.chainId + txData = transactions.buildTransaction( + parseAddress(address), 0.u256, gas, gasPrice, isEIP1559Enabled, maxPriorityFeePerGas, maxFeePerGas + ) + + 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: - trackPendingTransaction(result, $address, resolverAddress, PendingTransactionType.SetPubKey, username) - except rpc_response.RpcException as e: - raise + 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: + success = false + result = "failed to set the pubkey" proc statusRegistrarAddress*():string = let network = status_settings.getCurrentNetwork().toNetwork() diff --git a/status/statusgo_backend_new/accounts.nim b/status/statusgo_backend_new/accounts.nim index 37e4abf..29024ec 100644 --- a/status/statusgo_backend_new/accounts.nim +++ b/status/statusgo_backend_new/accounts.nim @@ -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, diff --git a/status/statusgo_backend_new/ens.nim b/status/statusgo_backend_new/ens.nim index 9d012c9..ed31e50 100644 --- a/status/statusgo_backend_new/ens.nim +++ b/status/statusgo_backend_new/ens.nim @@ -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].} = @@ -39,4 +41,40 @@ 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) \ No newline at end of file + 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) \ No newline at end of file diff --git a/status/statusgo_backend_new/provider.nim b/status/statusgo_backend_new/provider.nim index eda5d77..c4eb826 100644 --- a/status/statusgo_backend_new/provider.nim +++ b/status/statusgo_backend_new/provider.nim @@ -1,7 +1,7 @@ import json, json_serialization, strformat, chronicles import status_go -import accounts -import core +import ./utils +import ./core logScope: topics = "provider" diff --git a/status/statusgo_backend_new/utils.nim b/status/statusgo_backend_new/utils.nim index 6fff86a..77e0554 100644 --- a/status/statusgo_backend_new/utils.nim +++ b/status/statusgo_backend_new/utils.nim @@ -1,7 +1,13 @@ +import nimcrypto + proc isWakuEnabled(): bool = true # TODO: proc prefix*(methodName: string, isExt:bool = true): string = result = if isWakuEnabled(): "waku" else: "shh" result = result & (if isExt: "ext_" else: "_") - result = result & methodName \ No newline at end of file + result = result & methodName + + +proc hashPassword*(password: string): string = + result = "0x" & $keccak_256.digest(password) \ No newline at end of file diff --git a/vendor/status-go b/vendor/status-go index 9d8ec44..22bb09a 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit 9d8ec4432ebc0530788a87512a7e257efc465e75 +Subproject commit 22bb09a94f702c20d017644156c0997f0c8f0a0e