bitcore-payment-codes-cash
Version:
Payment Codes (BIP47) support for Bitcoin Cash
67 lines (51 loc) • 2.06 kB
JavaScript
;
var bitcore = require('bitcore-lib-cash');
var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var Transaction = bitcore.Transaction;
var BufferUtil = bitcore.util.buffer;
var PublicKey = bitcore.PublicKey;
var PrivateKey = bitcore.PrivateKey;
var Script = bitcore.Script;
var Point = bitcore.crypto.Point;
var PC = require ('../');
/* Creates a outgoint notification: Alice (sender) want to pay Bob (recipient)
* this class is for Alice to create a notification to Bob, using his scanner
* PaymentCode, her own payment code, the UTXO she is planning to use for the
* notification's TX source,
*
* Seehttps://github.com/justusranvier/bips/blob/760f19c346fe9f12e8e5cce0c809369e2a6f7543/bip-0047.mediawiki#notification-transaction
* */
function NotificationOut(senderPc, recipientPc, utxoPrivateKey, outpoint) {
$.checkArgument(utxoPrivateKey, "utxoPrivateKey key must be supplied");
outpoint = outpoint || 0;
var self = this;
var privateKey = new PrivateKey(utxoPrivateKey);
var ourPc = new PC.PaymentCode(senderPc);
var hisPc = new PC.PaymentCode(recipientPc);
var publicKeys = [];
var secrets = [];
var outputs = [];
// i. Alice selects the private key corresponding to the designated pubkey:
var a = privateKey.bn;
_.each(hisPc.xPubKeys, function(xpub) {
var pubKey =xpub.derive('m/0').publicKey;
// ii. Alice selects the public key associated with Bob's notification address:
var B = pubKey.point;
// iii. Alice calculates a secret point:
var secret = new PC.Secret(B.mul(a));
// iv. Alice calculates a 64 byte blinding factor:
var bf = new PC.BlindingFactor(secret, outpoint);
var output = new Buffer(ourPc.buffer);
bf.apply(output);
secrets.push(secret);
outputs.push(output);
});
this.secrets = secrets;
this.outputs = outputs
this.utxoPublicKey = utxoPrivateKey.publicKey;
};
NotificationOut.prototype.getScriptPubKey = function() {
return Script.buildPublicKeyHashOut(this.utxoPublicKey);
};
module.exports = NotificationOut;