Commit f678b33d authored by kenshin-samourai's avatar kenshin-samourai
Browse files

add derivation of private keys used for payments

parent 70bdde44
......@@ -83,6 +83,22 @@ class PaymentCode {
return getP2pkhAddress(child.publicKey, this.network);
}
derivePaymentPrivateKey(A, idx) {
if (!ecc.isPoint(A))
throw new TypeError('Argument is not a valid public key');
const b_node = this.derive(idx);
const b = b_node.privateKey;
const S = ecc.pointMultiply(A, b);
const Sx = S.slice(1, 33);
const s = sha256(Sx);
if (!ecc.isPrivate(s))
throw new TypeError('Invalid shared secret');
return ecc.privateAdd(b, s);
}
derivePaymentPublicKey(a, idx) {
if (!ecc.isPrivate(a) && !ecc.isPoint(a))
throw new TypeError('Argument is neither a valid private key or public key');
......
'use strict'
const assert = require('assert')
const ecc = require('tiny-secp256k1');
const bip47 = require('../src')
const utils = bip47.utils
......@@ -167,6 +168,27 @@ describe('payment-code', function() {
});
});
describe('PaymentCode.derivePaymentPrivateKey()', function() {
it('should successfully derive private keys from a payment code and a notif pubkey', function() {
try {
const pubkey1 = Buffer.from(PC_1.notifPubKey, 'hex');
const seed = Buffer.from(PC_2.seed, 'hex');
const pc2 = bip47.fromWalletSeed(seed, 0);
for (let i=0; i<10; i++) {
const privkeyPayment = pc2.derivePaymentPrivateKey(pubkey1, i);
const strPubkeyPayment = ecc.pointFromScalar(privkeyPayment).toString('hex');
const strPubkeyPayment2 = pc2.derivePaymentPublicKey(pubkey1, i).toString('hex');
if (strPubkeyPayment !== strPubkeyPayment2)
assert(false);
}
assert(true);
} catch(e) {
console.log(e)
assert(false);
}
});
});
describe('PaymentCode.getPaymentAddress()', function() {
it('should successfully derive P2PKH addresses from a payment code and a notif privkey', function() {
try {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment