UNPKG

@funded-labs/plug-controller

Version:

Internet Computer Plug wallet's controller

44 lines (43 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const identity_1 = require("@dfinity/identity"); 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 = '3053020101300506032b657004220420'; const KEY_SEPARATOR = 'a123032100'; class Ed25519KeyIdentity extends identity_1.Ed25519KeyIdentity { constructor(publicKey, _privateKey) { super(publicKey, _privateKey); this._privateKey = _privateKey; this._publicKey = identity_1.Ed25519PublicKey.from(publicKey); } 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}`; } /** * Serialize this key to JSON. */ toJSON() { return [ (0, arrayBuffer_1.bufferToHex)(this._publicKey.toDer()), (0, arrayBuffer_1.bufferToHex)(this._privateKey), ]; } static fromJSON(json) { const identity = super.fromJSON(json); const keyPair = identity.getKeyPair(); return new Ed25519KeyIdentity(keyPair.publicKey, keyPair.secretKey); } static fromSecretKey(key) { const identity = super.fromSecretKey(key); const keyPair = identity.getKeyPair(); return new Ed25519KeyIdentity(keyPair.publicKey, keyPair.secretKey); } } exports.default = Ed25519KeyIdentity;