Check-in [6881dd597e]
Overview
Comment:Made API more consistent
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6881dd597ed2f34dc0116f741da3086854d2eefb870c1526ab83cd4a76751c70
User & Date: rkeene on 2018-07-06 02:35:06
Other Links: manifest | tags
Context
2018-07-06
14:53
Updated work nomenclature to be more clear check-in: 3aeba5d8ef user: rkeene tags: trunk
02:35
Made API more consistent check-in: 6881dd597e user: rkeene tags: trunk
2018-07-05
17:19
Improved random number generation mechanisms check-in: e302d11ff2 user: rkeene tags: trunk
Changes

Added build/nano.txt version [698fe4d7c1].



















































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Address
::nano::address::toPublicKey address ?-hex|-binary? ?-verify|-no-verify?    ->  publicKey
::nano::address::fromPublicKey pubKey ?-xrb|-nano?                          ->  address
::nano::address::fromPrivateKey privateKey ?-xrb|-nano?                     ->  address

Key and Seed
::nano::key::newSeed ?-hex|-binary?                                         ->  seed
::nano::key::newKey ?-hex|-binary?                                          ->  privateKey
::nano::key::fromSeed seed ?index? ?-hex|-binary?                           ->  privateKey
::nano::key::publicKeyFromPrivateKey privateKey ?-hex|-binary?              ->  publicKey

Low-level Block
::nano::block::json::toBlock blockJSON                                      ->  blockData
::nano::block::json::fromDict blockDict                                     ->  blockJSON
::nano::block::json::fromBlock blockData ?-xrb|-nano? ?-type=<blockType>? ?-signKey=<privateKey>? ->  blockJSON
::nano::block::json::sign blockJSON privateKey ?-update|-signature ?-hex|binary?? ->  signature|blockJSON
::nano::block::json::verifySignature blockJSON args                         ->  boolean
::nano::block::json::work blockJSON ?-update|-work ?-hex|-binary??          ->  work|blockJSON
::nano::block::json::validateWork blockJSON                                 ->  boolean

::nano::block::dict::toBlock blockDict                                      ->  blockData
::nano::block::dict::fromJSON blockJSON                                     ->  blockDict
::nano::block::dict::fromBlock blockData ?-xrb|-nano? ?-type=<blockType>? ?-signKey=<privatKey>? ->  blockDict
::nano::block::dict::sign blockDict privateKey ?-update|-signature ?-hex|-binary?? ->  signature|blockDict
::nano::block::dict::verifySignature blockDict args                         ->  boolean
::nano::block::dict::work blockDict ?-update|-work ?-hex|-binary??          ->  work|blockDict
::nano::block::dict::validateWork blockDict                                 ->  boolean

::nano::block::hash blockData ?-hex|-binary?                                ->  blockHash

::nano::block::signBlockHash blockHash privateKey ?-hex|-binary?            ->  signature
::nano::block::sign blockData privateKey ?-hex|-binary?                     ->  signature

::nano::block::verifyBlockHash blockHash signature publicKey                ->  boolean
::nano::block::verify blockData signature publicKey                         ->  boolean

::nano::block::create::send args                                            ->  blockJSON|blockDict
::nano::block::create::receive args                                         ->  blockJSON|blockDict
::nano::block::create::setRepresentative args                               ->  blockJSON|blockDict

::nano::work::fromBlockHash blockHash                                       ->  work
::nano::work::fromBlock blockData                                           ->  work
::nano::work::validate blockHash work                                       ->  boolean

:nano::account::setFrontier account frontierHash balance representative    ->  nil
:nano::account::getFrontier account args                                   ->  frontierInfo
:nano::account::addPending account blockHash amount                        ->  nil
:nano::account::receive account blockHash privateKey                       ->  blockJSON|blockDict
:nano::account::send fromAccount toAccount amount privateKey               ->  blockJSON|blockDict
:nano::account::receiveAllPending account privateKey                       ->  ListOfBlockJSON|ListOfBlockDict

Modified build/test/test.tcl from [154ad80750] to [a74429d687].

83
84
85
86
87
88
89
90

91
92
93
94
95
96
97
83
84
85
86
87
88
89

90
91
92
93
94
95
96
97







-
+







	}

	return true
}

proc test_keygeneration {} {
	# Generate a new key pair
	set key [::nano::key::generateNewKey]
	set key [::nano::key::newKey]
	if {[string length $key] != 32} {
		puts "\[1.FAIL\] Got: [string length $key]"
		puts "\[1.FAIL\] Exp: 32"

		return false
	}

105
106
107
108
109
110
111
112

113
114
115
116
117
118
119
105
106
107
108
109
110
111

112
113
114
115
116
117
118
119







-
+







		puts "\[2.FAIL\] Exp: true"

		return false
	}

	# Create a key pair from a seed and index
	set seed [binary decode hex C4D214F19E706E9C7487CEF00DE8059200C32414F0ED82E5E33B523AEDF719BA]
	set key [::nano::key::computeKey $seed 0]
	set key [::nano::key::fromSeed $seed 0]
	set pubKey [string toupper [binary encode hex [::nano::internal::publicKey $key]]]
	set pubKey_expected "B63EC7A797F2A5858C754EC9C0537920C4F9DEA58F9F411F0C2161F6D303AA7A"
	if {$pubKey ne $pubKey_expected} {
		puts "\[3.FAIL\] Got: $pubKey"
		puts "\[3.FAIL\] Exp: $pubKey_expected"

		return false
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
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







-
+








-
+



















-
+













-
+

-

-
+
+










-
-
+
+







	}

	return true
}

proc test_blocks {} {
	set seed [binary decode hex C4D214F19E706E9C7487CEF00DE8059200C32414F0ED82E5E33B523AEDF719BA]
	set key [::nano::key::computeKey $seed 0 -hex]
	set key [::nano::key::fromSeed $seed 0 -hex]
	set address [::nano::address::fromPrivateKey $key -xrb]

	# High-level primitives
	## Receive/Open
	set block [::nano::block::create::receive \
		to $address \
		amount 1000000000000000000000000000000 \
		sourceBlock "207D3043D77B84E892AD4949D147386DE4C2FE4B2C8DC13F9469BC4A764681A7" \
		signKey $key
		signKey $key -json true
	]

	set blockDict [::json::json2dict $block]
	set blockSignature [string toupper [dict get $blockDict signature]]
	set blockSignature_expected "B574DE37F5FFF3DCFB5D0E505FC36B402444777CAA99BA86F89E9B82B6EB901B809554287F0B67D8C2A8306B4F69FE77FD0C9B3D0D10422A02CFEBB3810C7D02"
	if {$blockSignature ne $blockSignature_expected} {
		puts "\[1.FAIL\] Got: $blockSignature"
		puts "\[1.FAIL\] Exp: $blockSignature_expected"

		return false
	}

	## Send
	set block [::nano::block::create::send \
		from $address \
		to "xrb_1unc5hriitrdjq5dnyhr3zmd8t5hm7rhm9a1u3uun5ycbaacpu649yh5c4b5" \
		previous "D46BFC2E35B5A3CA4230839D67676F4A8498C2567F571D2B66A7F7B72214DEEE" \
		previousBalance 1000000000000000000000000000000 \
		amount 1000000000000000000000000000000 \
		signKey $key
		signKey $key -json true
	]

	set blockDict [::json::json2dict $block]
	set blockSignature [string toupper [dict get $blockDict signature]]
	set blockSignature_expected "BFE238A27FFBFBCF722EDC3700CA8E2405F5AE18E353E591917A2CBE393F0759C948E710DD723B3BFB21B491D9D0856EEFCAC0E25C7E5FF06185FE5D633B5204"
	if {$blockSignature ne $blockSignature_expected} {
		puts "\[2.FAIL\] Got: $blockSignature"
		puts "\[2.FAIL\] Exp: $blockSignature_expected"

		return false
	}

	# JSON Parsing a block
	set blockDict [::nano::block::toDict [::nano::block::fromJSON $block]]
	set blockDict [::nano::block::dict::fromBlock [::nano::block::json::toBlock $block]]
	dict unset blockDict _blockData
	dict set blockDict signKey $key

	set block     [::nano::block::jsonFromDict $blockDict]
	set block     [::nano::block::json::fromDict $blockDict]
	set block     [::nano::block::json::sign $block $key -update]
	set blockDict [::json::json2dict $block]
	set blockSignature [string toupper [dict get $blockDict signature]]
	if {$blockSignature ne $blockSignature_expected} {
		puts "\[3.FAIL\] Got: $blockSignature"
		puts "\[3.FAIL\] Exp: $blockSignature_expected"

		return false
	}

	# Verifying a block
	set signature [::nano::block::signBlockJSON $block $key -hex]
	set verify    [::nano::block::verifyBlockJSON $block $signature [::nano::key::publicKeyFromPrivateKey $key]]
	set signature [::nano::block::json::sign $block $key -hex]
	set verify    [::nano::block::json::verifySignature $block]
	if {!$verify} {
		puts "\[4.FAIL\] Got: $verify"
		puts "\[4.FAIL\] Exp: true"

		return false
	}

238
239
240
241
242
243
244
245

246
247
248
249
250
251
252
238
239
240
241
242
243
244

245
246
247
248
249
250
251
252







-
+







		puts "\[2.FAIL\] Exp: false"

		return false
	}

	# Generation
	set blockhash "1C840FED01000000D8CBCF440CB1E4DF386761E6E66609563BD62A649DF6D0BE"
	set work      [::nano::work::fromBlockhash $blockhash]
	set work      [::nano::work::fromBlockHash $blockhash]
	set verify    [::nano::work::validate $blockhash $work]
	if {!$verify} {
		puts "\[3.FAIL\] Got: $verify"
		puts "\[3.FAIL\] Exp: true"

		return false
	}

Modified examples/generate-work/generate-work from [dad84c19fb] to [9ea0cf83ab].

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
19












