UNPKG

@zlattice/lattice-js

Version:

Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)

37 lines 1.42 kB
import { Base58 } from '@ethersproject/basex'; import { sha256 } from "@ethersproject/sha2"; const ErrChecksum = new Error('checksum error'); const ErrInvalidFormat = new Error('invalid format: version and/or checksum bytes missing'); class Base58Impl { checkDecode(input) { const decoded = Base58.decode(input); if (decoded.length < 5) { throw ErrInvalidFormat; } const version = decoded[0]; const sum = new Uint8Array(4); sum.set(decoded.subarray(decoded.length - 4)); const expectedSum = this.checksum(Buffer.from(decoded.subarray(0, decoded.length - 4))); if (!expectedSum.equals(Buffer.from(sum))) { throw ErrChecksum; } const payload = decoded.subarray(1, decoded.length - 4); return { result: Buffer.from(payload), version: version }; } checkEncode(input, version) { const b = new Uint8Array(1 + input.length + 4); b[0] = version; b.set(input, 1); const sum = this.checksum(Buffer.from(b.subarray(0, 1 + input.length))); b.set(sum, 1 + input.length); return Base58.encode(b); } checksum(input) { const hash = sha256(input); const hash2 = sha256(hash); const bytes = Buffer.from(hash2.slice(2), "hex"); return bytes.subarray(0, 4); } } export { Base58Impl }; //# sourceMappingURL=base58.js.map