@zlattice/lattice-js
Version:
Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)
40 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Base58Impl = void 0;
const basex_1 = require("@ethersproject/basex");
const sha2_1 = require("@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 = basex_1.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 basex_1.Base58.encode(b);
}
checksum(input) {
const hash = (0, sha2_1.sha256)(input);
const hash2 = (0, sha2_1.sha256)(hash);
const bytes = Buffer.from(hash2.slice(2), "hex");
return bytes.subarray(0, 4);
}
}
exports.Base58Impl = Base58Impl;
//# sourceMappingURL=base58.js.map