Index: build/test/test.tcl ================================================================== --- build/test/test.tcl +++ build/test/test.tcl @@ -202,10 +202,19 @@ 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]] + if {!$verify} { + puts "\[4.FAIL\] Got: $verify" + puts "\[4.FAIL\] Exp: true" + + return false + } return true } set tests { Index: nano.tcl ================================================================== --- nano.tcl +++ nano.tcl @@ -9,10 +9,11 @@ namespace eval ::nano::block {} namespace eval ::nano::block::create {} namespace eval ::nano::account {} set ::nano::block::hashLength 32 +set ::nano::block::signatureLength 64 set ::nano::key::publicKeyLength 32 set ::nano::key::privateKeyLength 32 set ::nano::key::seedLength 32 set ::nano::address::base32alphabet {13456789abcdefghijkmnopqrstuwxyz} @@ -297,30 +298,46 @@ proc ::nano::block::signBlock {blockData args} { set blockHash [::nano::block::hash $blockData] tailcall ::nano::block::signBlockHash $blockHash {*}$args } + +proc ::nano::block::signBlockJSON {blockJSON args} { + set blockData [::nano::block::fromJSON $blockJSON] + + tailcall ::nano::block::signBlock $blockData {*}$args +} 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 $signature + return $valid } proc ::nano::block::verifyBlock {blockData args} { set blockHash [::nano::block::hash $blockData] 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} { array set block $blockDict if {[info exists block(signKey)] && ([info exists block(_blockData)] || [info exists block(_blockHash)])} {