Skip to content
Open
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
19 changes: 10 additions & 9 deletions dist/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
Object.defineProperty(exports, "__esModule", { value: true });
const bech32Lib = require("../");
const tape = require("tape");
const uint8arraytools = require("uint8array-tools");
const fixtures = require('../../src/test/fixtures');
function testValidFixture(f, bech32) {
if (f.hex) {
tape(`fromWords/toWords ${f.hex}`, (t) => {
t.plan(3);
const words = bech32.toWords(Buffer.from(f.hex, 'hex'));
const bytes = Buffer.from(bech32.fromWords(f.words));
const bytes2 = Buffer.from(bech32.fromWordsUnsafe(f.words));
const words = bech32.toWords(uint8arraytools.fromHex(f.hex));
const bytes = uint8arraytools.fromUtf8(bech32.fromWords(f.words));
const bytes2 = uint8arraytools.fromUtf8(bech32.fromWordsUnsafe(f.words));
t.same(words, f.words);
t.same(bytes.toString('hex'), f.hex);
t.same(bytes2.toString('hex'), f.hex);
t.same(uint8arraytools.toHex(bytes), f.hex);
t.same(uint8arraytools.toHex(bytes), f.hex);
});
}
tape(`encode ${f.prefix} ${f.hex || f.words}`, (t) => {
Expand All @@ -30,9 +31,9 @@ function testValidFixture(f, bech32) {
});
tape(`fails for ${f.string} with 1 bit flipped`, (t) => {
t.plan(2);
const buffer = Buffer.from(f.string, 'utf8');
buffer[f.string.lastIndexOf('1') + 1] ^= 0x1; // flip a bit, after the prefix
const str = buffer.toString('utf8');
const uint8array = uint8arraytools.fromUtf8(f.string);
uint8array[f.string.lastIndexOf('1') + 1] ^= 0x1; // flip a bit, after the prefix
const str = uint8arraytools.toUtf8(uint8array);
t.equal(bech32.decodeUnsafe(str, f.limit), undefined);
t.throws(() => {
bech32.decode(str, f.limit);
Expand All @@ -58,7 +59,7 @@ function testInvalidFixture(f, bech32) {
});
}
if (f.string !== undefined || f.stringHex) {
const str = f.string || Buffer.from(f.stringHex, 'hex').toString('binary');
const str = f.string || uint8arraytools.toUtf8(uint8arraytools.fromHex(f.stringHex));
tape(`decode fails for ${str} (${f.exception})`, (t) => {
t.plan(2);
t.equal(bech32.decodeUnsafe(str), undefined);
Expand Down
Loading