2015-02-12 11:42:32 -08:00
'use strict' ;
var _ = require ( 'lodash' ) ;
var async = require ( 'async' ) ;
var log = require ( 'npmlog' ) ;
var request = require ( 'request' )
log . debug = log . verbose ;
log . level = 'debug' ;
var fs = require ( 'fs' )
var Bitcore = require ( 'bitcore' )
2015-02-12 11:50:10 -08:00
var SignUtils = require ( './signutils' ) ;
2015-02-12 11:42:32 -08:00
var BASE _URL = 'http://localhost:3001/copay/api/' ;
2015-02-13 07:45:05 -08:00
function _createProposalOpts ( opts , signingKey ) {
var msg = opts . toAddress + '|' + opts . amount + '|' + opts . message ;
opts . proposalSignature = SignUtils . sign ( msg , signingKey ) ;
return opts ;
} ;
2015-02-12 11:50:10 -08:00
function _getUrl ( path ) {
2015-02-12 11:42:32 -08:00
return BASE _URL + path ;
} ;
2015-02-12 19:00:54 -08:00
function _parseError ( body ) {
if ( _ . isString ( body ) ) {
2015-02-13 07:45:05 -08:00
try {
2015-02-12 19:00:54 -08:00
body = JSON . parse ( body ) ;
2015-02-13 07:45:05 -08:00
} catch ( e ) {
body = { error : body } ;
}
2015-02-12 19:00:54 -08:00
}
var code = body . code || 'ERROR' ;
var message = body . error || 'There was an unknown error processing the request' ;
log . error ( code , message ) ;
} ;
function _signRequest ( url , args , privKey ) {
2015-02-12 19:50:57 -08:00
var message = url + '|' + JSON . stringify ( args ) ;
2015-02-12 19:00:54 -08:00
return SignUtils . sign ( message , privKey ) ;
} ;
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
function _createXPrivKey ( ) {
return new Bitcore . HDPrivateKey ( ) . toString ( ) ;
} ;
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
function CliLib ( opts ) {
2015-02-12 18:57:16 -08:00
if ( ! opts . filename ) {
throw new Error ( 'Please set the config filename' ) ;
}
2015-02-12 19:00:54 -08:00
this . filename = opts . filename ;
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 18:57:16 -08:00
2015-02-12 19:50:57 -08:00
CliLib . prototype . _save = function ( data ) {
2015-02-12 19:00:54 -08:00
fs . writeFileSync ( this . filename , JSON . stringify ( data ) ) ;
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . _load = function ( ) {
2015-02-12 11:42:32 -08:00
try {
2015-02-12 19:00:54 -08:00
return JSON . parse ( fs . readFileSync ( this . filename ) ) ;
2015-02-12 11:42:32 -08:00
} catch ( ex ) { }
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . _loadAndCheck = function ( ) {
var data = this . _load ( ) ;
if ( ! data ) {
log . error ( 'Wallet file not found.' ) ;
process . exit ( 1 ) ;
}
2015-02-12 20:21:24 -08:00
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.' ) ;
process . exit ( 1 ) ;
}
2015-02-12 19:00:54 -08:00
if ( data . n > 1 ) {
var pkrComplete = data . publicKeyRing && data . m && data . publicKeyRing . length === data . n ;
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.' )
}
}
return data ;
2015-02-12 13:54:17 -08:00
} ;
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
CliLib . prototype . createWallet = function ( walletName , copayerName , m , n , network , cb ) {
var self = this ;
var data = this . _load ( ) ;
2015-02-13 07:02:30 -08:00
if ( data ) return cb ( 'File ' + this . filename + ' already contains a wallet' ) ;
2015-02-12 13:54:17 -08:00
2015-02-13 05:58:49 -08:00
// Generate wallet key pair to verify copayers
var privKey = new Bitcore . PrivateKey ( ) ;
var pubKey = privKey . toPublicKey ( ) ;
2015-02-12 13:54:17 -08:00
data = {
xPrivKey : _createXPrivKey ( ) ,
m : m ,
2015-02-13 05:58:49 -08:00
n : n ,
walletPrivKey : privKey . toString ( ) ,
2015-02-12 13:54:17 -08:00
} ;
2015-02-12 11:42:32 -08:00
var args = {
name : walletName ,
m : m ,
n : n ,
pubKey : pubKey . toString ( ) ,
2015-02-12 19:00:54 -08:00
network : network || 'livenet' ,
2015-02-12 11:42:32 -08:00
} ;
request ( {
method : 'post' ,
2015-02-12 19:50:57 -08:00
url : _getUrl ( '/v1/wallets/' ) ,
2015-02-12 11:42:32 -08:00
body : args ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
2015-02-12 19:00:54 -08:00
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
var walletId = body . walletId ;
2015-02-12 19:23:59 -08:00
var secret = walletId + ':' + privKey . toString ( ) ;
2015-02-12 19:00:54 -08:00
data . secret = secret ;
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
self . _save ( data ) ;
self . _joinWallet ( data , secret , copayerName , function ( err ) {
2015-02-12 11:42:32 -08:00
if ( err ) return cb ( err ) ;
2015-02-12 13:54:17 -08:00
return cb ( null , data . secret ) ;
2015-02-12 11:42:32 -08:00
} ) ;
} ) ;
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . _joinWallet = function ( data , secret , copayerName , cb ) {
var self = this ;
2015-02-12 13:54:17 -08:00
2015-02-12 19:23:59 -08:00
var secretSplit = secret . split ( ':' ) ;
2015-02-12 11:42:32 -08:00
var walletId = secretSplit [ 0 ] ;
var privKey = Bitcore . PrivateKey . fromString ( secretSplit [ 1 ] ) ;
2015-02-12 19:00:54 -08:00
var xPubKey = new Bitcore . HDPublicKey ( data . xPrivKey ) ;
var xPubKeySignature = SignUtils . sign ( xPubKey . toString ( ) , privKey ) ;
2015-02-12 19:23:59 -08:00
var signingPrivKey = ( new Bitcore . HDPrivateKey ( data . xPrivKey ) ) . derive ( 'm/1/0' ) . privateKey ;
2015-02-12 11:42:32 -08:00
var args = {
walletId : walletId ,
name : copayerName ,
2015-02-12 19:00:54 -08:00
xPubKey : xPubKey . toString ( ) ,
2015-02-12 11:42:32 -08:00
xPubKeySignature : xPubKeySignature ,
} ;
request ( {
method : 'post' ,
2015-02-12 19:50:57 -08:00
url : _getUrl ( '/v1/wallets/' + walletId + '/copayers' ) ,
2015-02-12 11:42:32 -08:00
body : args ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
2015-02-12 19:00:54 -08:00
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
var wallet = body . wallet ;
data . copayerId = body . copayerId ;
data . signingPrivKey = signingPrivKey . toString ( ) ;
data . m = wallet . m ;
data . n = wallet . n ;
data . publicKeyRing = wallet . publicKeyRing ;
self . _save ( data ) ;
return cb ( ) ;
} ) ;
} ;
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
CliLib . prototype . joinWallet = function ( secret , copayerName , cb ) {
var self = this ;
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
var data = this . _load ( ) ;
2015-02-13 07:02:30 -08:00
if ( data ) return cb ( 'File ' + this . filename + ' already contains a wallet' ) ;
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
data = {
xPrivKey : _createXPrivKey ( ) ,
} ;
self . _joinWallet ( data , secret , copayerName , cb ) ;
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . status = function ( cb ) {
var self = this ;
var data = this . _loadAndCheck ( ) ;
2015-02-12 13:54:17 -08:00
2015-02-12 19:50:57 -08:00
var url = '/v1/wallets/' ;
var signature = _signRequest ( url , { } , data . signingPrivKey ) ;
2015-02-12 13:54:17 -08:00
2015-02-12 11:42:32 -08:00
request ( {
2015-02-12 13:54:17 -08:00
headers : {
'x-identity' : data . copayerId ,
'x-signature' : signature ,
} ,
2015-02-12 11:42:32 -08:00
method : 'get' ,
2015-02-12 13:54:17 -08:00
url : _getUrl ( url ) ,
2015-02-12 19:00:54 -08:00
json : true ,
2015-02-12 11:42:32 -08:00
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
2015-02-12 19:00:54 -08:00
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
2015-02-12 20:21:24 -08:00
var wallet = body ;
2015-02-13 05:58:49 -08:00
if ( wallet . n > 0 && wallet . status === 'complete' && ! data . verified ) {
var pubKey = Bitcore . PrivateKey . fromString ( data . walletPrivKey ) . toPublicKey ( ) . toString ( ) ;
2015-02-12 20:21:24 -08:00
var fake = [ ] ;
_ . each ( wallet . copayers , function ( copayer ) {
2015-02-13 05:58:49 -08:00
if ( ! SignUtils . verify ( copayer . xPubKey , copayer . xPubKeySignature , pubKey ) ) {
2015-02-12 20:21:24 -08:00
fake . push ( copayer ) ;
}
} ) ;
if ( fake . length > 0 ) {
log . error ( 'Some copayers in the wallet could not be verified to have known the wallet secret' ) ;
data . verified = 'corrupt' ;
} else {
data . verified = 'ok' ;
}
self . _save ( data ) ;
}
2015-02-12 11:42:32 -08:00
2015-02-12 20:21:24 -08:00
return cb ( null , wallet ) ;
2015-02-12 11:42:32 -08:00
} ) ;
} ;
2015-02-13 07:45:05 -08:00
/ * *
* send
*
* @ param inArgs
* @ param inArgs . toAddress
* @ param inArgs . amount
* @ param inArgs . message
* /
CliLib . prototype . send = function ( inArgs , cb ) {
2015-02-13 06:38:25 -08:00
var self = this ;
var data = this . _loadAndCheck ( ) ;
2015-02-13 07:45:05 -08:00
var args = _createProposalOpts ( inArgs , data . signingPrivKey ) ;
console . log ( '[clilib.js.249:args:]' , args ) ; //TODO
2015-02-13 06:38:25 -08:00
2015-02-13 07:45:05 -08:00
var url = '/v1/txproposals/' ;
var signature = _signRequest ( url , args , data . signingPrivKey ) ;
2015-02-13 06:38:25 -08:00
request ( {
headers : {
'x-identity' : data . copayerId ,
'x-signature' : signature ,
} ,
2015-02-13 07:45:05 -08:00
method : 'post' ,
2015-02-13 06:38:25 -08:00
url : _getUrl ( url ) ,
2015-02-13 07:45:05 -08:00
body : args ,
2015-02-13 06:38:25 -08:00
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
2015-02-13 07:45:05 -08:00
console . log ( '[clilib.js.251:body:]' , body ) ; //TODO
return cb ( null , body ) ;
2015-02-13 06:38:25 -08:00
} ) ;
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . sign = function ( proposalId , cb ) {
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . reject = function ( proposalId , cb ) {
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . address = function ( cb ) {
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 19:00:54 -08:00
CliLib . prototype . history = function ( limit , cb ) {
2015-02-12 11:42:32 -08:00
} ;
2015-02-12 19:00:54 -08:00
module . exports = CliLib ;