Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return actuall addresses in outputs instead of pubkeys #12

Merged
merged 1 commit into from
May 30, 2024
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
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");
});
Loading