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

add: method to validate SP payment code #10

Merged
merged 1 commit into from
May 28, 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
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,18 @@ export class SilentPayment {

return ret
}

static isPaymentCodeValid(pc: string) {
try {
const result = bech32m.decode(pc, 118);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, this 118 is the char limit being passed to decode? Since new versions of sp address can (and likely will) have more data added to the data part, might be better to use 1024 here? This is the recommended number from the BIP

const version = result.words.shift();
if (version !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent payment addresses are meant to be forward compatible, so that a v0 sender can send to a v1, v2, v3, etc address without needing to make any changes. BIP352 specifies the following rules for a v0 sp sender when checking an address:

  1. If version === 0, check that the data part is exactly 66-bytes (2 public keys). Otherwise, fail
  2. If 0 < version < 31, read the first 66-bytes of the data part and ignore the remaining bytes
  3. If version === 31, fail.

return false;
}
} catch (_) {
return false;
}

return true;
}
}
15 changes: 15 additions & 0 deletions tests/silent-payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,18 @@ it("SilentPayment._ser32() works", () => {
assert.strictEqual(SilentPayment._ser32(1).toString("hex"), "00000001");
assert.strictEqual(SilentPayment._ser32(444).toString("hex"), "000001bc");
});

it("can validate payment code", () => {
assert.ok(SilentPayment.isPaymentCodeValid("sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv"));
assert.ok(SilentPayment.isPaymentCodeValid("sp1qqgrz6j0lcqnc04vxccydl0kpsj4frfje0ktmgcl2t346hkw30226xqupawdf48k8882j0strrvcmgg2kdawz53a54dd376ngdhak364hzcmynqtn"));
assert.ok(SilentPayment.isPaymentCodeValid("sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjex54dmqmmv6rw353tsuqhs99ydvadxzrsy9nuvk74epvee55drs734pqq"));
assert.ok(SilentPayment.isPaymentCodeValid("sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqsg59z2rppn4qlkx0yz9sdltmjv3j8zgcqadjn4ug98m3t6plujsq9qvu5n"));
assert.ok(SilentPayment.isPaymentCodeValid("sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgq7c2zfthc6x3a5yecwc52nxa0kfd20xuz08zyrjpfw4l2j257yq6qgnkdh5"));

assert.ok(!SilentPayment.isPaymentCodeValid("sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgq7c2zfthc6x3a5yecwc52nxa0kfd20xuz08zyrjpfw4l2j257yq6qgn")); // short a few symbols
assert.ok(!SilentPayment.isPaymentCodeValid("sp1qq")); // short a few symbols
assert.ok(!SilentPayment.isPaymentCodeValid("garbage"));
assert.ok(!SilentPayment.isPaymentCodeValid("sp2qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgq7c2zfthc6x3a5yecwc52nxa0kfd20xuz08zyrjpfw4l2j257yq6qgnkdh5")); // wrong prefix
assert.ok(!SilentPayment.isPaymentCodeValid("qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv")); // no prefix
assert.ok(!SilentPayment.isPaymentCodeValid("qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgqjuexzk6murw56suy3e0rd2cgqvycxttddwsvgxe2usfpxumr70xc9pkqwv123")); // no prefix
});
Loading