Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/bitgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ BitGo.prototype.decrypt = function(password, opaque) {
return sjcl.decrypt(password, opaque);
};

BitGo.prototype.url = function(path) {
return this._baseUrl + path;
};

//
// market
// Get the latest bitcoin prices.
Expand All @@ -102,8 +106,7 @@ BitGo.prototype.market = function(callback) {
throw new Error('invalid argument');
}

var url = this._baseUrl + '/market/latest';
this.get(url)
this.get(this.url('/market/latest'))
.end(function(err, res) {
if (err) {
return callback(err);
Expand All @@ -127,13 +130,11 @@ BitGo.prototype.authenticate = function(username, password, otp, callback) {
}

var self = this;
var url = this._baseUrl + '/user/login';

if (this._user) {
return callback(new Error('already logged in'));
}

this.post(url)
this.post(this.url('/user/login'))
.send({email: username, password: password, otp: otp})
.end(function(err, res) {
if (self.handleBitGoAPIError(err, res, callback)) {
Expand All @@ -159,8 +160,7 @@ BitGo.prototype.logout = function(callback) {
}

var self = this;
var url = this._baseUrl + '/user/logout';
this.get(url)
this.get(this.url('/user/login'))
.send()
.end(function(err, res) {
delete self._user;
Expand All @@ -182,9 +182,8 @@ BitGo.prototype.me = function(callback) {
return callback(new Error('not authenticated'));
}

var url = this._baseUrl + '/user/me';
var self = this;
this.get(url)
this.get(this.url('/user/me'))
.send()
.end(function(err, res) {
if (self.handleBitGoAPIError(err, res, callback)) {
Expand Down Expand Up @@ -242,6 +241,6 @@ BitGo.prototype.handleBitGoAPIError = function(err, res, callback) {
var error = res.body.error ? res.body.error : res.status.toString();
callback({status: res.status, error: error, details: new Error(error)});
return true;
}
};

module.exports = BitGo;
42 changes: 25 additions & 17 deletions src/keychains.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Keychains.prototype.isValid = function(string) {
} catch (e) {
return false;
}
}
};

//
// create
Expand Down Expand Up @@ -60,9 +60,7 @@ Keychains.prototype.list = function(callback) {
if (typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/keychains';
this.bitgo.get(url)
this.bitgo.get(this.bitgo.url('/keychains'))
.end(function(err, res) {
if (err) {
return callback(err);
Expand All @@ -79,13 +77,10 @@ Keychains.prototype.add = function(options, callback) {
if (typeof(options) != 'object' || typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/keychains';
this.bitgo.post(url)
this.bitgo.post(this.bitgo.url('/keychains'))
.send({
label: options.label,
xpub: options.xpub,
xprv: options.encryptedXprv
encryptedXprv: options.encryptedXprv
})
.end(function(err, res) {
if (err) {
Expand All @@ -95,6 +90,24 @@ Keychains.prototype.add = function(options, callback) {
});
};

//
// addBitGo
// Add a new BitGo server keychain
//
Keychains.prototype.createBitGo = function(options, callback) {
if (typeof(options) != 'object' || typeof(callback) != 'function') {
throw new Error('invalid argument');
}
this.bitgo.post(this.bitgo.url('/keychains/bitgo'))
.send({})
.end(function(err, res) {
if (err) {
return callback(err);
}
callback(null, res.body);
});
};

//
// get
// Fetch an existing keychain
Expand All @@ -107,10 +120,8 @@ Keychains.prototype.get = function(options, callback) {
typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/keychains/' + options.xpub;
var self = this;
this.bitgo.post(url)
this.bitgo.post(this.bitgo.url('/keychain/' + options.xpub))
.send({
otp: options.otp
})
Expand All @@ -134,13 +145,10 @@ Keychains.prototype.update = function(options, callback) {
typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/keychains/' + options.xpub;
var self = this;
this.bitgo.put(url)
this.bitgo.put(this.bitgo.url('/keychain/' + options.xpub))
.send({
label: options.label,
xprv: options.encryptedXprv, // TODO: This field should be renamed to encryptedXprv
encryptedXprv: options.encryptedXprv,
otp: options.otp
})
.end(function(err, res) {
Expand Down
4 changes: 2 additions & 2 deletions src/transactionbuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var TransactionBuilder = function(wallet, recipient, fee) {
// Use the rough formula of 6 signatures per KB.
var signaturesPerInput = 2; // 2-of-3 wallets
return Math.ceil(_tx.ins.length * signaturesPerInput / 6) * FEE_PER_KB;
}
};

// Iterate _unspents, sum the inputs, and save _inputs with the total
// input amound and final list of inputs to use with the transaction.
Expand Down Expand Up @@ -227,7 +227,7 @@ var TransactionBuilder = function(wallet, recipient, fee) {
//
this.tx = function() {
return Util.bytesToHex(_tx.serialize());
}
};

};

Expand Down
57 changes: 24 additions & 33 deletions src/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ var Wallet = function(bitgo, wallet) {
this.wallet = wallet;
this.keychains = [];
if (wallet.private) {
this.keychains.push(wallet.private.userKeychain);
this.keychains.push(wallet.private.backupKeychain);
this.keychains.push(wallet.private.bitgoKeychain);
this.keychains = wallet.private.keychains;
}
};

Expand All @@ -27,15 +25,15 @@ var Wallet = function(bitgo, wallet) {
//
Wallet.prototype.address = function() {
return this.wallet.id;
}
};

//
// type
// Get the type of this wallet (e.g. 'bitcoin').
//
Wallet.prototype.type = function() {
return this.wallet.type;
}
};


//
Expand All @@ -44,49 +42,49 @@ Wallet.prototype.type = function() {
//
Wallet.prototype.label = function() {
return this.wallet.label;
}
};

//
// balance
// Get the balance of this wallet.
//
Wallet.prototype.balance = function() {
return this.wallet.balance;
}
};

//
// pendingBalance
// Get the pendingBalance of this wallet.
//
Wallet.prototype.pendingBalance = function() {
return this.wallet.pendingBalance;
}
};

//
// availableBalance
// Get the availableBalance of this wallet.
//
Wallet.prototype.availableBalance = function() {
return this.wallet.availableBalance;
}
};

Wallet.prototype.url = function(extra) {
extra = extra || '';
return this.bitgo.url('/wallet/' + this.address() + extra);
};

//
// createAddress
// Creates a new address for use with this wallet.
// Options include:
// internal: a flag if this should be an internal or external chain
//
Wallet.prototype.createAddress = function(options, callback) {
if (typeof(options) != 'object' || typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/address/chain/' + this.type() + '/' + this.address();
var chain = options.chain || 0;
var self = this;
this.bitgo.post(url)
.send({
internal: options.internal
})
this.bitgo.post(this.url('/chain/' + chain))
.send({})
.end(function(err, res) {
if (self.bitgo.handleBitGoAPIError(err, res, callback)) {
return;
Expand All @@ -105,10 +103,8 @@ Wallet.prototype.delete = function(callback) {
if (typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/addresses/' + this.type() + '/' + this.address();
var self = this;
this.bitgo.del(url)
this.bitgo.del(this.url())
.send()
.end(function(err, res) {
if (self.bitgo.handleBitGoAPIError(err, res, callback)) {
Expand All @@ -128,18 +124,18 @@ Wallet.prototype.unspents = function(options, callback) {
if (typeof(options) != 'object' || typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/transactions/unspents/' + this.type() + '/' + this.address();
var url = this.url('/unspents');
if (options.btcLimit) {
if (typeof(options.btcLimit) != 'number') {
if (typeof(options.limit) != 'number') {
throw new Error('invalid argument');
}
url += '?limit=' + (options.btcLimit * 1e8);
url += '?limit=' + (options.limit * 1e8);
}
var self = this;
this.bitgo.get(url)
.send()
.end(function(err, res) {
console.log(res.body);
if (self.bitgo.handleBitGoAPIError(err, res, callback)) {
return;
}
Expand All @@ -156,10 +152,8 @@ Wallet.prototype.transactions = function(options, callback) {
if (typeof(options) != 'object' || typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/transactions/' + this.type() + '/' + this.address();
var self = this;
this.bitgo.get(url)
this.bitgo.get(this.url('/tx'))
.send()
.end(function(err, res) {
if (self.bitgo.handleBitGoAPIError(err, res, callback)) {
Expand Down Expand Up @@ -187,7 +181,6 @@ Wallet.prototype.createTransaction = function(address, amount, fee, keychain, ca
typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var tb = new TransactionBuilder(this, { address: address, amount: amount }, fee);
tb.prepare()
.then(function() {
Expand All @@ -199,7 +192,7 @@ Wallet.prototype.createTransaction = function(address, amount, fee, keychain, ca
.catch(function(e) {
callback(e);
});
}
};

//
// send
Expand All @@ -212,17 +205,15 @@ Wallet.prototype.send = function(tx, callback) {
if (typeof(tx) != 'string' || typeof(callback) != 'function') {
throw new Error('invalid argument');
}

var url = this.bitgo._baseUrl + '/transactions/' + this.type();
var self = this;
this.bitgo.post(url)
this.bitgo.post(this.bitgo.url('/tx/send'))
.send({ tx: tx })
.end(function(err, res) {
if (self.bitgo.handleBitGoAPIError(err, res, callback)) {
return;
}
callback(null, { tx: res.body.transaction, hash: res.body.transactionHash });
});
}
};

module.exports = Wallet;
Loading