Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "silent-payments",
"version": "2.2.0",
"version": "2.2.1",
"description": "SilentPayments (BIP-352) in pure typescript",
"main": "src/index.ts",
"scripts": {
"tslint": "tsc src/index.ts --noEmit --skipLibCheck --target esnext --moduleResolution nodenext --module NodeNext ",
"test": "vitest run tests/"
},
"author": "overtorment",
Expand Down
20 changes: 17 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ export class SilentPayment {
const Bspend = spendXprv.publicKey;
const bspend = spendXprv.privateKey;

assert(bscan, 'could not derive bscan from seed');
assert(bspend, 'could not derive bspend from seed');

const bech32Version = 0;
const words = [bech32Version].concat(bech32m.toWords(concatUint8Arrays([Bscan, Bspend])));
const address = bech32m.encode('sp', words, 1023);
Expand All @@ -355,7 +358,10 @@ export class SilentPayment {
const t_k = SilentPayment.taggedHash("BIP0352/SharedSecret", concatUint8Arrays([sharedSecret, SilentPayment._ser32(k)]));

// Compute the expected output pubkey
const P_k = ecc.pointAdd(ecc.pointMultiply(G, t_k), code.Bspend);
const tkG = ecc.pointMultiply(G, t_k);
assert(tkG, 'Failed to compute tkG');
const P_k = ecc.pointAdd(tkG, code.Bspend);
assert(P_k, 'Failed to compute output pubkey');

let pubkeyHex = uint8ArrayToHex(P_k);
if (pubkeyHex.startsWith("02") || pubkeyHex.startsWith("03")) pubkeyHex = pubkeyHex.substring(2);
Expand Down Expand Up @@ -400,7 +406,10 @@ export class SilentPayment {
const t_k = SilentPayment.taggedHash("BIP0352/SharedSecret", concatUint8Arrays([sharedSecret, SilentPayment._ser32(k)]));

// Compute the expected output pubkey
const P_k = ecc.pointAdd(ecc.pointMultiply(G, t_k), hexToUint8Array(Bspend));
const tkG = ecc.pointMultiply(G, t_k);
assert(tkG, 'Failed to compute tkG');
const P_k = ecc.pointAdd(tkG, hexToUint8Array(Bspend));
assert(P_k, 'Failed to compute output pubkey');

let pubkeyHex = uint8ArrayToHex(P_k);
if (pubkeyHex.startsWith("02") || pubkeyHex.startsWith("03")) pubkeyHex = pubkeyHex.substring(2);
Expand All @@ -412,7 +421,7 @@ export class SilentPayment {
// alternatively, could compare addresses: SilentPayment.pubkeyToAddress(pubkeyHex) === SilentPayment.pubkeyToAddress(o.script)

// deriving spending privkey for this utxo: d = b_spend + t_k (mod n)
const u: UTXO = {
const u: Omit<UTXO, 'wif'> = {
txid: tx.getId(),
vout,
utxoType: "p2tr"
Expand All @@ -426,3 +435,8 @@ export class SilentPayment {
return ret;
}
}


function assert(condition: any, message: string): asserts condition {
if (!condition) throw new Error(message);
}