@helium/address
Version:
Helium public key utilities
58 lines • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const KeyTypes_1 = require("./KeyTypes");
const NetTypes_1 = require("./NetTypes");
class Address {
constructor(version, netType, keyType, publicKey) {
if (version !== 0) {
throw new Error('unsupported version');
}
if (!NetTypes_1.SUPPORTED_NET_TYPES.includes(netType)) {
throw new Error('unsupported net type');
}
if (!KeyTypes_1.SUPPORTED_KEY_TYPES.includes(keyType)) {
throw new Error('unsupported key type');
}
this.version = version;
this.netType = netType;
this.keyType = keyType;
this.publicKey = publicKey;
}
get bin() {
return Buffer.concat([
// eslint-disable-next-line no-bitwise
Buffer.from([this.netType | this.keyType]),
Buffer.from(this.publicKey),
]);
}
get b58() {
return (0, utils_1.bs58CheckEncode)(this.version, this.bin);
}
static fromB58(b58) {
const version = (0, utils_1.bs58Version)(b58);
const netType = (0, utils_1.bs58NetType)(b58);
const keyType = (0, utils_1.bs58KeyType)(b58);
const publicKey = (0, utils_1.bs58PublicKey)(b58);
return new Address(version, netType, keyType, publicKey);
}
static fromBin(bin) {
const version = 0;
const byte = bin[0];
const netType = (0, utils_1.byteToNetType)(byte);
const keyType = (0, utils_1.byteToKeyType)(byte);
const publicKey = bin.slice(1, bin.length);
return new Address(version, netType, keyType, publicKey);
}
static isValid(b58) {
try {
Address.fromB58(b58);
return true;
}
catch (error) {
return false;
}
}
}
exports.default = Address;
//# sourceMappingURL=Address.js.map