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
2015-02-13 11:07:47 -08:00
var BASE _URL = 'http://localhost:3001/copay/api' ;
2015-02-12 11:42:32 -08:00
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-13 08:35:20 -08:00
body = JSON . parse ( body ) ;
2015-02-13 07:45:05 -08:00
} catch ( e ) {
2015-02-13 08:35:20 -08:00
body = {
error : body
} ;
2015-02-13 07:45:05 -08:00
}
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-13 13:24:35 -08:00
function _createXPrivKey ( network ) {
return new Bitcore . HDPrivateKey ( network ) . toString ( ) ;
2015-02-12 19:00:54 -08:00
} ;
2015-02-12 11:42:32 -08:00
2015-02-13 10:49:47 -08:00
function ClientLib ( 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-13 10:49:47 -08:00
ClientLib . 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-13 10:49:47 -08:00
ClientLib . 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-13 10:49:47 -08:00
ClientLib . prototype . _loadAndCheck = function ( ) {
2015-02-12 19:00:54 -08:00
var data = this . _load ( ) ;
if ( ! data ) {
log . error ( 'Wallet file not found.' ) ;
process . exit ( 1 ) ;
}
2015-02-13 08:35:20 -08:00
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-13 10:49:47 -08:00
ClientLib . prototype . createWallet = function ( walletName , copayerName , m , n , network , cb ) {
2015-02-12 19:00:54 -08:00
var self = this ;
2015-02-13 13:24:35 -08:00
network = network || 'livenet' ;
2015-02-13 17:59:05 -08:00
if ( ! _ . contains ( [ 'testnet' , 'livenet' ] , network ) )
return cb ( 'Invalid network' ) ;
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-13 05:58:49 -08:00
// Generate wallet key pair to verify copayers
2015-02-13 13:24:35 -08:00
var privKey = new Bitcore . PrivateKey ( null , network ) ;
2015-02-13 05:58:49 -08:00
var pubKey = privKey . toPublicKey ( ) ;
2015-02-12 13:54:17 -08:00
data = {
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-13 13:24:35 -08:00
network : network ,
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-13 17:59:05 -08:00
var secret = walletId + ':' + privKey . toString ( ) + ':' + ( network == 'testnet' ? 'T' : 'L' ) ;
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-13 13:53:49 -08:00
ClientLib . prototype . _joinWallet = function ( data , secret , copayerName , cb ) {
2015-02-12 19:00:54 -08:00
var self = this ;
2015-02-13 13:24:35 -08:00
data = data || { } ;
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 ] ;
2015-02-13 13:24:35 -08:00
2015-02-13 11:26:33 -08:00
var walletPrivKey = Bitcore . PrivateKey . fromString ( secretSplit [ 1 ] ) ;
2015-02-13 13:24:35 -08:00
var network = secretSplit [ 2 ] == 'T' ? 'testnet' : 'livenet' ;
2015-02-13 13:53:49 -08:00
data . xPrivKey = _createXPrivKey ( network ) ;
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
var xPubKey = new Bitcore . HDPublicKey ( data . xPrivKey ) ;
2015-02-13 11:26:33 -08:00
var xPubKeySignature = SignUtils . sign ( xPubKey . toString ( ) , walletPrivKey ) ;
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 ;
2015-02-13 11:26:33 -08:00
data . walletPrivKey = walletPrivKey ;
2015-02-12 19:00:54 -08:00
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-13 10:49:47 -08:00
ClientLib . prototype . joinWallet = function ( secret , copayerName , cb ) {
2015-02-12 19:00:54 -08:00
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
self . _joinWallet ( data , secret , copayerName , cb ) ;
2015-02-12 11:42:32 -08:00
} ;
2015-02-13 10:49:47 -08:00
ClientLib . prototype . status = function ( cb ) {
2015-02-12 19:00:54 -08:00
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 08:35:20 -08:00
// TODO
//console.log('[clilib.js.214:wallet:]',wallet); //TODO
2015-02-12 20:21:24 -08:00
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 08:35:20 -08:00
console . log ( '[clilib.js.224]' , copayer . xPubKey , copayer . xPubKeySignature , pubKey ) ; //TODO
2015-02-13 05:58:49 -08:00
if ( ! SignUtils . verify ( copayer . xPubKey , copayer . xPubKeySignature , pubKey ) ) {
2015-02-13 08:35:20 -08:00
console . log ( '[clilib.js.227] FAKE' ) ; //TODO
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
* /
2015-02-13 10:49:47 -08:00
ClientLib . 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 ) ;
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
return cb ( null , body ) ;
2015-02-13 06:38:25 -08:00
} ) ;
2015-02-12 11:42:32 -08:00
} ;
2015-02-13 07:55:07 -08:00
// Get addresses
2015-02-13 10:49:47 -08:00
ClientLib . prototype . addresses = function ( cb ) {
2015-02-13 07:55:07 -08:00
var self = this ;
var data = this . _loadAndCheck ( ) ;
var url = '/v1/addresses/' ;
var signature = _signRequest ( url , { } , data . signingPrivKey ) ;
request ( {
headers : {
'x-identity' : data . copayerId ,
'x-signature' : signature ,
} ,
method : 'get' ,
url : _getUrl ( url ) ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
return cb ( null , body ) ;
} ) ;
} ;
// Creates a new address
// TODO: verify derivation!!
2015-02-13 10:49:47 -08:00
ClientLib . prototype . address = function ( cb ) {
2015-02-13 07:55:07 -08:00
var self = this ;
var data = this . _loadAndCheck ( ) ;
var url = '/v1/addresses/' ;
var signature = _signRequest ( url , { } , data . signingPrivKey ) ;
2015-02-12 11:42:32 -08:00
2015-02-13 07:55:07 -08:00
request ( {
headers : {
'x-identity' : data . copayerId ,
'x-signature' : signature ,
} ,
method : 'post' ,
url : _getUrl ( url ) ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
return cb ( null , body ) ;
} ) ;
2015-02-12 11:42:32 -08:00
} ;
2015-02-13 10:49:47 -08:00
ClientLib . prototype . history = function ( limit , cb ) {
2015-02-12 11:42:32 -08:00
} ;
2015-02-13 10:49:47 -08:00
ClientLib . prototype . balance = function ( cb ) {
2015-02-13 08:35:20 -08:00
var self = this ;
var data = this . _loadAndCheck ( ) ;
var url = '/v1/balance/' ;
var signature = _signRequest ( url , { } , data . signingPrivKey ) ;
request ( {
headers : {
'x-identity' : data . copayerId ,
'x-signature' : signature ,
} ,
method : 'get' ,
url : _getUrl ( url ) ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
return cb ( null , body ) ;
} ) ;
} ;
2015-02-13 12:02:56 -08:00
ClientLib . prototype . txProposals = function ( opts , cb ) {
2015-02-13 08:35:20 -08:00
var self = this ;
var data = this . _loadAndCheck ( ) ;
var url = '/v1/txproposals/' ;
var signature = _signRequest ( url , { } , data . signingPrivKey ) ;
request ( {
headers : {
'x-identity' : data . copayerId ,
'x-signature' : signature ,
} ,
method : 'get' ,
url : _getUrl ( url ) ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
return cb ( null , body ) ;
} ) ;
} ;
2015-02-13 12:02:56 -08:00
ClientLib . prototype . sign = function ( txp , cb ) {
var self = this ;
var data = this . _loadAndCheck ( ) ;
//Derive proper key to sign, for each input
var privs = [ ] ,
derived = { } ;
2015-02-13 13:53:49 -08:00
2015-02-13 13:24:35 -08:00
var network = new Bitcore . Address ( txp . toAddress ) . network . name ;
var xpriv = new Bitcore . HDPrivateKey ( data . xPrivKey , network ) ;
2015-02-13 12:02:56 -08:00
_ . each ( txp . inputs , function ( i ) {
if ( ! derived [ i . path ] ) {
derived [ i . path ] = xpriv . derive ( i . path ) . privateKey ;
}
privs . push ( derived [ i . path ] ) ;
} ) ;
var t = new Bitcore . Transaction ( ) ;
_ . each ( txp . inputs , function ( i ) {
t . from ( i , i . publicKeys , txp . requiredSignatures ) ;
} ) ;
t . to ( txp . toAddress , txp . amount )
. change ( txp . changeAddress )
. sign ( privs ) ;
var signatures = [ ] ;
_ . each ( privs , function ( p ) {
var s = t . getSignatures ( p ) [ 0 ] . signature . toDER ( ) . toString ( 'hex' ) ;
signatures . push ( s ) ;
} ) ;
2015-02-13 13:24:35 -08:00
2015-02-13 13:53:49 -08:00
var url = '/v1/txproposals/' + txp . id + '/signatures/' ;
var args = {
signatures : signatures
} ;
var reqSignature = _signRequest ( url , args , data . signingPrivKey ) ;
2015-02-13 17:51:40 -08:00
console . log ( '[clientlib.js.441:reqSignature:]' , url , args , reqSignature ) ; //TODO
2015-02-13 13:53:49 -08:00
2015-02-13 13:24:35 -08:00
request ( {
headers : {
'x-identity' : data . copayerId ,
2015-02-13 13:53:49 -08:00
'x-signature' : reqSignature ,
2015-02-13 13:24:35 -08:00
} ,
method : 'post' ,
url : _getUrl ( url ) ,
body : args ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
return cb ( null , body ) ;
} ) ;
2015-02-13 12:02:56 -08:00
} ;
2015-02-13 17:51:40 -08:00
ClientLib . prototype . reject = function ( txp , reason , cb ) {
var self = this ;
var data = this . _loadAndCheck ( ) ;
var url = '/v1/txproposals/' + txp . id + '/rejections/' ;
var args = {
reason : reason || '' ,
} ;
var reqSignature = _signRequest ( url , args , data . signingPrivKey ) ;
request ( {
headers : {
'x-identity' : data . copayerId ,
'x-signature' : reqSignature ,
} ,
method : 'post' ,
url : _getUrl ( url ) ,
body : args ,
json : true ,
} , function ( err , res , body ) {
if ( err ) return cb ( err ) ;
if ( res . statusCode != 200 ) {
_parseError ( body ) ;
return cb ( 'Request error' ) ;
}
return cb ( null , body ) ;
} ) ;
} ;
2015-02-13 08:35:20 -08:00
2015-02-13 10:49:47 -08:00
module . exports = ClientLib ;