@ -1,9 +1,11 @@
'use strict' ;
var _ = require ( 'lodash' ) ;
var Uuid = require ( 'uuid' ) ;
var chai = require ( 'chai' ) ;
var sinon = require ( 'sinon' ) ;
var should = chai . should ( ) ;
var Bitcore = require ( 'bitcore' ) ;
var WalletUtils = require ( '../lib/walletutils' ) ;
var aText = 'hola' ;
@ -16,19 +18,19 @@ var otherPubKey = '02555a2d45e309c00cc8c5090b6ec533c6880ab2d3bc970b3943def989b33
describe ( 'WalletUtils' , function ( ) {
describe ( '#hashMessage' , function ( ) {
it ( 'S hould create a hash' , function ( ) {
it ( 's hould create a hash' , function ( ) {
var res = WalletUtils . hashMessage ( aText ) ;
res . toString ( 'hex' ) . should . equal ( '4102b8a140ec642feaa1c645345f714bc7132d4fd2f7f6202db8db305a96172f' ) ;
} ) ;
} ) ;
describe ( '#signMessage' , function ( ) {
it ( 'S hould sign a message' , function ( ) {
it ( 's hould sign a message' , function ( ) {
var sig = WalletUtils . signMessage ( aText , aPrivKey ) ;
should . exist ( sig ) ;
sig . should . equal ( aSignature ) ;
} ) ;
it ( 'S hould fail to sign with wrong args' , function ( ) {
it ( 's hould fail to sign with wrong args' , function ( ) {
( function ( ) {
WalletUtils . signMessage ( aText , aPubKey ) ;
} ) . should . throw ( 'Number' ) ;
@ -36,22 +38,22 @@ describe('WalletUtils', function() {
} ) ;
describe ( '#verifyMessage' , function ( ) {
it ( 'S hould fail to verify a malformed signature' , function ( ) {
it ( 's hould fail to verify a malformed signature' , function ( ) {
var res = WalletUtils . verifyMessage ( aText , 'badsignature' , otherPubKey ) ;
should . exist ( res ) ;
res . should . equal ( false ) ;
} ) ;
it ( 'S hould fail to verify a null signature' , function ( ) {
it ( 's hould fail to verify a null signature' , function ( ) {
var res = WalletUtils . verifyMessage ( aText , null , otherPubKey ) ;
should . exist ( res ) ;
res . should . equal ( false ) ;
} ) ;
it ( 'S hould fail to verify with wrong pubkey' , function ( ) {
it ( 's hould fail to verify with wrong pubkey' , function ( ) {
var res = WalletUtils . verifyMessage ( aText , aSignature , otherPubKey ) ;
should . exist ( res ) ;
res . should . equal ( false ) ;
} ) ;
it ( 'S hould verify' , function ( ) {
it ( 's hould verify' , function ( ) {
var res = WalletUtils . verifyMessage ( aText , aSignature , aPubKey ) ;
should . exist ( res ) ;
res . should . equal ( true ) ;
@ -59,7 +61,7 @@ describe('WalletUtils', function() {
} ) ;
describe ( '#signMessage #verifyMessage round trip' , function ( ) {
it ( 'S hould sign and verify' , function ( ) {
it ( 's hould sign and verify' , function ( ) {
var aLongerText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." ;
var sig = WalletUtils . signMessage ( aLongerText , aPrivKey ) ;
WalletUtils . verifyMessage ( aLongerText , sig , aPubKey ) . should . equal ( true ) ;
@ -74,4 +76,21 @@ describe('WalletUtils', function() {
msg . should . equal ( 'hello world' ) ;
} ) ;
} ) ;
describe ( '#toSecret #fromSecret round trip' , function ( ) {
it ( 'should create secret and parse secret' , function ( ) {
var i = 0 ;
while ( i ++ < 100 ) {
var walletId = Uuid . v4 ( ) ;
var walletPrivKey = new Bitcore . PrivateKey ( ) ;
var network = 'testnet' ;
var secret = WalletUtils . toSecret ( walletId , walletPrivKey , network ) ;
var result = WalletUtils . fromSecret ( secret ) ;
result . walletId . should . equal ( walletId ) ;
result . walletPrivKey . toString ( ) . should . equal ( walletPrivKey . toString ( ) ) ;
result . network . should . equal ( network ) ;
} ;
} ) ;
} ) ;
} ) ;