From b944943eb8ab089f5e0ae937f52e037d68bee65b Mon Sep 17 00:00:00 2001 From: arnold Date: Thu, 20 Jul 2023 10:16:27 -0400 Subject: [PATCH] Added `Binary.fromMultibase` --- src/Binary.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Binary.ts b/src/Binary.ts index 039ae31..7bc5660 100644 --- a/src/Binary.ts +++ b/src/Binary.ts @@ -66,6 +66,23 @@ export default class Binary extends Uint8Array implements IBinary { return new Binary(hexToBytes(value)); } + static fromMultibase(value: string): Binary { + const code = value.charAt(0); + const encoded = value.slice(1); + + switch (code) { + case 'z': + return Binary.fromBase58(encoded); + case 'm': + return Binary.fromBase64(encoded); + case 'f': + case 'F': + return Binary.fromHex(encoded); + default: + throw new Error(`Unsupported multi-base encoding: ${code}`); + } + } + // Big Endian static fromInt16(value: number): Binary { return new Binary(int16ToBytes(value));