From 1d4c612a532c170fefa4e8d8150cfa90de753974 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 17 Mar 2021 13:38:17 +0100 Subject: [PATCH] Move wallet into its own folder --- nitro/wallet.nim | 57 ++---------------------------- nitro/{ => wallet}/ledger.nim | 4 +-- nitro/{ => wallet}/signedstate.nim | 4 +-- nitro/wallet/wallet.nim | 56 +++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 59 deletions(-) rename nitro/{ => wallet}/ledger.nim (92%) rename nitro/{ => wallet}/signedstate.nim (93%) create mode 100644 nitro/wallet/wallet.nim diff --git a/nitro/wallet.nim b/nitro/wallet.nim index 27bdd60..1f63045 100644 --- a/nitro/wallet.nim +++ b/nitro/wallet.nim @@ -1,56 +1,3 @@ -import std/tables -import ./basics -import ./keys -import ./protocol -import ./signedstate -import ./ledger +import ./wallet/wallet -include questionable/errorban - -export basics -export keys -export signedstate - -type - Wallet* = object - key: PrivateKey - channels: Table[ChannelId, SignedState] - ChannelId* = Destination - -func init*(_: type Wallet, key: PrivateKey): Wallet = - result.key = key - -func address*(wallet: Wallet): EthAddress = - wallet.key.toPublicKey.toAddress - -func `[]`*(wallet: Wallet, channel: ChannelId): ?SignedState = - wallet.channels[channel].catch.option - -func sign(wallet: Wallet, state: SignedState): SignedState = - var signed = state - signed.signatures &= @{wallet.address: wallet.key.sign(state.state)} - signed - -func createChannel(wallet: var Wallet, state: SignedState): ChannelId = - let signed = wallet.sign(state) - let id = getChannelId(signed.state.channel) - wallet.channels[id] = signed - id - -func openLedgerChannel*(wallet: var Wallet, - hub: EthAddress, - chainId: UInt256, - nonce: UInt48, - asset: EthAddress, - amount: UInt256): ChannelId = - let state = startLedger(wallet.address, hub, chainId, nonce, asset, amount) - wallet.createChannel(state) - -func acceptChannel*(wallet: var Wallet, signed: SignedState): ?!ChannelId = - if not signed.hasParticipant(wallet.address): - return ChannelId.failure "wallet owner is not a participant" - - if not verifySignatures(signed): - return ChannelId.failure "incorrect signatures" - - wallet.createChannel(signed).success +export wallet diff --git a/nitro/ledger.nim b/nitro/wallet/ledger.nim similarity index 92% rename from nitro/ledger.nim rename to nitro/wallet/ledger.nim index 42fa11a..56ec976 100644 --- a/nitro/ledger.nim +++ b/nitro/wallet/ledger.nim @@ -1,6 +1,6 @@ -import ./basics +import ../basics +import ../protocol import ./signedstate -import ./protocol func startLedger*(me: EthAddress, hub: EthAddress, diff --git a/nitro/signedstate.nim b/nitro/wallet/signedstate.nim similarity index 93% rename from nitro/signedstate.nim rename to nitro/wallet/signedstate.nim index cd8e63d..5baf0ed 100644 --- a/nitro/signedstate.nim +++ b/nitro/wallet/signedstate.nim @@ -1,5 +1,5 @@ -import ./basics -import ./protocol +import ../basics +import ../protocol include questionable/errorban diff --git a/nitro/wallet/wallet.nim b/nitro/wallet/wallet.nim new file mode 100644 index 0000000..37e0b43 --- /dev/null +++ b/nitro/wallet/wallet.nim @@ -0,0 +1,56 @@ +import std/tables +import ../basics +import ../keys +import ../protocol +import ./signedstate +import ./ledger + +include questionable/errorban + +export basics +export keys +export signedstate + +type + Wallet* = object + key: PrivateKey + channels: Table[ChannelId, SignedState] + ChannelId* = Destination + +func init*(_: type Wallet, key: PrivateKey): Wallet = + result.key = key + +func address*(wallet: Wallet): EthAddress = + wallet.key.toPublicKey.toAddress + +func `[]`*(wallet: Wallet, channel: ChannelId): ?SignedState = + wallet.channels[channel].catch.option + +func sign(wallet: Wallet, state: SignedState): SignedState = + var signed = state + signed.signatures &= @{wallet.address: wallet.key.sign(state.state)} + signed + +func createChannel(wallet: var Wallet, state: SignedState): ChannelId = + let signed = wallet.sign(state) + let id = getChannelId(signed.state.channel) + wallet.channels[id] = signed + id + +func openLedgerChannel*(wallet: var Wallet, + hub: EthAddress, + chainId: UInt256, + nonce: UInt48, + asset: EthAddress, + amount: UInt256): ChannelId = + let state = startLedger(wallet.address, hub, chainId, nonce, asset, amount) + wallet.createChannel(state) + +func acceptChannel*(wallet: var Wallet, signed: SignedState): ?!ChannelId = + if not signed.hasParticipant(wallet.address): + return ChannelId.failure "wallet owner is not a participant" + + if not verifySignatures(signed): + return ChannelId.failure "incorrect signatures" + + wallet.createChannel(signed).success