Allow wallet to be instantiated with a PrivateKey

This commit is contained in:
Mark Spanbroek 2023-07-05 14:55:14 +02:00 committed by markspanbroek
parent 5ed3f15706
commit 2b181aa0f7
2 changed files with 14 additions and 11 deletions

View File

@ -21,17 +21,18 @@ type Wallet* = ref object of Signer
address*: Address address*: Address
provider*: ?Provider provider*: ?Provider
proc new*(_: type Wallet, pk: string, provider: Provider): Wallet = proc new*(_: type Wallet, privateKey: PrivateKey): Wallet =
result = Wallet() let publicKey = privateKey.toPublicKey()
result.privateKey = PrivateKey.fromHex(pk).value let address = Address.init(publicKey.toCanonicalAddress())
result.publicKey = result.privateKey.toPublicKey() Wallet(privateKey: privateKey, publicKey: publicKey, address: address)
result.address = Address.init(result.publicKey.toCanonicalAddress()) proc new*(_: type Wallet, privateKey: PrivateKey, provider: Provider): Wallet =
result.provider = some provider let wallet = Wallet.new(privateKey)
proc new*(_: type Wallet, pk: string): Wallet = wallet.provider = some provider
result = Wallet() wallet
result.privateKey = PrivateKey.fromHex(pk).value proc new*(_: type Wallet, privateKey: string): Wallet =
result.publicKey = result.privateKey.toPublicKey() Wallet.new(PrivateKey.fromHex(privateKey).value)
result.address = Address.init(result.publicKey.toCanonicalAddress()) proc new*(_: type Wallet, privateKey: string, provider: Provider): Wallet =
Wallet.new(PrivateKey.fromHex(privateKey).value, provider)
proc connect*(wallet: Wallet, provider: Provider) = proc connect*(wallet: Wallet, provider: Provider) =
wallet.provider = some provider wallet.provider = some provider
proc createRandom*(_: type Wallet): Wallet = proc createRandom*(_: type Wallet): Wallet =

View File

@ -27,6 +27,7 @@ suite "Wallet":
test "Can create Wallet with private key": test "Can create Wallet with private key":
discard Wallet.new(pk1) discard Wallet.new(pk1)
discard Wallet.new(PrivateKey.fromHex(pk1).value)
test "Private key can start with 0x": test "Private key can start with 0x":
discard Wallet.new("0x" & pk1) discard Wallet.new("0x" & pk1)
@ -34,6 +35,7 @@ suite "Wallet":
test "Can create Wallet with provider": test "Can create Wallet with provider":
let provider = JsonRpcProvider.new() let provider = JsonRpcProvider.new()
discard Wallet.new(pk1, provider) discard Wallet.new(pk1, provider)
discard Wallet.new(PrivateKey.fromHex(pk1).value)
test "Can connect Wallet to provider": test "Can connect Wallet to provider":
let wallet = Wallet.new(pk1) let wallet = Wallet.new(pk1)