mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-07 16:13:09 +00:00
Add wallet
This commit is contained in:
parent
9b580395d8
commit
5837a655e0
@ -1,3 +1,5 @@
|
|||||||
import ./nitro/protocol
|
import ./nitro/protocol
|
||||||
|
import ./nitro/wallet
|
||||||
|
|
||||||
export protocol
|
export protocol
|
||||||
|
export wallet
|
||||||
|
|||||||
32
nitro/wallet.nim
Normal file
32
nitro/wallet.nim
Normal 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)
|
||||||
22
tests/nitro/testWallet.nim
Normal file
22
tests/nitro/testWallet.nim
Normal 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
|
||||||
@ -3,5 +3,6 @@ import ./nitro/protocol/testChannel
|
|||||||
import ./nitro/protocol/testOutcome
|
import ./nitro/protocol/testOutcome
|
||||||
import ./nitro/protocol/testState
|
import ./nitro/protocol/testState
|
||||||
import ./nitro/protocol/testSignature
|
import ./nitro/protocol/testSignature
|
||||||
|
import ./nitro/testWallet
|
||||||
|
|
||||||
{.warning[UnusedImport]: off.}
|
{.warning[UnusedImport]: off.}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user