Skip to content

Commit

Permalink
Fix bug in bytesToBase58btcMultibase() and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Jul 7, 2023
1 parent 9aa02be commit 771c54e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/crypto/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function base64UrlToBytes(base64urlString: string): Uint8Array {
export function bytesToBase58btcMultibase(header: Uint8Array, bytes: Uint8Array): string {
const multibaseBytes = new Uint8Array(header.length + bytes.length);
multibaseBytes.set(header);
multibaseBytes.set(bytes);
multibaseBytes.set(bytes, header.length);

return base58btc.encode(multibaseBytes);
}
7 changes: 0 additions & 7 deletions packages/crypto/tests/needed.spec.ts

This file was deleted.

32 changes: 32 additions & 0 deletions packages/crypto/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from 'chai';

import { bytesToBase58btcMultibase } from '../src/utils.js';

describe('Crypto Utils', () => {
describe('bytesToBase58btcMultibase()', () => {
it('returns a multibase encoded string', () => {
// Test Vector 1.
const input = {
header : new Uint8Array([0x00, 0x00]),
data : new Uint8Array([0x00, 0x00])
};
const output = 'z1111';
const encoded = bytesToBase58btcMultibase(input.header, input.data);
expect(encoded).to.be.a.string;
expect(encoded.substring(0, 1)).to.equal('z');
expect(encoded).to.deep.equal(output);
});

it('returns multibase encoded value with specified header', () => {
// Test Vector 1.
const input = {
header : new Uint8Array([0x01, 0x02]),
data : new Uint8Array([3, 4, 5, 6, 7])
};
const output = 'z3DUyZY2dc';

const encoded = bytesToBase58btcMultibase(input.header, input.data);
expect(encoded).to.deep.equal(output);
});
});
});

0 comments on commit 771c54e

Please sign in to comment.