Skip to content
This repository has been archived by the owner on Mar 17, 2019. It is now read-only.

Commit

Permalink
Adding test for unknown fallback address type
Browse files Browse the repository at this point in the history
  • Loading branch information
bmancini55 committed Mar 3, 2019
1 parent a250807 commit 78126e9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/decoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ describe('decoder', () => {

it('must fail if the signature is invalid');

it('must skip f field with unknown version');
it('must skip f field with unknown version', () => {
let input = 'lnbc11pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5xysxxatsyp3k7enxv4jsfqfndpjkcmr0vwkqhkwv54grmkq6r8w4tweqj3d44x66fx42uc6zufkw6ytdus4jxnyrwce5ryjv5hs23l4zcrl2u0vuzzs73eeyugr8gf85gcufgycpmplv60'; // prettier-ignore
let result = decoder.decode(input);
expect(result.unknownFields[0]).to.deep.equal({
type: 9,
value: {
version: 19,
address: Buffer.from('hello'),
},
});
});
it('must skip p field with length !== 52');
it('must skip h field with length !== 52');
it('must skip n field with length !== 53');
Expand Down
2 changes: 1 addition & 1 deletion src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function _encodeData(invoice, writer) {
let dataLen = bech32.sizeofBits(datum.value.address.byteLength * 8) + 1;
writer.writeUIntBE(datum.type, 1);
writer.writeUIntBE(dataLen, 2);
writer.writeUIntBE(datum.value.type, 1);
writer.writeUIntBE(datum.value.version, 1);
writer.writeBytes(datum.value.address);
}
break;
Expand Down
10 changes: 5 additions & 5 deletions src/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ class Invoice {
}

addFallbackAddress(addrStr) {
let type, address;
let version, address;
if (addrStr.startsWith('1') || addrStr.startsWith('m') || addrStr.startsWith('n')) {
type = 17;
version = 17;
address = bs58check.decode(addrStr).slice(1); // remove prefix
} else if (addrStr.startsWith('3') || addrStr.startsWith('2')) {
type = 18;
version = 18;
address = bs58check.decode(addrStr).slice(1); // remove prefix
} else if (addrStr.startsWith('bc1') || addrStr.startsWith('tb1')) {
let words = bech32.decode(addrStr).words;
type = words[0];
version = words[0];
address = bech32.fromWords(words.slice(1));
}
if (Array.isArray(address)) address = Buffer.from(address);
this.fields.push({ type: 9, value: { type, address } });
this.fields.push({ type: 9, value: { version, address } });
}

addRoute(routes) {
Expand Down
14 changes: 7 additions & 7 deletions src/invoice.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,49 +93,49 @@ describe('invoice', () => {
describe('addFallbackAddress', () => {
it('Bitcoin Mainnet P2PKH - 1', () => {
sut.addFallbackAddress('17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem');
expect(sut.fields[0].value.type).to.equal(17);
expect(sut.fields[0].value.version).to.equal(17);
expect(sut.fields[0].value.address).to.deep.equal(
Buffer.from('47376c6f537d62177a2c41c4ca9b45829ab99083', 'hex')
);
});
it('Bitcoin Testnet P2PKH - m', () => {
sut.addFallbackAddress('mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn');
expect(sut.fields[0].value.type).to.equal(17);
expect(sut.fields[0].value.version).to.equal(17);
expect(sut.fields[0].value.address).to.deep.equal(
Buffer.from('243f1394f44554f4ce3fd68649c19adc483ce924', 'hex')
);
});
it('Bitcoin Testnet P2PKH - n', () => {
sut.addFallbackAddress('n11ByR8jqq6DiPB7ny2Udt9tK7QDVKNXKw');
expect(sut.fields[0].value.type).to.equal(17);
expect(sut.fields[0].value.version).to.equal(17);
expect(sut.fields[0].value.address).to.deep.equal(
Buffer.from('d5c174880d3dcdaf904cd89f06f1f2862c948cb7', 'hex')
);
});
it('Bitcoin Mainnet P2SH - 3', () => {
sut.addFallbackAddress('3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX');
expect(sut.fields[0].value.type).to.equal(18);
expect(sut.fields[0].value.version).to.equal(18);
expect(sut.fields[0].value.address).to.deep.equal(
Buffer.from('8f55563b9a19f321c211e9b9f38cdf686ea07845', 'hex')
);
});
it('Bitcoin Testnet P2SH - 2', () => {
sut.addFallbackAddress('2MzQwSSnBHWHqSAqtTVQ6v47XtaisrJa1Vc');
expect(sut.fields[0].value.type).to.equal(18);
expect(sut.fields[0].value.version).to.equal(18);
expect(sut.fields[0].value.address).to.deep.equal(
Buffer.from('4e9f39ca4688ff102128ea4ccda34105324305b0', 'hex')
);
});
it('Bitcoin Mainnet Bech32', () => {
sut.addFallbackAddress('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
expect(sut.fields[0].value.type).to.equal(0);
expect(sut.fields[0].value.version).to.equal(0);
expect(sut.fields[0].value.address).to.deep.equal(
Buffer.from('751e76e8199196d454941c45d1b3a323f1433bd6', 'hex')
);
});
it('Bitcoin Testnet Bech32', () => {
sut.addFallbackAddress('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx');
expect(sut.fields[0].value.type).to.equal(0);
expect(sut.fields[0].value.version).to.equal(0);
expect(sut.fields[0].value.address).to.deep.equal(
Buffer.from('751e76e8199196d454941c45d1b3a323f1433bd6', 'hex')
);
Expand Down

0 comments on commit 78126e9

Please sign in to comment.