Index: nano.tcl ================================================================== --- nano.tcl +++ nano.tcl @@ -295,11 +295,40 @@ set blockDict [::nano::block::dict::fromJSON $blockJSON] tailcall ::nano::block::dict::toBlock $blockDict } proc ::nano::block::dict::fromJSON {blockJSON} { - tailcall ::json::json2dict $blockJSON + set retval [::json::json2dict $blockJSON] + + if {[dict get $retval "type"] eq "send"} { + set balance [dict get $retval "balance"] + set balance [format %lli "0x$balance"] + dict set retval "balance" $balance + } + + # Parse out the work data + if {[dict get $retval "type"] in {send receive change state}} { + set workDataBasedOn "previous" + } + + if {[dict get $retval "type"] eq "state" && [dict get $retval "previous"] eq "0000000000000000000000000000000000000000000000000000000000000000" && [dict get $retval "link"] eq "0000000000000000000000000000000000000000000000000000000000000000"} { + set workDataBasedOn "account" + } + + if {[dict get $retval "type"] eq "open"} { + set workDataBasedOn "account" + } + + if {[info exists workDataBasedOn]} { + if {$workDataBasedOn eq "previous"} { + dict set retval "_workData" [dict get $retval "previous"] + } else { + dict set retval "_workData" [::nano::address::toPublicKey [dict get $retval "account"]] + } + } + + return $retval } proc ::nano::block::json::fromDict {blockDict} { array set block $blockDict @@ -357,10 +386,18 @@ set block($field) [binary encode hex $block($field)] } set block($field) [string tolower $block($field)] } + "balance" { + if {$block(type) in {send receive change open}} { + set balanceFormatStr %032llx + } else { + set balanceFormatStr %lli + } + set block($field) [string toupper [format $balanceFormatStr "$block($field)"]] + } } return -level 0 [list $field [json::write string $block($field)]] }] set blockJSONEntries [join $blockJSONEntries] @@ -425,26 +462,34 @@ "open" { binary scan $blockData H64a32a32 \ block(source) \ block(representative) \ block(account) + + set block(_workData) $block(account) } "send" { binary scan $blockData H64a32H32 \ block(previous) \ block(destination) \ block(balance) + + set block(_workData) $block(previous) } "receive" { binary scan $blockData H64H64 \ block(previous) \ block(source) + + set block(_workData) $block(previous) } "change" { binary scan $blockData H64a32 \ block(previous) \ block(representative) + + set block(_workData) $block(previous) } default { return -code error "Invalid block type: $block(type)" } } @@ -762,11 +807,11 @@ } proc ::nano::block::json::validateWork {blockJSON} { set blockDict [::nano::block::dict::fromJSON $blockJSON] - tailcall ::nano::block::dict::validate $blockDict + tailcall ::nano::block::dict::validateWork $blockDict } # send from to previousBalance # amount sourceBlock # previous ?representative ? Index: test/test.tcl ================================================================== --- test/test.tcl +++ test/test.tcl @@ -209,10 +209,44 @@ set verify [::nano::block::json::verifySignature $block] if {!$verify} { puts "\[4.FAIL\] Got: $verify" puts "\[4.FAIL\] Exp: true" + return false + } + + # JSON Parse an old-style block + ## Parsing + set block [::nano::block::dict::fromJSON { + { + "account" : "xrb_13ezf4od79h1tgj9aiu4djzcmmguendtjfuhwfukhuucboua8cpoihmh8byo", + "destination" : "xrb_1gys8r4crpxhp94n4uho5cshaho81na6454qni5gu9n53gksoyy1wcd4udyb", + "type" : "send", + "previous" : "F685856D73A488894F7F3A62BC3A88E17E985F9969629FF3FDD4A0D4FD823F24", + "work" : "efe5bf06a43d0e0a", + "signature" : "E373A1A38C9A239F4D2AAE52B40EF6DFC8BFEDCEB476588958073B7F462746854282FFE4B98FA6782E92798DAD0E5483C3356550A31339E1D7934B487EF4570D", + "balance" : "00F035A9C7D818E7C34148C524FFFFEE" + } + }] + ## Convert to JSON + set block [::nano::block::json::fromDict $block] + + ## Convert back and verify signature + set verify [::nano::block::json::verifySignature $block] + if {!$verify} { + puts "\[5.FAIL\] Got: $verify" + puts "\[5.FAIL\] Exp: true" + + return false + } + + ## Verify Proof of Work + set verify [::nano::block::json::validateWork $block] + if {!$verify} { + puts "\[6.FAIL\] Got: $verify" + puts "\[6.FAIL\] Exp: true" + return false } return true }