Skip to content

Commit

Permalink
Fix wallet code for latest bitcoinjs-lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
caedesvvv committed Oct 16, 2014
1 parent 81f299e commit 955c95e
Show file tree
Hide file tree
Showing 39 changed files with 290 additions and 283 deletions.
5 changes: 4 additions & 1 deletion html/partials/browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ <h4>Inputs</h4>
<div class="large-6 columns">
<h4>Outputs</h4>
<div class="row" ng-repeat="anOut in tx.outs">
<div class="large-8 columns">
<div ng-if="anOut.notes" class="large-8 columns">
<a tooltip="{{anOut.address}}">{{anOut.notes}}</a>
</div>
<div ng-if="!anOut.notes" class="large-8 columns">
<a ng-safebrowser="anOut.address.toString()">{{anOut.address.toString()}}</a>
</div>
<div class="large-4 columns">
Expand Down
3 changes: 2 additions & 1 deletion html/wallet/tasks.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ <h2>Pending Transaction</h2>
<div class="large-6 columns">
<h3>Destinations</h3>
<div ng-repeat="out in task.tx.outs">
<li>{{out.address.toString()}} <b>{{out.value | currencyPresenter}}</b></li>
<!--<li>{{out.address.toString()}} <b>{{out.value | currencyPresenter}}</b></li>-->
<li><b>{{out.value | currencyPresenter}}</b></li>
</div>
</div>
<h3>Transaction:</h3>
Expand Down
9 changes: 4 additions & 5 deletions js/backend/channels/btcchan.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

define(['util/encryption', 'bitcoinjs-lib', 'backend/channels/utils'],
function (Encryption, Bitcoin, ChannelUtils) {
define(['util/encryption', 'bitcoinjs-lib', 'backend/channels/utils', 'crypto-js'],
function (Encryption, Bitcoin, ChannelUtils, CryptoJS) {

var CryptoJS = Bitcoin.CryptoJS;
var SHA256 = Bitcoin.CryptoJS.SHA256;
var SHA256 = CryptoJS.SHA256;

/************************************
* Channel
Expand Down Expand Up @@ -36,7 +35,7 @@ function (Encryption, Bitcoin, ChannelUtils) {
Channel.prototype.sendOpening = function() {
// Send announcement
var sessionKey = this.transport.getSessionKey();
var pubKeyHash = sessionKey.getPub().toHex(true);
var pubKeyHash = sessionKey.pub.toHex(true);

// Send encrypted
this.channelPostEncrypted(pubKeyHash, function(err, data){
Expand Down
13 changes: 6 additions & 7 deletions js/backend/channels/catchan.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ function (Bitcoin, Curve25519, Encryption, Protocol, Peer, ChannelUtils, Port) {
*/
Channel.prototype.prepareSession = function(sessionKey) {
// Set keys
var priv = sessionKey.priv;
var priv = sessionKey.d;
var ecPriv = Encryption.adaptPrivateKey(priv);

this.pub = Curve25519.ecDH(ecPriv);
this.priv = ecPriv;

// Setup peer details
var newMe = new Peer(this.pub.toByteArrayUnsigned());
var newMe = new Peer(this.pub.toBuffer().toJSON().data);

this.comms = newMe;

Expand All @@ -89,8 +89,7 @@ function (Bitcoin, Curve25519, Encryption, Protocol, Peer, ChannelUtils, Port) {
Channel.prototype.newSession = function() {
// For now this will get changed in transport
// and propagated back to all channels
var sessionKey = new Bitcoin.ECKey();
sessionKey.compressed = true;
var sessionKey = Bitcoin.ECKey.makeRandom(true);
this.prepareSession(sessionKey);
};

Expand Down Expand Up @@ -238,7 +237,7 @@ function (Bitcoin, Curve25519, Encryption, Protocol, Peer, ChannelUtils, Port) {
channelName: this.name,
otherKey: otherKey,
privKey: this.priv.toByteArrayUnsigned(),
pubKey: this.pub.toByteArrayUnsigned(),
pubKey: this.pub.toBuffer().toJSON().data,
data: data
};
// send
Expand Down Expand Up @@ -406,7 +405,7 @@ function (Bitcoin, Curve25519, Encryption, Protocol, Peer, ChannelUtils, Port) {

Channel.prototype.checkPairMessage = function(decoded) {
var idKey = decoded.body.pub;
var keys = bufToArray(Bitcoin.base58check.decode(idKey.substr(3)).payload);
var keys = bufToArray(Bitcoin.base58check.decode(idKey.substr(3)).slice(1));
var data = decoded.body;
var toCheck = data.address+data.nick+data.pub;

Expand Down Expand Up @@ -440,7 +439,7 @@ function (Bitcoin, Curve25519, Encryption, Protocol, Peer, ChannelUtils, Port) {
identity.contacts.contacts.forEach(function(contact) {
var idKey = contact.findIdentityKey();
if (idKey) {
var keys = bufToArray(Bitcoin.base58check.decode(idKey.data.substr(3)).payload);
var keys = bufToArray(Bitcoin.base58check.decode(idKey.data.substr(3)).slice(1));
var signKey = Bitcoin.convert.bytesToString(keys.slice(32));
if (signKey === decoded.body.pub) {
var toCheck = decoded.body.ephem+decoded.body.pub;
Expand Down
1 change: 1 addition & 0 deletions js/backend/channels/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function (Bitcoin, Mnemonic, Encryption) {
if (!pubKey) throw Error("Update with no public key!");

var fingerprint = Encryption.genFingerprint(pubKey);

// Check this is the correct peer (it should be but to be sure..)
if (this.fingerprint && fingerprint != this.fingerprint) {
throw Error("Invalid update for peer!");
Expand Down
15 changes: 6 additions & 9 deletions js/backend/channels/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ function (Bitcoin, Peer, Curve25519, Encryption) {
// Identity (communications) key
var selfKey;
if (identity.store.get('commsKey')) {
selfKey = new Bitcoin.ECKey(identity.store.get('commsKey'));
selfKey = Bitcoin.ECKey.fromBytes(identity.store.get('commsKey'));
}
else {
selfKey = new Bitcoin.ECKey();
selfKey.compressed = true;
selfKey = Bitcoin.ECKey.makeRandom(true);
identity.store.set('commsKey', selfKey.toBytes());
identity.store.save();
}
Expand All @@ -41,16 +40,16 @@ function (Bitcoin, Peer, Curve25519, Encryption) {
this.getSignKey = function() { return {pub: signPubKey, priv: signKey}; };

// Scanning
var scanPriv = Encryption.adaptPrivateKey(this.getSelfKey().priv);
var scanPriv = Encryption.adaptPrivateKey(this.getSelfKey().d);
var scanKeyPub = Curve25519.ecDH(scanPriv);
this.getScanKey = function() { return {priv: scanPriv, pub: scanKeyPub}; };

// Session keys
this.getSessionKey = function() { return this.sessionKey; };

// Initialize some own data
this.comms = new Peer(this.sessionKey.getPub().toBytes(true));
this.myself = new Peer(selfKey.getPub().toBytes(true));
this.comms = new Peer(this.sessionKey.pub.toBytes(true));
this.myself = new Peer(selfKey.pub.toBytes(true));

this.initWorker();
}
Expand Down Expand Up @@ -100,9 +99,7 @@ function (Bitcoin, Peer, Curve25519, Encryption) {
*/
Transport.prototype.newSession = function() {
var self = this;
this.sessionKey = new Bitcoin.ECKey();
this.sessionKey.compressed = true;

this.sessionKey = Bitcoin.ECKey.makeRandom(true);

Object.keys(this.channels).forEach(function(name) {
self.channels[name].prepareSession();
Expand Down
6 changes: 3 additions & 3 deletions js/backend/channels/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

define(['bitcoinjs-lib'],
function (Bitcoin) {
define(['bitcoinjs-lib', 'crypto-js'],
function (Bitcoin, CryptoJS) {

var SHA256 = Bitcoin.CryptoJS.SHA256;
var SHA256 = CryptoJS.SHA256;
var convert = Bitcoin.convert;

// ChannelUtils module
Expand Down
19 changes: 11 additions & 8 deletions js/backend/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ require.config({
'mnemonicjs': "../vendors/mnemonic.js/mnemonic",
'ngProgress': "../vendors/ngprogress/build/ngProgress",
'toaster': "../vendors/AngularJS-Toaster/toaster",
'crypto-js': "../vendors/crypto-js/cryptojs",
'identicon': "../vendors/identicon/identicon",
'pnglib': "../vendors/identicon/pnglib",
'qrcodejs': "../vendors/qrcodejs/qrcode",
'jsqrcode': "../vendors/jsqrcode/jsqrcode",
'async': "../vendors/async/lib/async",
'convert': "../vendors/wrappers/convert",
'crypto-js': "../vendors/wrappers/cryptojs",
'convert': "util/convert",
'bitcoinjs-lib': "../vendors/wrappers/bitcoinjs",
'big': "../vendors/big.js/big.min",

'bitcoinjs-lib': "../vendors/bitcoinjs-lib/bitcoinjs",
'bitcoinjs-lib-real': "../vendors/bitcoinjs-lib/bitcoinjs",
'sjcl-real': "../vendors/sjcl/sjcl",
'darkwallet_gateway': "../vendors/darkwallet_gateway/client/gateway",

Expand Down Expand Up @@ -66,11 +67,9 @@ require.config({
exports: 'QRCode'
},
'crypto-js': {
deps: ['bitcoinjs-lib'],
exports: 'CryptoJS'
},
'convert': {
deps: ['bitcoinjs-lib'],
exports: 'Convert'
},
'jsqrcode': {
Expand All @@ -89,11 +88,15 @@ require.config({
'sjcl-real': {
exports: 'sjcl'
},
'bitcoinjs-lib': {
exports: 'Bitcoin'
},
'sjcl': {
deps: ['sjcl-real']
},
'bitcoinjs-lib-real': {
exports: 'Bitcoin'
},
'bitcoinjs-lib': {
deps: ['bitcoinjs-lib-real', 'convert'],
exports: 'Bitcoin'
}
}
});
21 changes: 10 additions & 11 deletions js/backend/services/mixer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

define(['backend/port', 'backend/channels/catchan', 'util/protocol', 'bitcoinjs-lib', 'util/coinjoin', 'util/btc', 'sjcl'],
function(Port, Channel, Protocol, Bitcoin, CoinJoin, BtcUtils) {
define(['backend/port', 'backend/channels/catchan', 'util/protocol', 'bitcoinjs-lib', 'util/coinjoin', 'util/btc', 'crypto-js', 'sjcl'],
function(Port, Channel, Protocol, Bitcoin, CoinJoin, BtcUtils, CryptoJS) {

/*
* Service managing mixing.
Expand Down Expand Up @@ -164,10 +164,9 @@ function(Port, Channel, Protocol, Bitcoin, CoinJoin, BtcUtils) {
// Now do stuff with the task...
switch(task.state) {
case 'announce':
var id = Bitcoin.CryptoJS.SHA256(Math.random()+'').toString();
var id = CryptoJS.SHA256(Math.random()+'').toString();
console.log("[mixer] Announce join");
var myTx = new Bitcoin.Transaction(task.tx);
myTx = BtcUtils.fixTxVersions(myTx, this.core.getCurrentIdentity());
var myTx = Bitcoin.Transaction.fromHex(task.tx);
if (!task.timeout) {
task.timeout = 60;
}
Expand Down Expand Up @@ -261,7 +260,7 @@ function(Port, Channel, Protocol, Bitcoin, CoinJoin, BtcUtils) {

// Build the tx
var metadata = identity.tx.prepare(pocketIndex, [recipient], changeAddress, fee);
var guestTx = BtcUtils.fixTxVersions(metadata.tx.clone(), identity);
var guestTx = metadata.tx.clone();
this.ongoing[opening.id] = new CoinJoin(this.core, 'guest', 'accepted', guestTx, opening.amount, fee, peer);
this.ongoing[opening.id].pocket = pocketIndex;

Expand All @@ -272,7 +271,7 @@ function(Port, Channel, Protocol, Bitcoin, CoinJoin, BtcUtils) {

MixerService.prototype.sendTo = function(peer, id, tx, callback) {
// Now create and send the message
var msg = Protocol.CoinJoinMsg(id, tx.serializeHex());
var msg = Protocol.CoinJoinMsg(id, tx.toHex());
this.channel.postDH(peer.pubKey, msg, function(err, data) {
callback ? callback(err, data) : null;
});
Expand Down Expand Up @@ -325,14 +324,14 @@ function(Port, Channel, Protocol, Bitcoin, CoinJoin, BtcUtils) {

// Load master keys for the pockets
var pocket = identity.wallet.pockets.hdPockets[pocketIndex];
var masterKey = Bitcoin.HDWallet.fromBase58(sjcl.decrypt(password, pocket.privKey));
var changeKey = Bitcoin.HDWallet.fromBase58(sjcl.decrypt(password, pocket.privChangeKey));
var masterKey = Bitcoin.HDNode.fromBase58(sjcl.decrypt(password, pocket.privKey));
var changeKey = Bitcoin.HDNode.fromBase58(sjcl.decrypt(password, pocket.privChangeKey));

// Iterate over tx inputs and load private keys
var privKeys = {};
for(var i=0; i<coinJoin.myTx.ins.length; i++) {
var anIn = coinJoin.myTx.ins[i];
var output = identity.wallet.wallet.outputs[anIn.outpoint.hash+":"+anIn.outpoint.index];
var output = identity.wallet.wallet.outputs[Bitcoin.bufferutils.reverse(anIn.hash).toString('hex')+":"+anIn.index];
// we're only adding keyhash inputs for now
if (!output) {
throw new Error("Invalid input in our join (no output)");
Expand Down Expand Up @@ -427,7 +426,7 @@ function(Port, Channel, Protocol, Bitcoin, CoinJoin, BtcUtils) {
console.log("broadcasting!", _error, _data);
};
var walletService = this.core.service.wallet;
coinJoin.task.tx = coinJoin.tx.serializeHex();
coinJoin.task.tx = coinJoin.tx.toHex();
walletService.broadcastTx(coinJoin.tx, coinJoin.task, onBroadcast);
}
// Update budget (only guest applies budgeting)
Expand Down
2 changes: 1 addition & 1 deletion js/backend/services/multisig_track.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ define(['backend/port', 'util/protocol', 'util/btc', 'dwutil/multisig', 'bitcoin
*/
MultisigTrackService.prototype.sign = function(multisig, tx, signature) {
var task = this.prepareTask({}, multisig);
task.hash = convert.bytesToHex(tx.getHash());
task.hash = tx.getId();
task.signature = convert.bytesToHex(signature);

// Add the task
Expand Down
1 change: 0 additions & 1 deletion js/backend/services/stealth.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ define(['backend/port'], function(Port) {
stealthJobs[stealthJobIndex] = {type: 'process', cb: cb, nResults: results.length}

stealthJobIndex += 1;

stealthWorker.postMessage(request);
}
});
Expand Down
16 changes: 8 additions & 8 deletions js/backend/services/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function(IdentityKeyRing, Port, CurrencyFormatting, TransactionTasks, Bitcoin, B
*/
this.mixTransaction = function(newTx, metadata, password, callback) {
var identity = self.getCurrentIdentity();
var task = {tx: newTx.serializeHex(),
var task = {tx: newTx.toHex(),
state: 'announce',
total: metadata.total,
label: metadata.label,
Expand Down Expand Up @@ -383,7 +383,7 @@ function(IdentityKeyRing, Port, CurrencyFormatting, TransactionTasks, Bitcoin, B

this.signTransaction(newTx.clone(), metadata, password, function(err, signed) {
if (!err && signed.type === 'signed') {
task.fallback = signed.tx.serializeHex();
task.fallback = signed.tx.toHex();
// Callback for calling process
callback(null, {task: task, tx: newTx, type: 'mixer', privKeys: privKeys});
} else {
Expand All @@ -406,7 +406,7 @@ function(IdentityKeyRing, Port, CurrencyFormatting, TransactionTasks, Bitcoin, B
task.state = 'finished';
task.progress = 100;

var hash = Bitcoin.convert.bytesToHex(tx.getHash());
var hash = tx.getId();
var spendTask = TransactionTasks.processSpend(hash, task.total, task.recipients, task.label);
core.service.badge.setItems(self.getCurrentIdentity());
self.broadcastTx(tx, spendTask, function(err, data) {console.log(err,data);});
Expand All @@ -427,11 +427,11 @@ function(IdentityKeyRing, Port, CurrencyFormatting, TransactionTasks, Bitcoin, B
callback(err);
} else if (pending.length) {
// If pending signatures add task and callback with 2nd parameter
var task = core.service.multisigTrack.spend(newTx.serializeHex(), pending);
var task = core.service.multisigTrack.spend(newTx.toHex(), pending);
callback(null, {task: task, tx: newTx, type: 'signatures'});
} else if (broadcast) {
// Broadcast and add task
var txHash = Bitcoin.convert.bytesToHex(newTx.getHash());
var txHash = newTx.getId();
if (metadata.label) {
identity.txdb.setLabel(txHash, metadata.label);
}
Expand All @@ -450,10 +450,10 @@ function(IdentityKeyRing, Port, CurrencyFormatting, TransactionTasks, Bitcoin, B
*/
this.broadcastTx = function(newTx, task, callback) {
// Broadcasting
var serialized = newTx.serializeHex();
var serialized = newTx.toHex();

if (task.label) {
var hash = Bitcoin.convert.bytesToHex(newTx.getHash());
var hash = newTx.getId();
core.getCurrentIdentity().txdb.setLabel(hash, task.label);
}
var notifyTx = function(error, count) {
Expand All @@ -474,7 +474,7 @@ function(IdentityKeyRing, Port, CurrencyFormatting, TransactionTasks, Bitcoin, B
console.log("send tx", serialized);
var identity = self.getCurrentIdentity();
identity.tx.process(serialized, 0);
core.getClient().broadcast_transaction(newTx.serializeHex(), notifyTx);
core.getClient().broadcast_transaction(newTx.toHex(), notifyTx);
};
}
return WalletService;
Expand Down
4 changes: 2 additions & 2 deletions js/backend/workers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require(['bitcoinjs-lib', 'util/djbec', 'util/encryption', 'sjcl'], function(Bit
var decryptBeacon = function(scanKey, data, pk2) {
var scanKeyPriv = BigInteger.fromByteArrayUnsigned(scanKey.priv);
var shared = Curve25519.ecDH(scanKeyPriv, pk2);
shared = shared.toByteArrayUnsigned();
shared = shared.toBuffer().toJSON().data;
shared = Curve25519.bytes2string(shared);
return sjcl.decrypt(shared, data.data);
}
Expand Down Expand Up @@ -70,7 +70,7 @@ require(['bitcoinjs-lib', 'util/djbec', 'util/encryption', 'sjcl'], function(Bit
var pk2 = BigInteger.fromByteArrayUnsigned(otherKey);
var shared = Curve25519.ecDH(channelPriv, pk2);

shared = shared.toByteArrayUnsigned();
shared = shared.toBuffer().toJSON().data;
shared = Curve25519.bytes2string(shared);

// First decrypt with given dh secret for our cloak
Expand Down
2 changes: 1 addition & 1 deletion js/backend/workers/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ require(['bitcoinjs-lib', 'util/djbec', 'util/encryption', 'util/protocol', 'sjc
var shared = Curve25519.ecDH(myPrivKey, pk2);
data.pubKey = myPub;

shared = shared.toByteArrayUnsigned();
shared = shared.toBuffer().toJSON().data;
shared = Curve25519.bytes2string(shared);

var encrypted = sjcl.encrypt(shared, JSON.stringify(data), {ks: 256, ts: 128});
Expand Down

0 comments on commit 955c95e

Please sign in to comment.