mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-06 23:53:11 +00:00
Rename: PrivateKey -> EthPrivateKey, PublicKey -> EthPublicKey
This commit is contained in:
parent
b05d4e2e72
commit
f7d06a995b
@ -8,22 +8,22 @@ export toPublicKey
|
||||
push: {.upraises:[].}
|
||||
|
||||
type
|
||||
PrivateKey* = SkSecretKey
|
||||
PublicKey* = SkPublicKey
|
||||
EthPrivateKey* = SkSecretKey
|
||||
EthPublicKey* = SkPublicKey
|
||||
|
||||
proc rng(data: var openArray[byte]): bool =
|
||||
randomBytes(data) == data.len
|
||||
|
||||
proc random*(_: type PrivateKey): PrivateKey =
|
||||
PrivateKey.random(rng).get()
|
||||
proc random*(_: type EthPrivateKey): EthPrivateKey =
|
||||
EthPrivateKey.random(rng).get()
|
||||
|
||||
func `$`*(key: PrivateKey): string =
|
||||
func `$`*(key: EthPrivateKey): string =
|
||||
key.toHex()
|
||||
|
||||
func parse*(_: type PrivateKey, s: string): ?PrivateKey =
|
||||
func parse*(_: type EthPrivateKey, s: string): ?EthPrivateKey =
|
||||
SkSecretKey.fromHex(s).option
|
||||
|
||||
func toAddress*(key: PublicKey): EthAddress =
|
||||
func toAddress*(key: EthPublicKey): EthAddress =
|
||||
let hash = keccak256.digest(key.toRaw())
|
||||
var bytes: array[20, byte]
|
||||
for i in 0..<20:
|
||||
|
||||
@ -20,14 +20,14 @@ func hashMessage(message: openArray[byte]): array[32, byte] =
|
||||
data.add(message)
|
||||
keccak256.digest(data).data
|
||||
|
||||
func sign(key: PrivateKey, hash: array[32, byte]): Signature =
|
||||
func sign(key: EthPrivateKey, hash: array[32, byte]): Signature =
|
||||
key.signRecoverable(SkMessage(hash))
|
||||
|
||||
func sign*(key: PrivateKey, state: State): Signature =
|
||||
func sign*(key: EthPrivateKey, state: State): Signature =
|
||||
let hash = hashMessage(hashState(state))
|
||||
key.sign(hash)
|
||||
|
||||
func recover(signature: Signature, hash: array[32, byte]): ?PublicKey =
|
||||
func recover(signature: Signature, hash: array[32, byte]): ?EthPublicKey =
|
||||
recover(signature, SkMessage(hash)).option
|
||||
|
||||
func recover*(signature: Signature, state: State): ?EthAddress =
|
||||
|
||||
@ -15,17 +15,17 @@ export balances
|
||||
|
||||
type
|
||||
Wallet* = object
|
||||
key: PrivateKey
|
||||
key: EthPrivateKey
|
||||
channels: Table[ChannelId, SignedState]
|
||||
ChannelId* = Destination
|
||||
Payment* = tuple
|
||||
destination: Destination
|
||||
amount: UInt256
|
||||
|
||||
func init*(_: type Wallet, key: PrivateKey): Wallet =
|
||||
func init*(_: type Wallet, key: EthPrivateKey): Wallet =
|
||||
result.key = key
|
||||
|
||||
func publicKey*(wallet: Wallet): PublicKey =
|
||||
func publicKey*(wallet: Wallet): EthPublicKey =
|
||||
wallet.key.toPublicKey
|
||||
|
||||
func address*(wallet: Wallet): EthAddress =
|
||||
|
||||
@ -80,12 +80,12 @@ proc example*(_: type State): State =
|
||||
)
|
||||
|
||||
proc example*(_: type Signature): Signature =
|
||||
let key = PrivateKey.random
|
||||
let key = EthPrivateKey.random
|
||||
let state = State.example
|
||||
key.sign(state)
|
||||
|
||||
proc example*(_: type SignedState): SignedState =
|
||||
let state = State.example
|
||||
let key = PrivateKey.random
|
||||
let key = EthPrivateKey.random
|
||||
let signature = key.sign(state)
|
||||
SignedState(state: state, signatures: @[signature])
|
||||
|
||||
@ -7,7 +7,7 @@ suite "signature":
|
||||
|
||||
test "signs state hashes":
|
||||
let state = State.example
|
||||
let privateKey = PrivateKey.random()
|
||||
let privateKey = EthPrivateKey.random()
|
||||
let publicKey = privateKey.toPublicKey()
|
||||
|
||||
let signature = privateKey.sign(state)
|
||||
@ -19,7 +19,7 @@ suite "signature":
|
||||
|
||||
test "recovers ethereum address from signature":
|
||||
let state = State.example
|
||||
let key = PrivateKey.random()
|
||||
let key = EthPrivateKey.random()
|
||||
let address = key.toPublicKey.toAddress
|
||||
let signature = key.sign(state)
|
||||
check recover(signature, state) == address.some
|
||||
@ -27,7 +27,7 @@ suite "signature":
|
||||
|
||||
test "verifies signatures":
|
||||
let state = State.example
|
||||
let key = PrivateKey.random()
|
||||
let key = EthPrivateKey.random()
|
||||
let address = key.toPublicKey.toAddress
|
||||
let signature = key.sign(state)
|
||||
check verify(signature, state, address)
|
||||
@ -50,7 +50,7 @@ suite "signature":
|
||||
appDefinition: EthAddress.default,
|
||||
challengeDuration: 5
|
||||
)
|
||||
let seckey = PrivateKey.parse(
|
||||
let seckey = EthPrivateKey.parse(
|
||||
"41b0f5f91967dded8af487277874f95116094cc6004ac2b2169b5b6a87608f3e"
|
||||
).get()
|
||||
let expected = Signature.parse(
|
||||
|
||||
@ -3,7 +3,7 @@ import ./basics
|
||||
suite "wallet":
|
||||
|
||||
test "wallet is created from private key":
|
||||
let key = PrivateKey.random()
|
||||
let key = EthPrivateKey.random()
|
||||
let wallet = Wallet.init(key)
|
||||
check wallet.publicKey == key.toPublicKey
|
||||
check wallet.address == key.toPublicKey.toAddress
|
||||
@ -11,7 +11,7 @@ suite "wallet":
|
||||
|
||||
suite "wallet: opening ledger channel":
|
||||
|
||||
let key = PrivateKey.random()
|
||||
let key = EthPrivateKey.random()
|
||||
let asset = EthAddress.example
|
||||
let amount = 42.u256
|
||||
let hub = EthAddress.example
|
||||
@ -49,7 +49,7 @@ suite "wallet: opening ledger channel":
|
||||
|
||||
suite "wallet: accepting incoming channel":
|
||||
|
||||
let key = PrivateKey.random()
|
||||
let key = EthPrivateKey.random()
|
||||
var wallet: Wallet
|
||||
var signed: SignedState
|
||||
|
||||
@ -82,7 +82,7 @@ suite "wallet: accepting incoming channel":
|
||||
|
||||
suite "wallet: making payments":
|
||||
|
||||
let key = PrivateKey.random()
|
||||
let key = EthPrivateKey.random()
|
||||
let asset = EthAddress.example
|
||||
let hub = EthAddress.example
|
||||
let chainId = UInt256.example
|
||||
@ -144,7 +144,7 @@ suite "wallet: making payments":
|
||||
|
||||
suite "wallet: accepting payments":
|
||||
|
||||
let payerKey, receiverKey = PrivateKey.random()
|
||||
let payerKey, receiverKey = EthPrivateKey.random()
|
||||
let asset = EthAddress.example
|
||||
let chainId = UInt256.example
|
||||
let nonce = UInt48.example
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user