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::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 {
|
>
>
>
>
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#! /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::create {}
namespace eval ::nano::account {}
set ::nano::block::hashLength 32
set ::nano::key::publicKeyLength 32
set ::nano::key::privateKeyLength 32
set ::nano::key::seedLength 32
set ::nano::address::base32alphabet {13456789abcdefghijkmnopqrstuwxyz}
proc ::nano::address::toPublicKey {address args} {
set performChecksumCheck false
set outputFormat "bytes"
foreach arg $args {
switch -exact -- $arg {
|
︙ | | | ︙ | |
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
default {
return -code error "Invalid option: $arg"
}
}
}
if {![info exists inputFormat]} {
if {[string length $pubKey] == 32} {
set inputFormat "bytes"
} else {
set inputFormat "hex"
}
}
if {$inputFormat eq "hex"} {
set pubKey [binary decode hex $pubKey]
}
if {[string length $pubKey] != 32} {
return -code error "Invalid key (length)"
}
set checksum [string reverse [::nano::internal::hashData $pubKey 5]]
append pubKey $checksum
set pubKey [binary encode hex $pubKey]
|
|
|
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
default {
return -code error "Invalid option: $arg"
}
}
}
if {![info exists inputFormat]} {
if {[string length $pubKey] == $::nano::key::publicKeyLength} {
set inputFormat "bytes"
} else {
set inputFormat "hex"
}
}
if {$inputFormat eq "hex"} {
set pubKey [binary decode hex $pubKey]
}
if {[string length $pubKey] != $::nano::key::publicKeyLength} {
return -code error "Invalid key (length)"
}
set checksum [string reverse [::nano::internal::hashData $pubKey 5]]
append pubKey $checksum
set pubKey [binary encode hex $pubKey]
|
︙ | | | ︙ | |
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
}
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]
}
|
|
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
}
default {
return -code error "Invalid option: $arg"
}
}
}
}
if {[string length $seed] != $::nano::key::seedLength} {
set seed [binary decode hex $seed]
}
set key [::nano::internal::generateKey $seed $index]
if {$outputFormat eq "hex"} {
set key [binary encode hex $key]
}
|
︙ | | | ︙ | |
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
}
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]]
}
|
|
|
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
}
default {
return -code error "Invalid option: $arg"
}
}
}
if {[string length $key] != $::nano::key::privateKeyLength} {
set key [binary decode hex $key]
}
set pubKey [::nano::internal::publicKey $key]
if {$outputFormat eq "hex"} {
set pubKey [string toupper [binary encode hex $pubKey]]
}
|
︙ | | | ︙ | |
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
|
}
"-binary" {
set outputFormat "bytes"
}
}
}
if {[string length $blockHash] != 32} {
set blockHash [binary decode hex $blockHash]
}
if {[string length $key] != 32} {
set key [binary decode hex $key]
}
set signature [::nano::internal::signDetached $blockHash $key]
if {$outputFormat eq "hex"} {
set signature [string toupper [binary encode hex $signature]]
}
return $signature
}
proc ::nano::block::signBlock {blockData args} {
set blockHash [::nano::block::hash $blockData]
tailcall ::nano::block::signBlockHash $blockHash {*}$args
}
proc ::nano::block::verifyBlockHash {blockHash signature pubKey} {
if {[string length $blockHash] != 32} {
set blockHash [binary decode hex $blockHash]
}
if {[string length $pubKey] != 32} {
set key [binary decode hex $pubKey]
}
set valid [::nano::internal::verifyDetached $blockHash $signature $pubKey]
return $signature
}
|
|
|
|
|
|
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
|
}
"-binary" {
set outputFormat "bytes"
}
}
}
if {[string length $blockHash] != $::nano::block::hashLength} {
set blockHash [binary decode hex $blockHash]
}
if {[string length $key] != $::nano::key::privateKeyLength} {
set key [binary decode hex $key]
}
set signature [::nano::internal::signDetached $blockHash $key]
if {$outputFormat eq "hex"} {
set signature [string toupper [binary encode hex $signature]]
}
return $signature
}
proc ::nano::block::signBlock {blockData args} {
set blockHash [::nano::block::hash $blockData]
tailcall ::nano::block::signBlockHash $blockHash {*}$args
}
proc ::nano::block::verifyBlockHash {blockHash signature pubKey} {
if {[string length $blockHash] != $::nano::block::hashLength} {
set blockHash [binary decode hex $blockHash]
}
if {[string length $pubKey] != $::nano::key::publicKeyLength} {
set key [binary decode hex $pubKey]
}
set valid [::nano::internal::verifyDetached $blockHash $signature $pubKey]
return $signature
}
|
︙ | | | ︙ | |
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
}
default {
return -code error "Invalid option: $arg"
}
}
}
set hash [::nano::internal::hashData $blockData 32]
if {$outputFormat eq "hex"} {
set hash [string toupper [binary encode hex $hash]]
}
return $hash
}
|
|
|
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
}
default {
return -code error "Invalid option: $arg"
}
}
}
set hash [::nano::internal::hashData $blockData $::nano::block::hashLength]
if {$outputFormat eq "hex"} {
set hash [string toupper [binary encode hex $hash]]
}
return $hash
}
|
︙ | | | ︙ | |