1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
+
+
-
-
+
+
|
#! /usr/bin/env tclsh
package require json
package require json::write
namespace eval ::nano {}
namespace eval ::nano::address {}
namespace eval ::nano::key {}
namespace eval ::nano::block {}
namespace eval ::nano::block::account {}
namespace eval ::nano::address {}
namespace eval ::nano::block::create {}
namespace eval ::nano::account {}
set ::nano::address::base32alphabet {13456789abcdefghijkmnopqrstuwxyz}
proc ::nano::address::toPublicKey {address args} {
set performChecksumCheck false
set outputFormat "bytes"
foreach arg $args {
switch -exact -- $arg {
|
︙ | | |
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
append address $byte
}
set address [string reverse $address]
set address "${addressPrefix}${address}"
return $address
}
proc ::nano::address::fromPrivateKey {key args} {
set pubKey [::nano::key::publicKeyFromPrivateKey $key]
tailcall ::nano::address::fromPublicKey $pubKey {*}$args
}
proc ::nano::key::generateNewSeed {} {
tailcall ::nano::internal::generateSeed
}
proc ::nano::key::generateNewKey {} {
tailcall ::nano::internal::generateKey
}
proc ::nano::key::computeKey {seed args} {
set index 0
set outputFormat "bytes"
if {[llength $args] > 0} {
if {[string index [lindex $args 0] 0] ne "-"} {
set index [lindex $args 0]
set args [lrange $args 1 end]
}
foreach arg $args {
switch -exact -- $arg {
"-hex" {
set outputFormat "hex"
}
"-binary" {
set outputFormat "bytes"
}
default {
return -code error "Invalid option: $arg"
}
}
}
}
if {[string length $seed] != 32} {
set seed [binary decode hex $seed]
}
set key [::nano::internal::generateKey $seed $index]
if {$outputFormat eq "hex"} {
set key [binary encode hex $key]
}
return $key
}
proc ::nano::key::publicKeyFromPrivateKey {key args} {
set outputFormat "bytes"
foreach arg $args {
switch -- $arg {
"-hex" {
set outputFormat "hex"
}
"-binary" {
set outputFormat "bytes"
}
default {
return -code error "Invalid option: $arg"
}
}
}
if {[string length $key] != 32} {
set key [binary decode hex $key]
}
set pubKey [::nano::internal::publicKey $key]
if {$outputFormat eq "hex"} {
set pubKey [string toupper [binary encode hex $pubKey]]
}
return $pubKey
}
proc ::nano::block::fromJSON {json} {
array set block [json::json2dict $json]
switch -- $block(type) {
"state" {
# XXX:TODO: Find the source of this
|
︙ | | |
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
-
+
-
+
+
+
+
-
|
return -code error "Invalid block type $block(type)"
}
}
return $blockData
}
proc ::nano::block::_dictToJSON {blockDict {addArgs_fromPublicKey ""}} {
proc ::nano::block::_dictToJSON {blockDict} {
array set block $blockDict
if {[info exists block(signKey)] && ([info exists block(_blockData)] || [info exists block(_blockHash)])} {
if {![info exists block(_blockHash)]} {
set block(_blockHash) [binary encode hex [::nano::block::hash $block(_blockData)]]
}
set signKey [binary decode hex $block(signKey)]
set blockHash [binary decode hex $block(_blockHash)]
set signature [::nano::internal::signDetached $blockHash $signKey]
set signature [binary encode hex $signature]
set signature [string toupper $signature]
set block(signature) $signature
}
if {$block(type) eq "state"} {
if {![info exists block(link)]} {
set block(link) [::nano::address::toPublicKey $block(link_as_account) -hex]
}
if {![info exists block(link_as_address)]} {
set addressFormatFlag "-nano"
foreach field {account destination representative} {
if {![info exists block($field)]} {
continue
}
if {[string match "nano_*" $block($field)]} {
set addressFormatFlag "-nano"
} else {
set addressFormatFlag "-xrb"
}
break
}
set block(link_as_account) [::nano::address::fromPublicKey $block(link) $addressFormatFlag]
}
}
set blockJSONFields {
set blockJSONFields {type account source destination previous representative balance link link_as_account _blockHash}
type account source destination previous representative balance
link link_as_account _blockHash _workHash signature
}
set blockJSONEntries [lmap field $blockJSONFields {
if {![info exists block($field)]} {
continue
}
switch -exact -- $field {
"source" - "previous" - "link" - "_blockHash" {
"source" - "previous" - "link" - "_blockHash" - "_workHash" {
set block($field) [string toupper $block($field)]
}
}
return -level 0 [list $field [json::write string $block($field)]]
}]
set blockJSONEntries [join $blockJSONEntries]
set blockJSON [json::write object {*}$blockJSONEntries]
return $blockJSON
}
proc ::nano::block::toJSON {blockData args} {
proc ::nano::block::toDict {blockData args} {
set block(type) ""
set addressPrefix "nano_"
foreach arg $args {
switch -glob -- $arg {
"-type=*" {
set block(type) [lindex [split $arg =] 1]
}
"-signKey=*" {
set block(signKey) [string range $arg 9 end]
}
"-xrb" {
set addressPrefix "xrb_"
}
"-nano" {
set addressPrefix "nano_"
}
default {
return -code error "Invalid option: $arg"
}
}
}
if {$block(type) eq ""} {
switch -- [string length $blockData] {
176 { set block(type) state }
|
︙ | | |
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
+
+
+
+
+
+
+
+
-
+
|
}
"balance" {
set block($field) [format %lli "0x$block($field)"]
}
}
}
set block(_blockData) $blockData
return [array get block]
}
proc ::nano::block::toJSON {blockData args} {
set blockDict [::nano::block::toDict $blockData {*}$args]
set blockJSON [_dictToJSON [array get block] ${addArgs_fromPublicKey}]
set blockJSON [_dictToJSON $blockDict]
return $blockJSON
}
proc ::nano::block::hash {blockData args} {
set outputFormat "bytes"
foreach arg $args {
|
︙ | | |
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
-
+
-
-
+
+
+
+
+
+
+
+
-
+
-
+
+
+
+
+
-
+
+
+
+
+
+
-
+
+
+
+
+
-
+
+
+
+
+
-
+
+
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
if {$outputFormat eq "hex"} {
set hash [string toupper [binary encode hex $hash]]
}
return $hash
}
proc ::nano::block::account::_finalizeBlock {blockDict} {
proc ::nano::block::jsonFromDict {blockDict} {
set blockJSON [::nano::block::_dictToJSON $blockDict]
set block [::nano::block::fromJSON $blockJSON]
set blockHash [::nano::block::hash $block -hex]
dict set blockDict "_blockHash" $blockHash
set blockHash [::nano::block::hash $block]
dict set blockDict "_blockHash" [binary encode hex $blockHash]
set blockJSON [::nano::block::_dictToJSON $blockDict]
return $blockJSON
}
# send from <account> to <account> previousBalance <balance>
# amount <amount> sourceBlock <sourceBlockHash>
# previous <previousBlockHash> ?representative <representative>?
proc ::nano::block::account::send {args} {
proc ::nano::block::create::send {args} {
array set block $args
if {![info exists block(representative)]} {
set block(representative) $block(from)
}
set block(balance) [expr {$block(priorBalance) - $block(amount)}]
set block(balance) [expr {$block(previousBalance) - $block(amount)}]
set blockDict [dict create \
"type" state \
"account" $block(from) \
"previous" $block(previous) \
"representative" $block(representative) \
"balance" $block(balance) \
"link_as_account" $block(to) \
"_workHash" $block(previous) \
]
if {[info exists block(signKey)]} {
dict set blockDict signKey $block(signKey)
}
tailcall _finalizeBlock $blockDict
tailcall ::nano::block::jsonFromDict $blockDict
}
# Usage:
# receive to <account> previousBalance <balance> amount <amount>
# sourceBlock <sourceBlockHash> ?previous <previousBlockHash>?
# ?representative <representative>?
proc ::nano::block::account::receive {args} {
proc ::nano::block::create::receive {args} {
array set block $args
if {![info exists block(representative)]} {
set block(representative) $block(to)
}
if {![info exists block(previous)]} {
set block(previous) "0000000000000000000000000000000000000000000000000000000000000000"
set block(previousBalance) 0
set block(_workHash) [::nano::address::toPublicKey $block(to) -hex]
} else {
set block(_workHash) $block(previous)
}
set block(balance) [expr {$block(priorBalance) + $block(amount)}]
set block(balance) [expr {$block(previousBalance) + $block(amount)}]
set blockDict [dict create \
"type" state \
"account" $block(to) \
"previous" $block(previous) \
"representative" $block(representative) \
"balance" $block(balance) \
"link" $block(sourceBlock) \
"_workHash" $block(_workHash) \
]
if {[info exists block(signKey)]} {
dict set blockDict signKey $block(signKey)
}
tailcall _finalizeBlock $blockDict
tailcall ::nano::block::jsonFromDict $blockDict
}
# Usage:
# setRepresentative account <account> previous <previousBlockHash>
# representative <newRepresentative> balance <balance>
proc ::nano::block::account::setRepresentative {args} {
proc ::nano::block::create::setRepresentative {args} {
array set block $args
set block(balance) $block(priorBalance)
set block(link) "0000000000000000000000000000000000000000000000000000000000000000"
set blockDict [dict create \
"type" state \
"account" $block(account) \
"previous" $block(previous) \
"representative" $block(representative) \
"balance" $block(balance) \
"link" $block(link) \
"_workHash" $block(previous) \
]
if {[info exists block(signKey)]} {
dict set blockDict signKey $block(signKey)
}
tailcall ::nano::block::jsonFromDict $blockDict
}
# -- Tracked accounts --
proc ::nano::account::setFrontier {account frontierHash balance representative} {
set accountPubKey [::nano::address::toPublicKey $account -hex]
set ::nano::account::frontiers($accountPubKey) [dict create \
frontierHash $frontierHash balance $balance representative $representative \
]
}
proc ::nano::account::getFrontier {account args} {
set accountPubKey [::nano::address::toPublicKey $account -hex]
if {![info exists ::nano::account::frontiers($accountPubKey)]} {
set frontier [dict create balance 0]
} else {
set frontier $::nano::account::frontiers($accountPubKey)
}
return [dict get $frontier {*}$args]
}
proc ::nano::account::addPending {account blockHash amount} {
set accountPubKey [::nano::address::toPublicKey $account -hex]
set ::nano::account::pending([list $accountPubKey $blockHash]) [dict create amount $amount]
}
proc ::nano::account::receive {account blockHash signKey} {
set accountPubKey [::nano::address::toPublicKey $account -hex]
set frontierInfo [getFrontier $account]
dict with frontierInfo {}
set blockInfo $::nano::account::pending([list $accountPubKey $blockHash])
unset ::nano::account::pending([list $accountPubKey $blockHash])
set amount [dict get $blockInfo amount]
set blockArgs [list to $account previousBalance $balance \
tailcall _finalizeBlock $blockDict
}
amount $amount sourceBlock $blockHash \
signKey $signKey representative $representative]
if {[info exists frontierHash]} {
lappend blockArgs previous $frontierHash
}
set block [::nano::block::create::receive {*}$blockArgs]
set newFrontierHash [dict get [json::json2dict $block] "_blockHash"]
set balance [expr {$balance + $amount}]
setFrontier $account $newFrontierHash $balance $representative
return $block
}
proc ::nano::account::send {fromAccount toAccount amount signKey} {
set fromAccountPubKey [::nano::address::toPublicKey $fromAccount -hex]
set toAccountPubKey [::nano::address::toPublicKey $fromAccount -hex]
set fromFrontierInfo [getFrontier $fromAccount]
set toFrontierInfo [getFrontier $toAccount]
set fromBalance [dict get $fromFrontierInfo balance]
set fromFrontierHash [dict get $fromFrontierInfo frontierHash]
set fromRepresentative [dict get $fromFrontierInfo representative]
set signKey [binary encode hex $signKey]
set block [::nano::block::create::send \
from $fromAccount \
to $toAccount \
previous $fromFrontierHash \
previousBalance $fromBalance \
amount $amount \
signKey $signKey
]
set newBalance [expr {$fromBalance - $amount}]
set newFrontierHash [dict get [json::json2dict $block] "_blockHash"]
setFrontier $fromAccount $newFrontierHash $newBalance $fromRepresentative
addPending $toAccount $newFrontierHash $amount
return $block
}
proc ::nano::account::receiveAllPending {key} {
set outBlocks [list]
set accountPubKey [::nano::key::publicKeyFromPrivateKey $key -hex]
set signKey [binary encode hex $key]
set account [::nano::address::fromPublicKey $accountPubKey]
foreach accountPubKeyBlockHash [array names ::nano::account::pending [list $accountPubKey *]] {
set blockHash [lindex $accountPubKeyBlockHash 1]
lappend outBlocks [receive $account $blockHash $signKey]
}
return $outBlocks
}
package provide nano 0
|