Use Result to return error when wallet creation fails
Co-authored-by: Eric Mastro <eric.mastro@gmail.com>
This commit is contained in:
parent
43041e7948
commit
15ed76ebed
|
@ -30,16 +30,15 @@ proc new*(_: type Wallet, privateKey: PrivateKey, provider: Provider): Wallet =
|
||||||
let wallet = Wallet.new(privateKey)
|
let wallet = Wallet.new(privateKey)
|
||||||
wallet.provider = some provider
|
wallet.provider = some provider
|
||||||
wallet
|
wallet
|
||||||
proc new*(_: type Wallet, privateKey: string): ?Wallet =
|
proc new*(_: type Wallet, privateKey: string): ?!Wallet =
|
||||||
if key =? PrivateKey.fromHex(privateKey):
|
let keyResult = PrivateKey.fromHex(privateKey)
|
||||||
some Wallet.new(key)
|
if keyResult.isErr:
|
||||||
else:
|
return failure newException(WalletError, "invalid key: " & $keyResult.error)
|
||||||
none Wallet
|
success Wallet.new(keyResult.get())
|
||||||
proc new*(_: type Wallet, privateKey: string, provider: Provider): ?Wallet =
|
proc new*(_: type Wallet, privateKey: string, provider: Provider): ?!Wallet =
|
||||||
if key =? PrivateKey.fromHex(privateKey):
|
let wallet = ? Wallet.new(privateKey)
|
||||||
some Wallet.new(key, provider)
|
wallet.provider = some provider
|
||||||
else:
|
success wallet
|
||||||
none Wallet
|
|
||||||
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 =
|
||||||
|
|
|
@ -21,20 +21,20 @@ suite "Wallet":
|
||||||
await provider.close()
|
await provider.close()
|
||||||
|
|
||||||
test "Can create Wallet with private key":
|
test "Can create Wallet with private key":
|
||||||
check isSome Wallet.new(pk1)
|
check isSuccess Wallet.new(pk1)
|
||||||
discard Wallet.new(PrivateKey.fromHex(pk1).get)
|
discard Wallet.new(PrivateKey.fromHex(pk1).get)
|
||||||
|
|
||||||
test "Private key can start with 0x":
|
test "Private key can start with 0x":
|
||||||
check isSome Wallet.new("0x" & pk1)
|
check isSuccess Wallet.new("0x" & pk1)
|
||||||
|
|
||||||
test "Can create Wallet with provider":
|
test "Can create Wallet with provider":
|
||||||
let provider = JsonRpcProvider.new()
|
let provider = JsonRpcProvider.new()
|
||||||
check isSome Wallet.new(pk1, provider)
|
check isSuccess Wallet.new(pk1, provider)
|
||||||
discard Wallet.new(PrivateKey.fromHex(pk1).get, provider)
|
discard Wallet.new(PrivateKey.fromHex(pk1).get, provider)
|
||||||
|
|
||||||
test "Cannot create wallet with invalid key string":
|
test "Cannot create wallet with invalid key string":
|
||||||
check isNone Wallet.new("0xInvalidKey")
|
check isFailure Wallet.new("0xInvalidKey")
|
||||||
check isNone Wallet.new("0xInvalidKey", JsonRpcProvider.new())
|
check isFailure Wallet.new("0xInvalidKey", JsonRpcProvider.new())
|
||||||
|
|
||||||
test "Can connect Wallet to provider":
|
test "Can connect Wallet to provider":
|
||||||
let wallet = !Wallet.new(pk1)
|
let wallet = !Wallet.new(pk1)
|
||||||
|
|
Loading…
Reference in New Issue