@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
168 lines (165 loc) • 5.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtKey = void 0;
const ApiStatic_1 = require("../api/ApiStatic");
const ExtKeyNative_1 = require("../api/ExtKeyNative");
const FinalizationHelper_1 = require("../FinalizationHelper");
const BaseApi_1 = require("./BaseApi");
class ExtKey extends BaseApi_1.BaseApi {
native;
ptr;
static async freeExtKey(ptr) {
const nativeApi = new ExtKeyNative_1.ExtKeyNative(ApiStatic_1.ApiStatic.getInstance());
await nativeApi.deleteExtKey(ptr);
}
/**
* Creates ExtKey from given seed.
* @param {Uint8Array} seed the seed used to generate Key
* @returns {ExtKey} object
*/
static async fromSeed(seed) {
const ptr = await ExtKeyNative_1.ExtKeyNative.fromSeed([seed]);
const native = new ExtKeyNative_1.ExtKeyNative(ApiStatic_1.ApiStatic.getInstance());
const extKey = new ExtKey(native, ptr);
const fh = FinalizationHelper_1.FinalizationHelper.getInstance();
fh.register(extKey, { ptr: ptr, onFree: () => this.freeExtKey(ptr) });
return extKey;
}
/**
* Decodes ExtKey from Base58 format.
*
* @param {string} base58 the ExtKey in Base58
* @returns {ExtKey} object
*/
static async fromBase58(base58) {
const ptr = await ExtKeyNative_1.ExtKeyNative.fromBase58([base58]);
const native = new ExtKeyNative_1.ExtKeyNative(ApiStatic_1.ApiStatic.getInstance());
const extKey = new ExtKey(native, ptr);
const fh = FinalizationHelper_1.FinalizationHelper.getInstance();
fh.register(extKey, { ptr: ptr, onFree: () => this.freeExtKey(ptr) });
return extKey;
}
/**
* Generates a new ExtKey.
*
* @returns {ExtKey} object
*/
static async generateRandom() {
const ptr = await ExtKeyNative_1.ExtKeyNative.generateRandom([]);
const native = new ExtKeyNative_1.ExtKeyNative(ApiStatic_1.ApiStatic.getInstance());
const extKey = new ExtKey(native, ptr);
const fh = FinalizationHelper_1.FinalizationHelper.getInstance();
fh.register(extKey, { ptr: ptr, onFree: () => this.freeExtKey(ptr) });
return extKey;
}
/**
* //doc-gen:ignore
*/
constructor(native, ptr) {
super(ptr);
this.native = native;
this.ptr = ptr;
}
static fromPtr(ptr) {
const native = new ExtKeyNative_1.ExtKeyNative(ApiStatic_1.ApiStatic.getInstance());
return new ExtKey(native, ptr);
}
/**
* Generates child ExtKey from a current ExtKey using BIP32.
*
* @param {number} index number from 0 to 2^31-1
* @returns {ExtKey} object
*/
async derive(index) {
const ptr = await this.native.derive(this.servicePtr, [index]);
const extKey = new ExtKey(this.native, ptr);
const fh = FinalizationHelper_1.FinalizationHelper.getInstance();
fh.register(extKey, { ptr: ptr, onFree: () => ExtKey.freeExtKey(ptr) });
return extKey;
}
/**
* Generates hardened child ExtKey from a current ExtKey using BIP32.
*
* @param {number} index number from 0 to 2^31-1
* @returns {ExtKey} object
*/
async deriveHardened(index) {
const extKeyPtr = await this.native.deriveHardened(this.servicePtr, [index]);
return new ExtKey(this.native, extKeyPtr);
}
/**
* Converts ExtKey to Base58 string.
*
* @returns {string} ExtKey in Base58 format
*/
async getPrivatePartAsBase58() {
return this.native.getPrivatePartAsBase58(this.servicePtr, []);
}
/**
* Converts the public part of ExtKey to Base58 string.
*
* @returns {string} ExtKey in Base58 format
*/
async getPublicPartAsBase58() {
return this.native.getPublicPartAsBase58(this.servicePtr, []);
}
/**
* Extracts ECC PrivateKey.
*
* @returns {string} ECC key in WIF format
*/
async getPrivateKey() {
return this.native.getPrivateKey(this.servicePtr, []);
}
/**
* Extracts ECC PublicKey.
*
* @returns {string} ECC key in BASE58DER format
*/
async getPublicKey() {
return this.native.getPublicKey(this.servicePtr, []);
}
/**
* Extracts raw ECC PrivateKey.
*
* @returns {Uint8Array} ECC PrivateKey
*/
async getPrivateEncKey() {
return this.native.getPrivateEncKey(this.servicePtr, []);
}
/**
* Extracts ECC PublicKey Address.
*
* @returns {string} ECC Address in BASE58 format
*/
async getPublicKeyAsBase58Address() {
return this.native.getPublicKeyAsBase58Address(this.servicePtr, []);
}
/**
* Gets the chain code of Extended Key.
*
* @returns {Uint8Array} Raw chain code
*/
async getChainCode() {
return this.native.getChainCode(this.servicePtr, []);
}
/**
* Validates a signature of a message.
*
* @param {Uint8Array} message data used on validation
* @param {Uint8Array} signature signature of data to verify
* @returns {boolean} message validation result
*/
async verifyCompactSignatureWithHash(message, signature) {
return this.native.verifyCompactSignatureWithHash(this.servicePtr, [message, signature]);
}
/**
* Checks if ExtKey is Private.
*
* @returns {boolean} true if ExtKey is private
*/
async isPrivate() {
return this.native.isPrivate(this.servicePtr, []);
}
}
exports.ExtKey = ExtKey;