Skip to content

Commit

Permalink
feat: create receiver wallet for easier support sending to counterpar…
Browse files Browse the repository at this point in the history
…ty's segwit addresses
  • Loading branch information
Overtorment committed Apr 26, 2024
1 parent 0a2f02c commit f4b8047
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/bip47.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ function BIP47Factory(ecc) {
const prvKey = uintArrayToBuffer(prvKeyUint8);
return bip32.fromPrivateKey(prvKey, bobNode.chainCode, this.network.network);
}
getReceiveWallet(bobNode, index) {
if (!this.network || !this.RootPaymentCodeNode)
throw new Error('Root Payment code node or network not set');
const aliceNode = this.RootPaymentCodeNode.derive(0);
bobNode = bobNode.derive(index);
if (aliceNode.privateKey === undefined)
throw new Error('Missing private key to generate receive wallets');
const s = getSharedSecret(bobNode.publicKey, aliceNode.privateKey);
const sG = ecc.pointMultiply(G, s, true);
if (sG === null)
throw new Error('Could not calculate private key');
const pubKeyUint8 = ecc.pointAdd(bobNode.publicKey, sG);
if (pubKeyUint8 === null)
throw new Error('Could not sum pub keys');
const pubKey = uintArrayToBuffer(pubKeyUint8);
return bip32.fromPublicKey(pubKey, bobNode.chainCode, this.network.network);
}
getPaymentCodeNode() {
return this.RootPaymentCodeNode;
}
Expand Down
29 changes: 29 additions & 0 deletions ts-src/bip47.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ export function BIP47Factory(ecc: TinySecp256k1Interface): BIP47API {
);
}

getReceiveWallet(bobNode: BIP32Interface, index: number): BIP32Interface {
if (!this.network || !this.RootPaymentCodeNode)
throw new Error('Root Payment code node or network not set');

const aliceNode = this.RootPaymentCodeNode.derive(0);

bobNode = bobNode.derive(index);

if (aliceNode.privateKey === undefined)
throw new Error('Missing private key to generate receive wallets');

const s = getSharedSecret(bobNode.publicKey, aliceNode.privateKey);

const sG = ecc.pointMultiply(G, s, true);

if (sG === null) throw new Error('Could not calculate private key');

const pubKeyUint8 = ecc.pointAdd(bobNode.publicKey, sG);

if (pubKeyUint8 === null) throw new Error('Could not sum pub keys');

const pubKey = uintArrayToBuffer(pubKeyUint8);
return bip32.fromPublicKey(
pubKey,
bobNode.chainCode,
this.network.network,
);
}

getPaymentCodeNode(): BIP32Interface {
return this.RootPaymentCodeNode as BIP32Interface;
}
Expand Down
5 changes: 5 additions & 0 deletions ts-src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export interface BIP47Interface {
index: number,
): BIP32Interface;

getReceiveWallet(
bobsRootPaymentCodeNode: BIP32Interface,
index: number,
): BIP32Interface;

getPaymentCodeNode(): BIP32Interface;

getPaymentAddress(
Expand Down
1 change: 1 addition & 0 deletions types/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface BIP47Interface {
network: NetworkCoin;
RootPaymentCodeNode: BIP32Interface;
getPaymentWallet(bobsRootPaymentCodeNode: BIP32Interface, index: number): BIP32Interface;
getReceiveWallet(bobsRootPaymentCodeNode: BIP32Interface, index: number): BIP32Interface;
getPaymentCodeNode(): BIP32Interface;
getPaymentAddress(bobsRootPaymentCodeNode: BIP32Interface, index: number): string;
getSerializedPaymentCode(): string;
Expand Down

0 comments on commit f4b8047

Please sign in to comment.