-
+






#! /usr/bin/env tclsh

package require nano

if {[llength $argv] == 0} {
	puts stderr "Usage: generate-work <hash>..."

	exit 1
}

foreach arg $argv {
	if {[catch {
		set work [::nano::work::fromBlockhash $arg]
		set work [::nano::work::fromBlockHash $arg]
	} err]} {
		puts stderr "Error generating work for \"$arg\": $err"
	} else {
		puts "WORK($arg) = $work"
	}
}

Modified examples/offline-batch-sign/sign from [9a3ce01965] to [aeab02f36b].

39
40
41
42
43
44
45
46

47
48
49
50
51
52
53

54
55
56
57
58
59
60
39
40
41
42
43
44
45

46
47
48
49
50
51
52

53
54
55
56
57
58
59
60







-
+






-
+







	prompt "Seed" seed
	if {[string length $seed] == 0} {
		break
	}

	if {[catch {
		# Verify the seed
		::nano::internal::generateKey [binary decode hex $seed] 0
		::nano::key::fromSeed $seed 0

		lappend seeds $seed
	} err]} {
		puts "Error: $err"
	}
}
set penultimateDestinationKey [::nano::internal::generateKey [binary decode hex [lindex $seeds 0]] 0]
set penultimateDestinationKey [::nano::key::fromSeed [lindex $seeds 0] 0]
set penultimateDestination    [::nano::address::fromPrivateKey $penultimateDestinationKey]

