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)
|
||||
wallet.provider = some provider
|
||||
wallet
|
||||
proc new*(_: type Wallet, privateKey: string): ?Wallet =
|
||||
if key =? PrivateKey.fromHex(privateKey):
|
||||
some Wallet.new(key)
|
||||
else:
|
||||
none Wallet
|
||||
proc new*(_: type Wallet, privateKey: string, provider: Provider): ?Wallet =
|
||||
if key =? PrivateKey.fromHex(privateKey):
|
||||
some Wallet.new(key, provider)
|
||||
else:
|
||||
none Wallet
|
||||
proc new*(_: type Wallet, privateKey: string): ?!Wallet =
|
||||
let keyResult = PrivateKey.fromHex(privateKey)
|
||||
if keyResult.isErr:
|
||||
return failure newException(WalletError, "invalid key: " & $keyResult.error)
|
||||
success Wallet.new(keyResult.get())
|
||||
proc new*(_: type Wallet, privateKey: string, provider: Provider): ?!Wallet =
|
||||
let wallet = ? Wallet.new(privateKey)
|
||||
wallet.provider = some provider
|
||||
success wallet
|
||||
proc connect*(wallet: Wallet, provider: Provider) =
|
||||
wallet.provider = some provider
|
||||
proc createRandom*(_: type Wallet): Wallet =
|
||||
|
|
|
@ -21,20 +21,20 @@ suite "Wallet":
|
|||
await provider.close()
|
||||
|
||||
test "Can create Wallet with private key":
|
||||
check isSome Wallet.new(pk1)
|
||||
check isSuccess Wallet.new(pk1)
|
||||
discard Wallet.new(PrivateKey.fromHex(pk1).get)
|
||||
|
||||
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":
|
||||
let provider = JsonRpcProvider.new()
|
||||
check isSome Wallet.new(pk1, provider)
|
||||
check isSuccess Wallet.new(pk1, provider)
|
||||
discard Wallet.new(PrivateKey.fromHex(pk1).get, provider)
|
||||
|
||||
test "Cannot create wallet with invalid key string":
|
||||
check isNone Wallet.new("0xInvalidKey")
|
||||
check isNone Wallet.new("0xInvalidKey", JsonRpcProvider.new())
|
||||
check isFailure Wallet.new("0xInvalidKey")
|
||||
check isFailure Wallet.new("0xInvalidKey", JsonRpcProvider.new())
|
||||
|
||||
test "Can connect Wallet to provider":
|
||||
let wallet = !Wallet.new(pk1)
|
||||
|
|
Loading…
Reference in New Issue