mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-07 16:13:09 +00:00
Wallet: open ledger channel
This commit is contained in:
parent
d1c580eb91
commit
292a7ec2ac
@ -10,7 +10,7 @@ export keys
|
|||||||
type
|
type
|
||||||
Wallet* = object
|
Wallet* = object
|
||||||
key: PrivateKey
|
key: PrivateKey
|
||||||
channels: seq[Channel]
|
channels*: seq[Channel]
|
||||||
Channel* = object
|
Channel* = object
|
||||||
latest*, upcoming*: ?ChannelUpdate
|
latest*, upcoming*: ?ChannelUpdate
|
||||||
ChannelUpdate* = object
|
ChannelUpdate* = object
|
||||||
@ -23,13 +23,25 @@ proc init*(_: type Wallet, key: PrivateKey): Wallet =
|
|||||||
proc address*(wallet: Wallet): EthAddress =
|
proc address*(wallet: Wallet): EthAddress =
|
||||||
wallet.key.toPublicKey.toAddress
|
wallet.key.toPublicKey.toAddress
|
||||||
|
|
||||||
proc openLedger*(wallet: Wallet, asset: EthAddress, amount: UInt256): Channel =
|
proc openLedgerChannel*(wallet: var Wallet,
|
||||||
let me = wallet.address.toDestination
|
hub: EthAddress,
|
||||||
let outcome = Outcome.init(asset, {me: amount})
|
chainId: UInt256,
|
||||||
let state = State(outcome: outcome)
|
nonce: UInt48,
|
||||||
let signature = wallet.key.sign(state)
|
asset: EthAddress,
|
||||||
let update = ChannelUpdate(
|
amount: UInt256): Channel =
|
||||||
state: state,
|
let state = State(
|
||||||
signatures: @{wallet.address: signature}
|
channel: ChannelDefinition(
|
||||||
|
chainId: chainId,
|
||||||
|
participants: @[wallet.address, hub],
|
||||||
|
nonce: nonce
|
||||||
|
),
|
||||||
|
outcome: Outcome.init(asset, {wallet.address.toDestination: amount})
|
||||||
)
|
)
|
||||||
Channel(upcoming: update.some)
|
let channel = Channel(
|
||||||
|
upcoming: ChannelUpdate(
|
||||||
|
state: state,
|
||||||
|
signatures: @{wallet.address: wallet.key.sign(state)}
|
||||||
|
).some
|
||||||
|
)
|
||||||
|
wallet.channels.add(channel)
|
||||||
|
channel
|
||||||
|
|||||||
@ -1,23 +1,52 @@
|
|||||||
import ./basics
|
import ./basics
|
||||||
|
|
||||||
suite "nitro wallet":
|
suite "wallet":
|
||||||
|
|
||||||
|
test "wallet can be created from private key":
|
||||||
|
let key = PrivateKey.random()
|
||||||
|
let wallet = Wallet.init(key)
|
||||||
|
check wallet.address == key.toPublicKey.toAddress
|
||||||
|
|
||||||
|
suite "wallet: opening ledger channel":
|
||||||
|
|
||||||
let key = PrivateKey.random()
|
let key = PrivateKey.random()
|
||||||
let asset = EthAddress.example
|
let asset = EthAddress.example
|
||||||
let amount = 42.u256
|
let amount = 42.u256
|
||||||
|
let hub = EthAddress.example
|
||||||
|
let chainId = UInt256.example
|
||||||
|
let nonce = UInt48.example
|
||||||
|
|
||||||
test "wallet can be created from private key":
|
var wallet: Wallet
|
||||||
let wallet = Wallet.init(key)
|
var channel: Channel
|
||||||
check wallet.address == key.toPublicKey.toAddress
|
|
||||||
|
|
||||||
test "opens ledger channel":
|
setup:
|
||||||
let wallet = Wallet.init(key)
|
wallet = Wallet.init(key)
|
||||||
let me = wallet.address.toDestination
|
channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount)
|
||||||
let channel = wallet.openLedger(asset, amount)
|
|
||||||
let expectedOutcome = Outcome.init(asset, {me: amount})
|
test "creates a new upcoming state":
|
||||||
let expectedState = State(outcome: expectedOutcome)
|
|
||||||
let expectedSignatures = @{wallet.address: key.sign(expectedState)}
|
|
||||||
check channel.latest.isNone
|
check channel.latest.isNone
|
||||||
check channel.upcoming?.state == expectedState.some
|
check channel.upcoming.isSome
|
||||||
check channel.upcoming?.signatures == expectedSignatures.some
|
|
||||||
|
test "sets correct channel definition":
|
||||||
|
let definition = channel.upcoming?.state?.channel
|
||||||
|
check definition?.chainId == chainId.some
|
||||||
|
check definition?.nonce == nonce.some
|
||||||
|
check definition?.participants == @[wallet.address, hub].some
|
||||||
|
|
||||||
|
test "provides correct outcome":
|
||||||
|
let outcome = channel.upcoming?.state?.outcome
|
||||||
|
let destination = wallet.address.toDestination
|
||||||
|
check outcome == Outcome.init(asset, {destination: amount}).some
|
||||||
|
|
||||||
|
test "signs the upcoming state":
|
||||||
|
let state = channel.upcoming?.state
|
||||||
|
let signatures = channel.upcoming?.signatures
|
||||||
|
check signatures == @{wallet.address: key.sign(state.get)}.some
|
||||||
|
|
||||||
|
test "sets app definition and app data to zero":
|
||||||
|
check channel.upcoming?.state?.appDefinition == EthAddress.zero.some
|
||||||
|
check channel.upcoming?.state?.appData?.len == 0.some
|
||||||
|
|
||||||
|
test "updates the list of channels":
|
||||||
|
check wallet.channels == @[channel]
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user