Skip to content

Commit

Permalink
fix: return actuall addresses in outputs instead of pubkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Overtorment committed May 30, 2024
1 parent 8228f4d commit d3a45d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "silent-payments",
"version": "0.3.3",
"version": "0.3.4",
"description": "BIP-352",
"main": "src/index.ts",
"scripts": {
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class SilentPayment {
const Pmk = Buffer.from(ecc.pointAdd(ecc.pointMultiply(G, tk) as Uint8Array, Bm) as Uint8Array);

// Encode Pmk as a BIP341 taproot output
const address = Pmk.slice(1).toString("hex");
const address = SilentPayment.pubkeyToAddress(Pmk.slice(1).toString("hex"));
const newTarget: Target = { address };
newTarget.value = amount;
ret.push(newTarget);
Expand Down Expand Up @@ -194,4 +194,13 @@ export class SilentPayment {

return true;
}

static pubkeyToAddress(hex: string): string {
const publicKey = Buffer.from("5120" + hex, "hex");
return bitcoin.address.fromOutputScript(publicKey, bitcoin.networks.bitcoin);
}

static addressToPubkey(address: string): string {
return bitcoin.address.toOutputScript(address).subarray(2).toString("hex");
}
}
14 changes: 13 additions & 1 deletion tests/silent-payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ tests.forEach((testCase, index) => {
}).toThrow("No eligible UTXOs with private keys found");
} else {
const generated = sp.createTransaction(utxos, recipients);
const generated_pubkeys: string[] = generated.map((obj) => obj.address).filter(Boolean) as string[];
const generated_pubkeys: string[] = generated.map((obj) => SilentPayment.addressToPubkey(String(obj.address))).filter(Boolean) as string[];
assert(matchSubset(generated_pubkeys, sending.expected.outputs));
}
});
Expand Down Expand Up @@ -176,3 +176,15 @@ it("can validate payment code", () => {
assert.ok(!SilentPayment.isPaymentCodeValid("qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv")); // no prefix
assert.ok(!SilentPayment.isPaymentCodeValid("qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv123")); // no prefix
});

it("can turn pubkey into taproot address", () => {
assert.strictEqual(SilentPayment.pubkeyToAddress("40ef293a8a0ebaf8b351a27d89ff4b5b3822a635e4afdca77a30170c363bafa3"), "bc1pgrhjjw52p6a03v635f7cnl6ttvuz9f34ujhaefm6xqtscd3m473szkl92g");

expect(() => {
SilentPayment.pubkeyToAddress("512040ef293a8a0ebaf8b351a27d89ff4b5b3822a635e4afdca77a30170c363bafa3");
}).toThrow(/has no matching Address/);
});

it("can turn taproot address into pubkey", () => {
assert.strictEqual(SilentPayment.addressToPubkey("bc1pgrhjjw52p6a03v635f7cnl6ttvuz9f34ujhaefm6xqtscd3m473szkl92g"), "40ef293a8a0ebaf8b351a27d89ff4b5b3822a635e4afdca77a30170c363bafa3");
});

0 comments on commit d3a45d7

Please sign in to comment.