Skip to content

Commit

Permalink
Added Binary.fromMultibase
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed Jul 20, 2023
1 parent 85a6a58 commit b944943
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit b944943

Please sign in to comment.