From 771c54e6f9538ebef5a772811dd709d9d912d5ff Mon Sep 17 00:00:00 2001 From: Frank Hinek Date: Thu, 6 Jul 2023 14:19:44 -0700 Subject: [PATCH] Fix bug in bytesToBase58btcMultibase() and add tests Signed-off-by: Frank Hinek --- packages/crypto/src/utils.ts | 2 +- packages/crypto/tests/needed.spec.ts | 7 ------ packages/crypto/tests/utils.spec.ts | 32 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) delete mode 100644 packages/crypto/tests/needed.spec.ts create mode 100644 packages/crypto/tests/utils.spec.ts diff --git a/packages/crypto/src/utils.ts b/packages/crypto/src/utils.ts index fe2f888a7..93f818920 100644 --- a/packages/crypto/src/utils.ts +++ b/packages/crypto/src/utils.ts @@ -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); } \ No newline at end of file diff --git a/packages/crypto/tests/needed.spec.ts b/packages/crypto/tests/needed.spec.ts deleted file mode 100644 index acbae9e82..000000000 --- a/packages/crypto/tests/needed.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { expect } from 'chai'; - -describe('@tbd54566975/crypto', () => { - it('should have tests', () => { - expect(true).to.equal(true); - }); -}); \ No newline at end of file diff --git a/packages/crypto/tests/utils.spec.ts b/packages/crypto/tests/utils.spec.ts new file mode 100644 index 000000000..2acf15f0a --- /dev/null +++ b/packages/crypto/tests/utils.spec.ts @@ -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); + }); + }); +}); \ No newline at end of file