Add wallet

This commit is contained in:
Mark Spanbroek 2021-03-09 16:53:15 +01:00
parent 9b580395d8
commit 5837a655e0
4 changed files with 57 additions and 0 deletions

View File

@ -1,3 +1,5 @@
import ./nitro/protocol
import ./nitro/wallet
export protocol
export wallet

32
nitro/wallet.nim Normal file
View File

@ -0,0 +1,32 @@
import ./basics
import ./keys
import ./protocol
include questionable/errorban
export basics
export keys
type
Wallet* = object
key: PrivateKey
channels: seq[Channel]
Channel* = object
latest*, upcoming*: ?StateUpdate
StateUpdate* = object
state*: State
signatures*: seq[(EthAddress, Signature)]
proc init*(_: type Wallet, key: PrivateKey): Wallet =
result.key = key
proc address*(wallet: Wallet): EthAddress =
wallet.key.toPublicKey.toAddress
proc openLedger*(wallet: Wallet, asset: EthAddress, amount: UInt256): Channel =
let me = wallet.address.toDestination
let outcome = Outcome.init(asset, {me: amount})
let state = State(outcome: outcome)
let signature = wallet.key.sign(state)
let update = StateUpdate(state: state, signatures: @{wallet.address: signature})
Channel(upcoming: update.some)

View File

@ -0,0 +1,22 @@
import ./basics
suite "nitro wallet":
let key = PrivateKey.random()
let asset = EthAddress.example
let amount = 42.u256
test "wallet can be created from private key":
let wallet = Wallet.init(key)
check wallet.address == key.toPublicKey.toAddress
test "opens ledger channel":
let wallet = Wallet.init(key)
let me = wallet.address.toDestination
let channel = wallet.openLedger(asset, amount)
let expectedOutcome = Outcome.init(asset, {me: amount})
let expectedState = State(outcome: expectedOutcome)
let expectedSignatures = @{wallet.address: key.sign(expectedState)}
check channel.latest.isNone
check channel.upcoming.?state == expectedState.some
check channel.upcoming.?signatures == expectedSignatures.some

View File

@ -3,5 +3,6 @@ import ./nitro/protocol/testChannel
import ./nitro/protocol/testOutcome
import ./nitro/protocol/testState
import ./nitro/protocol/testSignature
import ./nitro/testWallet
{.warning[UnusedImport]: off.}