puts ">> Output File"
while true {
	prompt "Filename" outputFileName
	set outputFileName [file normalize $outputFileName]
	if {[catch {

Modified nano.c from [ec928b6713] to [5ea860caff].

435
436
437
438
439
440
441

442
443


444
445
446
447
448
449
450
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453







+


+
+







	memcpy(workOut, work, sizeof(work));

	return;
}

static int nano_tcl_validate_work(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
	unsigned char *blockhash, *work;
	Tcl_WideUInt tclWorkMin;
	uint64_t workMin = NANO_WORK_DEFAULT_MIN;
	int blockhash_length, work_length;
	int valid, result;
	int tgwifo_ret;

	if (objc < 3 || objc > 4) {
		Tcl_WrongNumArgs(interp, 1, objv, "workBlockhash work ?workMin?");

		return(TCL_ERROR);
	}

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
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







-
-
-
+
+
+
+
-
-
-
+
+
+
-

















+




+















-
-
-
-
+
+
+
+
+
+







	if (work_length != NANO_WORK_VALUE_LENGTH) {
		Tcl_SetResult(interp, "Work size is wrong", NULL);

		return(TCL_ERROR);
	}

	if (objc == 4) {
		/* XXX:TODO: Implement getting a uint64_t from Tcl */
		Tcl_SetResult(interp, "User-supplied workMin is not implemented", NULL);

		tgwifo_ret = Tcl_GetWideIntFromObj(interp, objv[3], (Tcl_WideInt *) &tclWorkMin);
		if (tgwifo_ret != TCL_OK) {
			return(tgwifo_ret);
		}
		return(TCL_ERROR);
	}


		workMin = tclWorkMin;
	}
	int valid, result;

	valid = nano_validate_work(blockhash, work, workMin);
	if (valid) {
		result = 1;
	} else {
		result = 0;
	}

	Tcl_SetObjResult(interp, Tcl_NewBooleanObj(result));

	return(TCL_OK);

	/* NOTREACH */
	clientData = clientData;
}

static int nano_tcl_generate_work(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
	Tcl_WideUInt tclWorkMin;
	unsigned char *blockhash;
	unsigned char work[NANO_WORK_VALUE_LENGTH];
	uint64_t workMin = NANO_WORK_DEFAULT_MIN;
	int blockhash_length;
	int tgwifo_ret;

	if (objc < 2 || objc > 3) {
		Tcl_WrongNumArgs(interp, 1, objv, "workBlockhash ?workMin?");

		return(TCL_ERROR);
	}

	blockhash = Tcl_GetByteArrayFromObj(objv[1], &blockhash_length);
	if (blockhash_length != NANO_BLOCK_HASH_LENGTH) {
		Tcl_SetResult(interp, "Block hash size is wrong", NULL);

		return(TCL_ERROR);
	}

	if (objc == 3) {
		/* XXX:TODO: Implement getting a uint64_t from Tcl */
		Tcl_SetResult(interp, "User-supplied workMin is not implemented", NULL);

		return(TCL_ERROR);
		tgwifo_ret = Tcl_GetWideIntFromObj(interp, objv[2], (Tcl_WideInt *) &tclWorkMin);
		if (tgwifo_ret != TCL_OK) {
			return(tgwifo_ret);
		}

		workMin = tclWorkMin;
	}

	nano_generate_work(blockhash, work, workMin);

	Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(work, sizeof(work)));

	return(TCL_OK);

Modified nano.tcl from [9876c8ba9b] to [a0ad3d6e3d].

1
2
3
4
5
6
7
8
9


10
11
12
13


14


15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27









+
+




+
+

+
+







#! /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::json {}
namespace eval ::nano::block::dict {}
namespace eval ::nano::block::create {}
namespace eval ::nano::work {}
namespace eval ::nano::account {}

# Constants
set ::nano::block::stateBlockPreamble [binary decode hex "0000000000000000000000000000000000000000000000000000000000000006"]
set ::nano::address::base32alphabet {13456789abcdefghijkmnopqrstuwxyz}

# Address management functions
proc ::nano::address::toPublicKey {address args} {
	set performChecksumCheck false
	set outputFormat "bytes"
	foreach arg $args {
		switch -exact -- $arg {
			"-verify" {
				set performChecksumCheck true
80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
128
129


130
131
132
133
134


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
86
87
88
89
90
91
92

93
94
95
96
97
98
99
100
101
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
128
129
130
131


132
133
134
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







-
+









-
-
-
-
-
-






-
-
+
-
-
-
-
-
-
-
-
+


-
-
-
-
-
-
+
+

-
-
+
+



-
-
+
+









-
-
+
+



+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








		set result $resultBinary
	}

	return $result
}

proc ::nano::address::fromPublicKey {pubKey args} {
proc ::nano::address::fromPublicKey {publicKey args} {
	set addressPrefix "nano_"
	foreach arg $args {
		switch -exact -- $arg {
			"-xrb" {
				set addressPrefix "xrb_"
			}
			"-nano" {
				set addressPrefix "nano_"
			}
			"-hex" {
				set inputFormat "hex"
			}
			"-binary" {
				set inputFormat "bytes"
			}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	if {![info exists inputFormat]} {
		if {[string length $pubKey] == $::nano::key::publicKeyLength} {
	if {[string length $publicKey] != $::nano::key::publicKeyLength} {
			set inputFormat "bytes"
		} else {
			set inputFormat "hex"
		}
	}

	if {$inputFormat eq "hex"} {
		set pubKey [binary decode hex $pubKey]
		set publicKey [binary decode hex $publicKey]
	}

	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 checksum [string reverse [::nano::internal::hashData $publicKey 5]]
	append publicKey $checksum

	set pubKey [binary encode hex $pubKey]
	set pubKey [expr "0x$pubKey"]
	set publicKey [binary encode hex $publicKey]
	set publicKey [expr "0x$publicKey"]
	set alphabet [split $::nano::address::base32alphabet ""]
	set address ""
	for {set index 0} {$index < 60} {incr index} {
		set fiveBits [expr {$pubKey & 0x1F}]
		set pubKey [expr {$pubKey >> 5}]
		set fiveBits [expr {$publicKey & 0x1F}]
		set publicKey [expr {$publicKey >> 5}]
		set byte [lindex $alphabet $fiveBits]
		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]
proc ::nano::address::fromPrivateKey {privateKey args} {
	set pubKey [::nano::key::publicKeyFromPrivateKey $privateKey]
	tailcall ::nano::address::fromPublicKey $pubKey {*}$args
}

# Key management functions
proc ::nano::key::generateNewSeed {} {
	tailcall ::nano::internal::generateSeed
}

proc ::nano::key::generateNewKey {} {
	tailcall ::nano::internal::generateKey
}

proc ::nano::key::computeKey {seed args} {
proc ::nano::key::newSeed {args} {
	set outputFormat "bytes"
	foreach arg $args {
		switch -exact -- $arg {
			"-hex" {
				set outputFormat "hex"
			}
			"-binary" {
				set outputFormat "bytes"
			}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	set retval [::nano::internal::generateSeed]

	if {$outputFormat eq "hex"} {
		set retval [binary encode hex $retval]
	}

	return $retval
}

proc ::nano::key::newKey {args} {
	set outputFormat "bytes"
	foreach arg $args {
		switch -exact -- $arg {
			"-hex" {
				set outputFormat "hex"
			}
			"-binary" {
				set outputFormat "bytes"
			}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	set retval [::nano::internal::generateKey]

	if {$outputFormat eq "hex"} {
		set retval [binary encode hex $retval]
	}

	return $retval
}

proc ::nano::key::fromSeed {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]
		}
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
230
231
232
233
216
217
218
219
220
221
222

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238


239
240
241
242

243
244
245
246
247
248
249
250
251


252
253
254
255
256


257
258
259
260
261
262
263
264







-
+















-
-
+
+


-
+







+
-
-
+
+



-
-
+







	if {$outputFormat eq "hex"} {
		set key [binary encode hex $key]
	}

	return $key
}

proc ::nano::key::publicKeyFromPrivateKey {key args} {
proc ::nano::key::publicKeyFromPrivateKey {privateKey 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] != $::nano::key::privateKeyLength} {
		set key [binary decode hex $key]
	if {[string length $privateKey] != $::nano::key::privateKeyLength} {
		set privateKey [binary decode hex $privateKey]
	}

	set pubKey [::nano::internal::publicKey $key]
	set pubKey [::nano::internal::publicKey $privateKey]
	if {$outputFormat eq "hex"} {
		set pubKey [string toupper [binary encode hex $pubKey]]
	}

	return $pubKey
}

# Low-level block management
proc ::nano::block::fromJSON {json} {
	array set block [json::json2dict $json]
proc ::nano::block::dict::toBlock {blockDict} {
	array set block $blockDict

	switch -- $block(type) {
		"state" {
			# XXX:TODO: Find the source of this
			append blockData [binary decode hex "0000000000000000000000000000000000000000000000000000000000000006"]
			append blockData $::nano::block::stateBlockPreamble
			append blockData [::nano::address::toPublicKey $block(account)]
			append blockData [binary decode hex $block(previous)]
			append blockData [::nano::address::toPublicKey $block(representative)]
			append blockData [binary decode hex [format %032llX $block(balance)]]
			if {![info exists block(link)] && [info exists block(link_as_account)]} {
				append blockData [::nano::address::toPublicKey $block(link_as_account)]
			} else {
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
352
353
354
355
356
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







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
+


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-


-
-
-
-
-
-
-
+

-
-
-
-
-
-
-
-





+







			return -code error "Invalid block type $block(type)"
		}
	}

	return $blockData
}

proc ::nano::block::signBlockHash {blockHash key args} {
	set outputFormat "bytes"
	foreach arg $args {
		switch -exact -- $arg {
			"-hex" {
				set outputFormat "hex"
			}
			"-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} {
proc ::nano::block::json::toBlock {blockJSON} {
	set blockHash [::nano::block::hash $blockData]

	tailcall ::nano::block::signBlockHash $blockHash {*}$args
}

proc ::nano::block::signBlockJSON {blockJSON args} {
	set blockData [::nano::block::fromJSON $blockJSON]
	set blockDict [::nano::block::dict::fromJSON $blockJSON]

	tailcall ::nano::block::signBlock $blockData {*}$args
	tailcall ::nano::block::dict::toBlock $blockDict
}

proc ::nano::block::verifyBlockHash {blockHash signature pubKey} {
	if {[string length $blockHash] != $::nano::block::hashLength} {
		set blockHash [binary decode hex $blockHash]
	}

	if {[string length $signature] != $::nano::block::signatureLength} {
		set signature [binary decode hex $signature]
	}

	if {[string length $pubKey] != $::nano::key::publicKeyLength} {
		set key [binary decode hex $pubKey]
	}

	set valid [::nano::internal::verifyDetached $blockHash $signature $pubKey]

	return $valid
}

proc ::nano::block::verifyBlock {blockData args} {
proc ::nano::block::dict::fromJSON {blockJSON} {
	set blockHash [::nano::block::hash $blockData]

	tailcall ::json::json2dict $blockJSON
	tailcall ::nano::block::verifyBlockHash $blockHash {*}$args
}

proc ::nano::block::verifyBlockJSON {blockJSON args} {
	set blockData [::nano::block::fromJSON $blockJSON]

	tailcall ::nano::block::verifyBlock $blockData {*}$args
}

proc ::nano::block::_dictToJSON {blockDict} {
proc ::nano::block::json::fromDict {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 block(signature) [::nano::block::signBlockHash $block(_blockHash) $block(signKey) -hex]
	}

	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)]} {
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
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







+
+
+
+


+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+









-
+







	set blockJSONEntries [lmap field $blockJSONFields {
		if {![info exists block($field)]} {
			continue
		}

		switch -exact -- $field {
			"source" - "previous" - "link" - "_blockHash" - "_workHash" {
				if {[string length $block($field)] == $::nano::block::hashLength} {
					set block($field) [binary encode hex $block($field)]
				}

				set block($field) [string toupper $block($field)]
			}
			"signature" {
				if {[string length $block($field)] == $::nano::block::signatureLength} {
					set block($field) [binary encode hex $block($field)]
		}
				}

				set block($field) [string toupper $block($field)]
			}
			"work" {
				if {[string length $block($field)] == $::nano::work::workValueLength} {
					set block($field) [binary encode hex $block($field)]
				}

				set block($field) [string tolower $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::toDict {blockData args} {
proc ::nano::block::dict::fromBlock {blockData args} {
	set block(type) ""
	set addressPrefix "nano_"
	foreach arg $args {
		switch -glob -- $arg {
			"-type=*" {
				set block(type) [lindex [split $arg =] 1]
			}
426
427
428
429
430
431
432
433

434
435
436
437
438
439
440
441

442
443
444
445
446
447
448
406
407
408
409
410
411
412

413
414
415
416
417
418
419
420

421
422
423
424
425
426
427
428







-
+







-
+







	set addArgs_fromPublicKey [list]
	if {$addressPrefix eq "xrb_"} {
		lappend addArgs_fromPublicKey "-xrb"
	}

	switch -- $block(type) {
		"state" {
			binary scan $blockData H64a32H64a32H32H64 \
			binary scan $blockData a32a32H64a32H32H64 \
				block(header) \
				block(account) \
				block(previous) \
				block(representative) \
				block(balance) \
				block(link)

			if {$block(header) ne "0000000000000000000000000000000000000000000000000000000000000006"} {
			if {$block(header) ne $::nano::block::stateBlockPreamble} {
				return -code error "Invalid block"
			}
		}
		"open" {
			binary scan $blockData H64a32a32 \
				block(source) \
				block(representative) \
485
486
487
488
489
490
491
492
493


494
495

496
497
498
499
500
501
502
465
466
467
468
469
470
471


472
473
474

475
476
477
478
479
480
481
482







-
-
+
+

-
+







	}

	set block(_blockData) $blockData

	return [array get block]
}

proc ::nano::block::toJSON {blockData args} {
	set blockDict [::nano::block::toDict $blockData {*}$args]
proc ::nano::block::json::fromBlock {blockData args} {
	set blockDict [::nano::block::dict::fromBlock $blockData {*}$args]

	set blockJSON [_dictToJSON $blockDict]
	set blockJSON [::nano::block::json::fromDict $blockDict]

	return $blockJSON
}

proc ::nano::block::hash {blockData args} {
	set outputFormat "bytes"
	foreach arg $args {
518
519
520
521
522
523
524

































525
526
527
528


































































































529



















































































530

531


532
533
534












































535
536
537
538
539
540
541
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719

720
721
722
723



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+

+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	if {$outputFormat eq "hex"} {
		set hash [string toupper [binary encode hex $hash]]
	}

	return $hash
}

proc ::nano::block::signBlockHash {blockHash privateKey args} {
	set outputFormat "bytes"
	foreach arg $args {
		switch -exact -- $arg {
			"-hex" {
				set outputFormat "hex"
			}
			"-binary" {
				set outputFormat "bytes"
			}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	if {[string length $blockHash] != $::nano::block::hashLength} {
		set blockHash [binary decode hex $blockHash]
	}

	if {[string length $privateKey] != $::nano::key::privateKeyLength} {
		set privateKey [binary decode hex $privateKey]
	}

	set signature [::nano::internal::signDetached $blockHash $privateKey]

	if {$outputFormat eq "hex"} {
		set signature [string toupper [binary encode hex $signature]]
	}

	return $signature
}

proc ::nano::block::jsonFromDict {blockDict} {
	set blockJSON [::nano::block::_dictToJSON $blockDict]
	set block [::nano::block::fromJSON $blockJSON]
	set blockHash [::nano::block::hash $block]
proc ::nano::block::sign {blockData args} {
	set blockHash [::nano::block::hash $blockData]

	tailcall ::nano::block::signBlockHash $blockHash {*}$args
}

proc ::nano::block::verifyBlockHash {blockHash signature publicKey} {
	if {[string length $blockHash] != $::nano::block::hashLength} {
		set blockHash [binary decode hex $blockHash]
	}

	if {[string length $signature] != $::nano::block::signatureLength} {
		set signature [binary decode hex $signature]
	}

	if {[string length $publicKey] != $::nano::key::publicKeyLength} {
		set publicKey [binary decode hex $publicKey]
	}

	set valid [::nano::internal::verifyDetached $blockHash $signature $publicKey]

	return $valid
}

proc ::nano::block::verifyBlock {blockData args} {
	set blockHash [::nano::block::hash $blockData]

	tailcall ::nano::block::verifyBlockHash $blockHash {*}$args
}

proc ::nano::block::dict::_addBlockData {blockDict} {
	if {[dict exists $blockDict _blockData]} {
		return $blockDict
	}

	set blockData [::nano::block::dict::toBlock $blockDict]

	dict set blockDict _blockData $blockData

	return $blockDict
}

proc ::nano::block::dict::_addBlockHash {blockDict} {
	if {[dict exists $blockDict _blockHash]} {
		return $blockDict
	}

	set blockDict [_addBlockData $blockDict]
	set blockData [dict get $blockDict _blockData]

	set blockHash [::nano::block::hash $blockData -binary]

	dict set blockDict _blockHash $blockHash

	return $blockDict
}

proc ::nano::block::dict::sign {blockDict privateKey args} {
	set outputMode "signature"
	set outputFormat "bytes"
	foreach arg $args {
		switch -- $arg {
			"-update" {
				set outputMode "update"
			}
			"-signature" {
				set outputMode "signature"
			}
			"-hex" {
				set outputFormat "hex"
			}
			"-binary" {
				set outputFormat "bytes"
			}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	set blockDict [_addBlockHash $blockDict]

	set blockHash [dict get $blockDict _blockHash]

	set signature [::nano::block::signBlockHash $blockHash $privateKey -binary]

	if {$outputMode eq "signature"} {
		if {$outputFormat eq "hex"} {
			set signature [binary encode hex $signature]
		}

		return $signature
	}

	dict set blockDict signature $signature

	return $blockDict
}

proc ::nano::block::json::sign {blockJSON privateKey args} {
	set outputMode "signature"
	foreach arg $args {
		switch -- $arg {
			"-update" {
				set outputMode "update"
			}
			"-signature" {
				set outputMode "signature"
			}
			"-hex" - "-binary" {}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	set blockDict [::nano::block::dict::fromJSON $blockJSON]

	set retval [::nano::block::dict::sign $blockDict $privateKey {*}$args]

	if {$outputMode eq "signature"} {
		return $retval
	}

	set retval [::nano::block::json::fromDict $retval]

	return $retval
}

proc ::nano::block::dict::verifySignature {blockDict} {
	set publicKey [::nano::address::toPublicKey [dict get $blockDict account]]
	set signature [dict get $blockDict signature]

	set blockDict [_addBlockHash $blockDict]

	set blockHash [dict get $blockDict _blockHash]

	tailcall ::nano::block::verifyBlockHash $blockHash $signature $publicKey
}

proc ::nano::block::json::verifySignature {blockJSON} {
	set blockDict [::nano::block::dict::fromJSON $blockJSON]

	tailcall ::nano::block::dict::verifySignature $blockDict
}

proc ::nano::block::dict::work {blockDict args} {
	set outputMode "work"
	foreach arg $args {
		switch -- $arg {
			"-update" {
				set outputMode "update"
			}
			"-work" {
				set outputMode "work"
			}
			"-hex" {
				set outputFormat "hex"
			}
			"-binary" {
				set outputFormat "bytes"
			}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	set blockDict [_addBlockHash $blockDict]

	set blockHash [dict get $blockDict _blockHash]

	set work [::nano::work::fromBlockHash $blockHash -binary]

	if {$outputMode eq "work"} {
		if {$outputFormat eq "hex"} {
			set work [binary encode hex $work]
		}

		return $work
	}

	dict set blockDict "_blockHash" [binary encode hex $blockHash]
	dict set blockDict work $work

	return $blockDict
}
	set blockJSON [::nano::block::_dictToJSON $blockDict]

	return $blockJSON

proc ::nano::block::json::work {blockJSON args} {
	set outputMode "work"
	foreach arg $args {
		switch -- $arg {
			"-update" {
				set outputMode "update"
			}
			"-work" {
				set outputMode "work"
			}
			"-hex" - "-binary" {}
			default {
				return -code error "Invalid option: $arg"
			}
		}
	}

	set blockDict [::nano::block::dict::fromJSON $blockJSON]

	set retval [::nano::block::dict::work $blockDict {*}$args]

	if {$outputMode eq "work"} {
		return $retval
	}

	set retval [::nano::block::json::fromDict $retval]

	return $retval
}

proc ::nano::block::dict::validateWork {blockDict} {
	set blockDict [_addBlockHash $blockDict]

	set blockHash [dict get $blockDict _blockHash]
	set work      [dict get $blockDict work]

	tailcall ::nano::work::validate $blockHash $work
}

proc ::nano::block::json::validateWork {blockJSON} {
	set blockDict [::nano::block::dict::fromJSON $blockJSON]

	tailcall ::nano::block::dict::validate $blockDict
}

#   send from <account> to <account> previousBalance <balance>
#        amount <amount> sourceBlock <sourceBlockHash>
#        previous <previousBlockHash> ?representative <representative>?
proc ::nano::block::create::send {args} {
	array set block $args
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
786
787
788
789
790
791
792
793
794

795
796
797
798
799

800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816







+
+
-
+
+
+


-
+








+







		"balance" $block(balance) \
		"link_as_account" $block(to) \
		"_workHash" $block(previous) \
		"_comment" "Send $block(amount) raw from $block(from) to $block(to)" \
	]

	if {[info exists block(signKey)]} {
		set blockDict [::nano::block::dict::sign $blockDict $block(signKey) -update]
	}
		dict set blockDict signKey $block(signKey)

	if {[info exists block(-json)] && $block(-json)} {
		return [::nano::block::json::fromDict $blockDict]
	}

	tailcall ::nano::block::jsonFromDict $blockDict
	return $blockDict
}

# Usage:
#   receive to <account> previousBalance <balance> amount <amount>
#           sourceBlock <sourceBlockHash> ?previous <previousBlockHash>?
#           ?representative <representative>?
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
591
592
593
594
595
596
597


598



599
600
601

602
603
604
605
606
607
608
829
830
831
832
833
834
835
836
837

838
839
840
841
842

843
844
845
846
847
848
849
850







+
+
-
+
+
+


-
+







		"balance" $block(balance) \
		"link" $block(sourceBlock) \
		"_workHash" $block(_workHash) \
		"_comment" "Receive $block(amount) raw on $block(to) from hash $block(sourceBlock)" \
	]

	if {[info exists block(signKey)]} {
		set blockDict [::nano::block::dict::sign $blockDict $block(signKey) -update]
	}
		dict set blockDict signKey $block(signKey)

	if {[info exists block(-json)] && $block(-json)} {
		return [::nano::block::json::fromDict $blockDict]
	}

	tailcall ::nano::block::jsonFromDict $blockDict
	return $blockDict
}

# Usage:
#   setRepresentative account <account> previous <previousBlockHash>
#                     representative <newRepresentative> balance <balance>
proc ::nano::block::create::setRepresentative {args} {
	array set block $args
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
662

663
664
665

666
667
668
669
670
671
672
861
862
863
864
865
866
867
868
869
870
871
872


873
874
875
876
877
878
879



880
881
882
883
884

885
886
887
888
889
890


891
892



893





894


895
896



897
898
899
900
901

902
903
904
905

906
907
908

909
910
911
912
913
914
915
916







+
+
+
+
+
-
-
+
+

+
+
+
+
-
-
-
+
+
+


-
+

+



-
-
+
+
-
-
-
+
-
-
-
-
-
+
-
-


-
-
-
+
+
+


-
+



-
+


-
+







		"_workHash" $block(previous) \
	]

	if {[info exists block(signKey)]} {
		dict set blockDict signKey $block(signKey)
	}

	if {[info exists block(signKey)]} {
		set blockDict [::nano::block::dict::sign $blockDict $block(signKey) -update]
	}

	if {[info exists block(-json)] && $block(-json)} {
	tailcall ::nano::block::jsonFromDict $blockDict
}
		return [::nano::block::json::fromDict $blockDict]
	}

	return $blockDict
}

# Work generation functions
proc ::nano::work::fromBlockhash {blockhash} {
	if {[string length $blockhash] != 32} {
		set blockhash [binary decode hex $blockhash]
proc ::nano::work::fromBlockHash {blockHash} {
	if {[string length $blockHash] != $::nano::block::hashLength} {
		set blockHash [binary decode hex $blockHash]
	}

	set work [binary encode hex [::nano::internal::generateWork $blockhash]]
	set work [binary encode hex [::nano::internal::generateWork $blockHash]]
	set work [string tolower $work]

	return $work
}

proc ::nano::work::fromBlock {blockJSON} {
	set blockDict [::json::json2dict $blockJSON]
proc ::nano::work::fromBlock {blockData} {
	set blockHash [::nano::block::hash $blockData -binary]
	set workhash [dict get $blockDict _workHash]
	tailcall ::nano::work::fromBlockhash $workhash
}


proc ::nano::work::updateBlock {blockJSON} {
	set blockDict [::json::json2dict $blockJSON]
	set workhash [dict get $blockDict _workHash]
	set work [::nano::work::fromBlockhash $workhash]
	tailcall ::nano::work::fromBlockhash $blockHash
	dict set blockDict work $work
	tailcall ::nano::block::_dictToJSON $blockDict
}

proc ::nano::work::validate {blockhash work} {
	if {[string length $blockhash] != 32} {
		set blockhash [binary decode hex $blockhash]
proc ::nano::work::validate {blockHash work} {
	if {[string length $blockHash] != $::nano::block::hashLength} {
		set blockHash [binary decode hex $blockHash]
	}

	if {[string length $work] != 8} {
	if {[string length $work] != $::nano::work::workValueLength} {
		set work [binary decode hex $work]
	}

	tailcall ::nano::internal::validateWork $blockhash $work
	tailcall ::nano::internal::validateWork $blockHash $work
}

# -- Tracked accounts --
# High level account management
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 \
	]
}

690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706


707
708
709
710
711
712
713

714
715
716
717
718
719
720
934
935
936
937
938
939
940

941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966







-









+
+







+







proc ::nano::account::receive {account blockHash signKey} {
	set accountPubKey [::nano::address::toPublicKey $account -hex]

	set frontierInfo [getFrontier $account]
	dict with frontierInfo {}

	set blockInfo [dict get $::nano::account::pending $accountPubKey $blockHash]
	dict unset ::nano::account::pending $accountPubKey $blockHash

	set amount [dict get $blockInfo amount]
	set blockArgs [list to $account previousBalance $balance \
	                    amount $amount sourceBlock $blockHash \
	                    signKey $signKey representative $representative]

	if {[info exists frontierHash]} {
		lappend blockArgs previous $frontierHash
	}

	dict set blockArgs -json true

	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
	dict unset ::nano::account::pending $accountPubKey $blockHash

	return $block
}

proc ::nano::account::send {fromAccount toAccount amount signKey} {
	set fromAccountPubKey [::nano::address::toPublicKey $fromAccount -hex]
	set toAccountPubKey   [::nano::address::toPublicKey $fromAccount -hex]
730
731
732
733
734
735
736
737


738
739
740
741
742
743
744
976
977
978
979
980
981
982

983
984
985
986
987
988
989
990
991







-
+
+








	set block [::nano::block::create::send \
		from            $fromAccount \
		to              $toAccount \
		previous        $fromFrontierHash \
		previousBalance $fromBalance \
		amount          $amount \
		signKey         $signKey
		signKey         $signKey \
		-json           true
	]

	set newBalance [expr {$fromBalance - $amount}]
	set newFrontierHash [dict get [json::json2dict $block] "_blockHash"]

	setFrontier $fromAccount $newFrontierHash $newBalance $fromRepresentative
	addPending  $toAccount $newFrontierHash $amount