@ -1,6 +1,7 @@
'use strict' ;
'use strict' ;
var _ = require ( 'lodash' ) ;
var _ = require ( 'lodash' ) ;
var $ = require ( 'preconditions' ) . singleton ( ) ;
var util = require ( 'util' ) ;
var util = require ( 'util' ) ;
var async = require ( 'async' ) ;
var async = require ( 'async' ) ;
var log = require ( 'npmlog' ) ;
var log = require ( 'npmlog' ) ;
@ -61,7 +62,7 @@ function API(opts) {
} ;
} ;
API . prototype . _ loadAndCheck = function ( ) {
API . prototype . _ loadAndCheck = function ( opts ) {
var data = this . storage . load ( ) ;
var data = this . storage . load ( ) ;
if ( ! data ) {
if ( ! data ) {
log . error ( 'Wallet file not found.' ) ;
log . error ( 'Wallet file not found.' ) ;
@ -69,11 +70,15 @@ API.prototype._loadAndCheck = function() {
}
}
if ( data . verified == 'corrupt' ) {
if ( data . verified == 'corrupt' ) {
log . error ( 'The wallet is tagged as corrupt. Some of the copayers cannot be verified to have known the wallet secret.' ) ;
throw new Error ( 'The wallet is tagged as corrupt. Some of the copayers cannot be verified to have known the wallet secret.' ) ;
process . exit ( 1 ) ;
}
}
if ( data . n > 1 ) {
if ( data . n > 1 ) {
var pkrComplete = data . publicKeyRing && data . m && data . publicKeyRing . length === data . n ;
var pkrComplete = data . publicKeyRing && data . m && data . publicKeyRing . length === data . n ;
if ( opts . requireCompletePKR && ! pkrComplete ) {
throw new Error ( 'Wallet Incomplete, cannot derive address.' ) ;
}
if ( ! pkrComplete ) {
if ( ! pkrComplete ) {
log . warn ( 'The file ' + this . filename + ' is incomplete. It will allow you to operate with the wallet but it should not be trusted as a backup. Please wait for all copayers to join the wallet and run the tool with -export flag.' )
log . warn ( 'The file ' + this . filename + ' is incomplete. It will allow you to operate with the wallet but it should not be trusted as a backup. Please wait for all copayers to join the wallet and run the tool with -export flag.' )
}
}
@ -135,7 +140,7 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb
data = {
data = {
m : m ,
m : m ,
n : n ,
n : n ,
walletPrivKey : privKey . toString ( ) ,
walletPrivKey : privKey . toWIF ( ) ,
network : network ,
network : network ,
} ;
} ;
@ -193,11 +198,12 @@ API.prototype._joinWallet = function(data, secret, copayerName, cb) {
this . _ doPostRequest ( url , args , data , function ( err , body ) {
this . _ doPostRequest ( url , args , data , function ( err , body ) {
var wallet = body . wallet ;
var wallet = body . wallet ;
data . copayerId = body . copayerId ;
data . copayerId = body . copayerId ;
data . walletPrivKey = walletPrivKey ;
data . walletPrivKey = walletPrivKey . toWIF ( ) ;
data . signingPrivKey = signingPrivKey . toString ( ) ;
data . signingPrivKey = signingPrivKey . toString ( ) ;
data . m = wallet . m ;
data . m = wallet . m ;
data . n = wallet . n ;
data . n = wallet . n ;
data . publicKeyRing = wallet . publicKeyRing ;
data . publicKeyRing = wallet . publicKeyRing ;
data . network = wallet . network ,
self . storage . save ( data ) ;
self . storage . save ( data ) ;
return cb ( ) ;
return cb ( ) ;
@ -283,13 +289,10 @@ API.prototype.getAddresses = function(cb) {
API . prototype . createAddress = function ( cb ) {
API . prototype . createAddress = function ( cb ) {
var self = this ;
var self = this ;
var data = this . _ loadAndCheck ( ) ;
var data = this . _ loadAndCheck ( { requireCompletePKR : true } ) ;
$ . checkState ( data . publicKeyRing . length != data . n , 'Wallet Incomplete, cannot derive address.' ) ;
var url = '/v1/addresses/' ;
var url = '/v1/addresses/' ;
this . _ doPostRequest ( url , { } , data , function ( err , address ) {
this . _ doPostRequest ( url , { } , data , function ( err , address ) {
if ( err ) return cb ( err ) ;
if ( err ) return cb ( err ) ;
if ( ! Verifier . checkAddress ( data , address ) ) {
if ( ! Verifier . checkAddress ( data , address ) ) {
return cb ( new ServerCompromisedError ( 'Server sent fake address' ) ) ;
return cb ( new ServerCompromisedError ( 'Server sent fake address' ) ) ;
}
}