nim-ethers/ethers/wallet.nim

67 lines
2.2 KiB
Nim
Raw Normal View History

2022-07-12 18:23:43 +00:00
import eth/keys
import ./provider
2023-07-04 09:38:48 +00:00
import ./transaction
2022-07-12 18:23:43 +00:00
import ./signer
2023-07-04 08:40:22 +00:00
import ./wallet/error
2023-07-04 09:38:48 +00:00
import ./wallet/signing
2022-07-12 18:23:43 +00:00
export keys
2023-07-04 08:40:22 +00:00
export WalletError
2022-07-12 18:23:43 +00:00
2022-07-21 00:00:57 +00:00
var rng {.threadvar.}: ref HmacDrbgContext
proc getRng: ref HmacDrbgContext =
2022-07-14 08:22:54 +00:00
if rng.isNil:
2022-07-21 00:00:57 +00:00
rng = newRng()
2022-07-14 08:22:54 +00:00
rng
2022-07-12 18:23:43 +00:00
type Wallet* = ref object of Signer
privateKey*: PrivateKey
publicKey*: PublicKey
address*: Address
provider*: ?Provider
2022-07-12 18:23:43 +00:00
proc new*(_: type Wallet, pk: string, provider: Provider): Wallet =
2022-07-12 18:23:43 +00:00
result = Wallet()
result.privateKey = PrivateKey.fromHex(pk).value
result.publicKey = result.privateKey.toPublicKey()
result.address = Address.init(result.publicKey.toCanonicalAddress())
result.provider = some provider
proc new*(_: type Wallet, pk: string): Wallet =
result = Wallet()
result.privateKey = PrivateKey.fromHex(pk).value
result.publicKey = result.privateKey.toPublicKey()
result.address = Address.init(result.publicKey.toCanonicalAddress())
proc connect*(wallet: Wallet, provider: Provider) =
2022-07-12 18:23:43 +00:00
wallet.provider = some provider
proc createRandom*(_: type Wallet): Wallet =
result = Wallet()
2022-07-21 00:00:57 +00:00
result.privateKey = PrivateKey.random(getRng()[])
2022-07-12 18:23:43 +00:00
result.publicKey = result.privateKey.toPublicKey()
result.address = Address.init(result.publicKey.toCanonicalAddress())
proc createRandom*(_: type Wallet, provider: Provider): Wallet =
2022-07-12 18:23:43 +00:00
result = Wallet()
2022-07-21 00:00:57 +00:00
result.privateKey = PrivateKey.random(getRng()[])
2022-07-12 18:23:43 +00:00
result.publicKey = result.privateKey.toPublicKey()
result.address = Address.init(result.publicKey.toCanonicalAddress())
result.provider = some provider
2022-07-12 19:13:34 +00:00
method provider*(wallet: Wallet): Provider =
without provider =? wallet.provider:
2023-07-04 08:40:22 +00:00
raiseWalletError "Wallet has no provider"
provider
2022-07-12 19:13:34 +00:00
2022-07-21 00:00:57 +00:00
method getAddress(wallet: Wallet): Future[Address] {.async.} =
return wallet.address
2022-07-12 18:23:43 +00:00
2023-07-04 09:38:48 +00:00
proc signTransaction*(wallet: Wallet,
transaction: Transaction): Future[seq[byte]] {.async.} =
if sender =? transaction.sender and sender != wallet.address:
2023-07-04 08:40:22 +00:00
raiseWalletError "from address mismatch"
2023-07-04 09:38:48 +00:00
return wallet.privateKey.sign(transaction)
2022-07-12 19:21:35 +00:00
2023-07-04 09:38:48 +00:00
method sendTransaction*(wallet: Wallet, transaction: Transaction): Future[TransactionResponse] {.async.} =
let signed = await signTransaction(wallet, transaction)
return await provider(wallet).sendTransaction(signed)