From a83011a332b79416348c5a531e28ff8c32e342a6 Mon Sep 17 00:00:00 2001 From: overtorment Date: Tue, 28 May 2024 19:40:14 +0100 Subject: [PATCH] add: method to validate SP payment code --- src/index.ts | 14 ++++++++++++++ tests/silent-payment.test.ts | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/index.ts b/src/index.ts index c60b1f0..7898e34 100644 --- a/src/index.ts +++ b/src/index.ts @@ -183,4 +183,18 @@ export class SilentPayment { return ret } + + static isPaymentCodeValid(pc: string) { + try { + const result = bech32m.decode(pc, 118); + const version = result.words.shift(); + if (version !== 0) { + return false; + } + } catch (_) { + return false; + } + + return true; + } } diff --git a/tests/silent-payment.test.ts b/tests/silent-payment.test.ts index cdb4d2e..88a41d7 100644 --- a/tests/silent-payment.test.ts +++ b/tests/silent-payment.test.ts @@ -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 +});