UNPKG

@funded-labs/plug-controller

Version:

Internet Computer Plug wallet's controller

116 lines (115 loc) 5.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable no-underscore-dangle */ const secp256k1_1 = __importDefault(require("secp256k1")); const js_sha256_1 = require("js-sha256"); const genericSignIdentity_1 = require("../genericSignIdentity"); const publicKey_1 = __importDefault(require("./publicKey")); const arrayBuffer_1 = require("../../arrayBuffer"); const PEM_BEGIN = `-----BEGIN EC PARAMETERS----- BgUrgQQACg== -----END EC PARAMETERS----- -----BEGIN EC PRIVATE KEY-----`; const PEM_END = '-----END EC PRIVATE KEY-----'; const PRIV_KEY_INIT = '30740201010420'; const KEY_SEPARATOR = 'a00706052b8104000aa144034200'; class Secp256k1KeyIdentity extends genericSignIdentity_1.GenericSignIdentity { // `fromRaw` and `fromDer` should be used for instantiation, not this constructor. constructor(publicKey, _privateKey) { super(); this._privateKey = _privateKey; this._publicKey = publicKey; this._privateKey = _privateKey; } static fromParsedJson(obj) { const [publicKeyRaw, privateKeyRaw] = obj; return new Secp256k1KeyIdentity(publicKey_1.default.fromRaw((0, arrayBuffer_1.bufferFromHex)(publicKeyRaw)), (0, arrayBuffer_1.bufferFromHex)(privateKeyRaw)); } static fromJSON(json) { const parsed = JSON.parse(json); if (Array.isArray(parsed)) { if (typeof parsed[0] === 'string' && typeof parsed[1] === 'string') { return this.fromParsedJson([parsed[0], parsed[1]]); } throw new Error('Deserialization error: JSON must have at least 2 items.'); } else if (typeof parsed === 'object' && parsed !== null) { const { publicKey, _publicKey, secretKey, _privateKey } = parsed; const pk = publicKey ? publicKey_1.default.fromRaw(new Uint8Array(publicKey.data).buffer) : publicKey_1.default.fromDer(new Uint8Array(_publicKey.data).buffer); if (publicKey && secretKey && secretKey.data) { return new Secp256k1KeyIdentity(pk, new Uint8Array(secretKey.data).buffer); } if (_publicKey && _privateKey && _privateKey.data) { return new Secp256k1KeyIdentity(pk, new Uint8Array(_privateKey.data).buffer); } } throw new Error(`Deserialization error: Invalid JSON type for string: ${JSON.stringify(json)}`); } static fromKeyPair(publicKey, privateKey) { return new Secp256k1KeyIdentity(publicKey_1.default.fromRaw(publicKey), privateKey); } static fromSecretKey(secretKey) { const publicKey = secp256k1_1.default.publicKeyCreate(new Uint8Array(secretKey), false); const identity = Secp256k1KeyIdentity.fromKeyPair(publicKey.buffer, new Uint8Array(secretKey).buffer); return identity; } /** * Serialize this key to JSON. */ toJSON() { return [ (0, arrayBuffer_1.bufferToHex)(this._publicKey.toRaw()), (0, arrayBuffer_1.bufferToHex)(this._privateKey), ]; } /** * Return a copy of the key pair. */ getKeyPair() { return { secretKey: new Uint8Array(this._privateKey).buffer, publicKey: this._publicKey, }; } /** * Return the public key. */ getPublicKey() { return this._publicKey; } /** * Return private key in a pem file */ getPem() { const rawPrivateKey = this._privateKey.toString(); const rawPublicKey = this._publicKey.toRaw().toString(); return `${PEM_BEGIN}\n${Buffer.from(`${PRIV_KEY_INIT}${rawPrivateKey}${KEY_SEPARATOR}${rawPublicKey}`, 'hex').toString('base64')}\n${PEM_END}`; } /** * Signs a blob of data, with this identity's private key. * @param challenge - challenge to sign with this identity's secretKey, producing a signature */ sign(challenge) { return __awaiter(this, void 0, void 0, function* () { const hash = js_sha256_1.sha256.create(); hash.update(challenge); const { signature } = secp256k1_1.default.ecdsaSign(new Uint8Array(hash.digest()), new Uint8Array(this._privateKey)); return signature.buffer; }); } } exports.default = Secp256k1KeyIdentity;