Wallet Class WIP
This commit is contained in:
parent
4546c54c8b
commit
296c78cdf0
10 changed files with 8152 additions and 249 deletions
115
js/models/blockchain/Insight.js
Normal file
115
js/models/blockchain/Insight.js
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
'use strict';
|
||||
|
||||
var imports = require('soop').imports();
|
||||
var bitcore = require('bitcore');
|
||||
var http = require('http');
|
||||
|
||||
function Insight(opts) {
|
||||
opts = opts || {};
|
||||
this.host = 'localhost';
|
||||
this.port = '3001';
|
||||
}
|
||||
|
||||
function _asyncForEach(array, fn, callback) {
|
||||
array = array.slice(0);
|
||||
function processOne() {
|
||||
var item = array.pop();
|
||||
fn(item, function(result) {
|
||||
if(array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
}
|
||||
});
|
||||
}
|
||||
if(array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
}
|
||||
};
|
||||
|
||||
Insight.prototype.getBalance = function(unspent) {
|
||||
var balance = 0;
|
||||
for(var i=0;i<unspent.length; i++) {
|
||||
balance = balance + unspent[i].amount;
|
||||
}
|
||||
|
||||
return balance;
|
||||
};
|
||||
|
||||
Insight.prototype.listUnspent = function(addresses, cb) {
|
||||
var self = this;
|
||||
|
||||
if (!addresses || !addresses.length) return cb();
|
||||
|
||||
var all = [];
|
||||
|
||||
_asyncForEach(addresses, function(addr, callback) {
|
||||
var options = {
|
||||
host: self.host,
|
||||
port: self.port,
|
||||
method: 'GET',
|
||||
path: '/api/addr/' + addr + '/utxo'
|
||||
};
|
||||
|
||||
self._request(options, function(err, res) {
|
||||
if (res && res.length > 0) {
|
||||
all = all.concat(res);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}, function() {
|
||||
return cb(all);
|
||||
});
|
||||
};
|
||||
|
||||
Insight.prototype.sendRawTransaction = function(rawtx, cb) {
|
||||
if (!rawtx) return callback();
|
||||
|
||||
var options = {
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
method: 'POST',
|
||||
path: '/api/tx/send',
|
||||
data: 'rawtx='+rawtx,
|
||||
headers: { 'content-type' : 'application/x-www-form-urlencoded' }
|
||||
};
|
||||
|
||||
this._request(options, function(err,res) {
|
||||
if (err) return cb();
|
||||
return cb(res.txid);
|
||||
});
|
||||
};
|
||||
|
||||
Insight.prototype._request = function(options, callback) {
|
||||
var req = http.request(options, function(response) {
|
||||
var ret;
|
||||
if (response.statusCode == 200) {
|
||||
response.on('data', function(chunk) {
|
||||
try {
|
||||
ret = JSON.parse(chunk);
|
||||
} catch (e) {
|
||||
callback({message: "Wrong response from insight"});
|
||||
return;
|
||||
}
|
||||
});
|
||||
response.on('end', function () {
|
||||
callback(undefined, ret);
|
||||
return;
|
||||
});
|
||||
}
|
||||
else {
|
||||
callback({message: 'Error ' + response.statusCode});
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (options.data) {
|
||||
req.write(options.data);
|
||||
}
|
||||
req.end();
|
||||
}
|
||||
|
||||
|
||||
module.exports = require('soop')(Insight);
|
||||
|
||||
|
|
@ -1,114 +1,78 @@
|
|||
'use strict';
|
||||
|
||||
var imports = require('soop').imports();
|
||||
|
||||
var bitcore = require('bitcore');
|
||||
var http = require('http');
|
||||
|
||||
function Wallet(opts) {
|
||||
var Storage = imports.Storage || require('FakeStorage');
|
||||
var Network = imports.Network || require('FakeNetwork');
|
||||
var Blockchain = imports.Blockchain || require('FakeBlockchain');
|
||||
|
||||
var copay = copay || require('../../../copay');
|
||||
|
||||
|
||||
function Wallet(opts, config) {
|
||||
opts = opts || {};
|
||||
this.host = 'localhost';
|
||||
this.port = '3001';
|
||||
}
|
||||
|
||||
function asyncForEach(array, fn, callback) {
|
||||
array = array.slice(0);
|
||||
function processOne() {
|
||||
var item = array.pop();
|
||||
fn(item, function(result) {
|
||||
if(array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
}
|
||||
});
|
||||
}
|
||||
if(array.length > 0) {
|
||||
setTimeout(processOne, 0); // schedule immediately
|
||||
} else {
|
||||
callback(); // Done!
|
||||
}
|
||||
};
|
||||
console.log('### CREATING WALLET.'
|
||||
+ (opts.walletId ? ' USING ID: ' +opts.walletId : ' NEW ID') );
|
||||
|
||||
Wallet.prototype.getBalance = function(unspent) {
|
||||
var balance = 0;
|
||||
for(var i=0;i<unspent.length; i++) {
|
||||
balance = balance + unspent[i].amount;
|
||||
}
|
||||
//
|
||||
this.storage = new Storage(config.storage);
|
||||
this.network = new Network(config.network);
|
||||
this.blockchain = new Blockchain(config.blockchain);
|
||||
|
||||
return balance;
|
||||
};
|
||||
|
||||
Wallet.prototype.listUnspent = function(addresses, cb) {
|
||||
var self = this;
|
||||
this.privateKey = new copay.PrivateKey({networkName: config.networkName});
|
||||
console.log('\t### PrivateKey Initialized');
|
||||
|
||||
if (!addresses || !addresses.length) return cb();
|
||||
|
||||
var all = [];
|
||||
|
||||
asyncForEach(addresses, function(addr, callback) {
|
||||
var options = {
|
||||
host: self.host,
|
||||
port: self.port,
|
||||
method: 'GET',
|
||||
path: '/api/addr/' + addr + '/utxo'
|
||||
};
|
||||
|
||||
self.request(options, function(err, res) {
|
||||
if (res && res.length > 0) {
|
||||
all = all.concat(res);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}, function() {
|
||||
return cb(all);
|
||||
|
||||
this.publicKeyRing = opts.publicKeyRing || new copay.PublicKeyRing({
|
||||
id: opts.walletId,
|
||||
requiredCopayers: opts.requiredCopayers || config.wallet.requiredCopayers,
|
||||
totalCopayers: opts.totalCopayers || config.wallet.totalCopayers,
|
||||
networkName: config.networkName,
|
||||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.sendRawTransaction = function(rawtx, cb) {
|
||||
if (!rawtx) return callback();
|
||||
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
|
||||
console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.id);
|
||||
|
||||
var options = {
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
method: 'POST',
|
||||
path: '/api/tx/send',
|
||||
data: 'rawtx='+rawtx,
|
||||
headers: { 'content-type' : 'application/x-www-form-urlencoded' }
|
||||
};
|
||||
|
||||
this.request(options, function(err,res) {
|
||||
if (err) return cb();
|
||||
return cb(res.txid);
|
||||
this.txProposals = new copay.TxProposals({
|
||||
walletId: this.publicKeyRing.id,
|
||||
publicKeyRing: this.publicKeyRing,
|
||||
networkName: config.networkName,
|
||||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.request = function(options, callback) {
|
||||
var req = http.request(options, function(response) {
|
||||
var ret;
|
||||
if (response.statusCode == 200) {
|
||||
response.on('data', function(chunk) {
|
||||
try {
|
||||
ret = JSON.parse(chunk);
|
||||
} catch (e) {
|
||||
callback({message: "Wrong response from insight"});
|
||||
return;
|
||||
}
|
||||
});
|
||||
response.on('end', function () {
|
||||
callback(undefined, ret);
|
||||
return;
|
||||
});
|
||||
}
|
||||
else {
|
||||
callback({message: 'Error ' + response.statusCode});
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (options.data) {
|
||||
req.write(options.data);
|
||||
}
|
||||
req.end();
|
||||
console.log('\t### TxProposals Initialized');
|
||||
}
|
||||
//
|
||||
// var Read = function(walletId) {
|
||||
// this.storage.read(walletId);
|
||||
// $rootScope.w = new copay.PublicKeyRing.fromObj(pkr);
|
||||
// $rootScope.txProposals = new copay.TxProposals.fromObj(txp);
|
||||
// $rootScope.PrivateKey = new copay.PrivateKey.fromObj(priv); //TODO secure
|
||||
//
|
||||
//
|
||||
|
||||
// HERE or in Storage?
|
||||
// $rootScope.walletId = walletId;
|
||||
// $rootScope.w = new copay.PublicKeyRing.fromObj(pkr);
|
||||
// $rootScope.txProposals = new copay.TxProposals.fromObj(txp);
|
||||
// $rootScope.PrivateKey = new copay.PrivateKey.fromObj(priv); //TODO secure
|
||||
|
||||
// // JIC: Add our key
|
||||
// try {
|
||||
// $rootScope.publicKeyRing.addCopayer(
|
||||
// $rootScope.PrivateKey.getBIP32().extendedPublicKeyString()
|
||||
// );
|
||||
// } catch (e) {
|
||||
// console.log('NOT NECCESARY AN ERROR:', e); //TODO
|
||||
// };
|
||||
// ret = true;
|
||||
// }
|
||||
// return ret;
|
||||
// };
|
||||
// };
|
||||
|
||||
|
||||
module.exports = require('soop')(Wallet);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue