Ivan Socolsky
10 years ago
12 changed files with 736 additions and 1190 deletions
@ -0,0 +1,46 @@ |
|||
'use strict'; |
|||
|
|||
var _ = require('lodash'); |
|||
var $ = require('preconditions').singleton(); |
|||
var util = require('util'); |
|||
var async = require('async'); |
|||
var log = require('npmlog'); |
|||
var events = require('events'); |
|||
log.debug = log.verbose; |
|||
var Bitcore = require('bitcore') |
|||
|
|||
var Credentials = require('./credentials'); |
|||
var WalletUtils = require('../walletutils'); |
|||
var Verifier = require('./verifier'); |
|||
var ServerCompromisedError = require('./servercompromisederror'); |
|||
var ClientError = require('../clienterror'); |
|||
|
|||
function AirGapped(opts) { |
|||
this.verbose = !!opts.verbose; |
|||
if (this.verbose) { |
|||
log.level = 'debug'; |
|||
} else { |
|||
log.level = 'info'; |
|||
} |
|||
this.credentials = Credentials.create(opts.network || 'livenet'); |
|||
}; |
|||
|
|||
util.inherits(AirGapped, events.EventEmitter); |
|||
|
|||
AirGapped.prototype.getSeed = function() { |
|||
var cred = this.credentials; |
|||
|
|||
return { |
|||
network: cred.network, |
|||
xPubKey: cred.xPubKey, |
|||
requestPrivKey: cred.requestPrivKey, |
|||
}; |
|||
}; |
|||
|
|||
AirGapped.prototype.signTxProposals = function(txps, cb) { |
|||
return cb(null, _.map(txps, function(txp) { |
|||
return {}; |
|||
})); |
|||
}; |
|||
|
|||
module.exports = AirGapped; |
File diff suppressed because it is too large
@ -0,0 +1,110 @@ |
|||
'use strict'; |
|||
|
|||
var $ = require('preconditions').singleton(); |
|||
var _ = require('lodash'); |
|||
var Bitcore = require('bitcore'); |
|||
var WalletUtils = require('../walletutils'); |
|||
|
|||
var FIELDS = [ |
|||
'network', |
|||
'xPrivKey', |
|||
'xPubKey', |
|||
// 'roPrivKey',
|
|||
'requestPrivKey', |
|||
'copayerId', |
|||
'publicKeyRing', |
|||
'walletId', |
|||
'walletName', |
|||
'm', |
|||
'n', |
|||
'walletPrivKey', |
|||
'sharedEncryptingKey', |
|||
'copayerName', |
|||
]; |
|||
|
|||
function Credentials() { |
|||
this.version = '1.0.0'; |
|||
}; |
|||
|
|||
Credentials.create = function(network) { |
|||
var x = new Credentials(); |
|||
|
|||
x.network = network; |
|||
x.xPrivKey = (new Bitcore.HDPrivateKey(network)).toString(); |
|||
x._expand(); |
|||
return x; |
|||
}; |
|||
|
|||
Credentials.fromExtendedPrivateKey = function(network, xPrivKey) { |
|||
var x = new Credentials(); |
|||
x.network = network; |
|||
x.xPrivKey = xPrivKey; |
|||
x._expand(); |
|||
return x; |
|||
}; |
|||
|
|||
Credentials.fromAirGapped = function(network, xPubKey, requestPrivKey) { |
|||
var x = new Credentials(); |
|||
x.network = network; |
|||
x.xPubKey = xPubKey; |
|||
x.requestPrivKey = requestPrivKey; |
|||
x._expand(); |
|||
return x; |
|||
}; |
|||
|
|||
Credentials.prototype._expand = function() { |
|||
$.checkState(this.xPrivKey || this.xPubKey); |
|||
|
|||
if (this.xPrivKey) { |
|||
var xPrivKey = new Bitcore.HDPrivateKey.fromString(this.xPrivKey); |
|||
this.xPubKey = (new Bitcore.HDPublicKey(xPrivKey)).toString(); |
|||
// this.roPrivKey = xPrivKey.derive('m/1/0').privateKey.toString();
|
|||
this.requestPrivKey = xPrivKey.derive('m/1/1').privateKey.toString(); |
|||
} |
|||
this.copayerId = WalletUtils.xPubToCopayerId(this.xPubKey); |
|||
}; |
|||
|
|||
Credentials.fromObj = function(obj) { |
|||
var x = new Credentials(); |
|||
|
|||
_.each(FIELDS, function(k) { |
|||
x[k] = obj[k]; |
|||
}); |
|||
|
|||
return x; |
|||
}; |
|||
|
|||
Credentials.prototype.toObj = function() { |
|||
return this; |
|||
}; |
|||
|
|||
Credentials.prototype.addWalletInfo = function(walletId, walletName, m, n, walletPrivKey, copayerName) { |
|||
this.walletId = walletId; |
|||
this.walletName = walletName; |
|||
this.m = m; |
|||
this.n = n; |
|||
this.walletPrivKey = walletPrivKey; |
|||
this.sharedEncryptingKey = WalletUtils.privateKeyToAESKey(walletPrivKey); |
|||
this.copayerName = copayerName; |
|||
if (n == 1) { |
|||
this.addPublicKeyRing([this.xPubKey]); |
|||
} |
|||
}; |
|||
|
|||
Credentials.prototype.addPublicKeyRing = function(publicKeyRing) { |
|||
this.publicKeyRing = _.clone(publicKeyRing); |
|||
}; |
|||
|
|||
Credentials.prototype.canSign = function() { |
|||
return !!this.xPrivKey; |
|||
}; |
|||
|
|||
Credentials.prototype.isComplete = function() { |
|||
if (!this.walletId) return false; |
|||
if (!this.publicKeyRing || this.publicKeyRing.length != this.n) return false; |
|||
return true; |
|||
}; |
|||
|
|||
|
|||
|
|||
module.exports = Credentials; |
@ -1,33 +0,0 @@ |
|||
|
|||
var fs = require('fs') |
|||
|
|||
function FileStorage(opts) { |
|||
if (!opts.filename) { |
|||
throw new Error('Please set wallet filename'); |
|||
} |
|||
this.filename = opts.filename; |
|||
this.fs = opts.fs || fs; |
|||
}; |
|||
|
|||
FileStorage.prototype.getName = function() { |
|||
return this.filename; |
|||
}; |
|||
|
|||
FileStorage.prototype.save = function(data, cb) { |
|||
this.fs.writeFile(this.filename, JSON.stringify(data), cb); |
|||
}; |
|||
|
|||
FileStorage.prototype.load = function(cb) { |
|||
this.fs.readFile(this.filename, 'utf8', function(err,data) { |
|||
if (err) return cb(err); |
|||
try { |
|||
data = JSON.parse(data); |
|||
} catch (e) { |
|||
} |
|||
return cb(null, data); |
|||
}); |
|||
}; |
|||
|
|||
|
|||
module.exports = FileStorage; |
|||
|
@ -1,5 +1,3 @@ |
|||
//var client = ;
|
|||
|
|||
var client = module.exports = require('./api'); |
|||
client.FileStorage = require('./filestorage'); |
|||
client.Verifier = require('./verifier'); |
|||
client.AirGapped = require('./airgapped'); |
|||
|
File diff suppressed because it is too large
Loading…
Reference in new issue