UNPKG

@taquito/signer

Version:

Provide signing functionality to be with taquito

103 lines (102 loc) 4.18 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Tz1 = void 0; const blake2b_1 = require("@stablelib/blake2b"); const ed25519_1 = require("@stablelib/ed25519"); const utils_1 = require("@taquito/utils"); const typedarray_to_buffer_1 = require("typedarray-to-buffer"); const core_1 = require("@taquito/core"); /** * @description Provide signing logic for ed25519 curve based key (tz1) */ class Tz1 { /** * * @param key Encoded private key * @param encrypted Is the private key encrypted * @param decrypt Decrypt function * @throws {@link InvalidKeyError} */ constructor(key, encrypted, decrypt) { this.key = key; const keyPrefix = key.substring(0, encrypted ? 5 : 4); if (!(0, utils_1.isValidPrefix)(keyPrefix)) { throw new core_1.InvalidKeyError(`${(0, utils_1.invalidDetail)(utils_1.ValidationResult.NO_PREFIX_MATCHED)} expecting either '${utils_1.Prefix.EDESK}' or '${utils_1.Prefix.EDSK}'.`); } this._key = decrypt((0, utils_1.b58cdecode)(this.key, utils_1.prefix[keyPrefix])); this._publicKey = this._key.slice(32); if (!this._key) { throw new core_1.InvalidKeyError('unable to decode'); } this.isInit = this.init(); } init() { return __awaiter(this, void 0, void 0, function* () { if (this._key.length !== 64) { const { publicKey, secretKey } = (0, ed25519_1.generateKeyPairFromSeed)(new Uint8Array(this._key)); this._publicKey = publicKey; this._key = secretKey; } return true; }); } /** * * @param bytes Bytes to sign * @param bytesHash Blake2b hash of the bytes to sign */ sign(bytes, bytesHash) { return __awaiter(this, void 0, void 0, function* () { yield this.isInit; const signature = (0, ed25519_1.sign)(new Uint8Array(this._key), new Uint8Array(bytesHash)); const signatureBuffer = (0, typedarray_to_buffer_1.default)(signature); const sbytes = bytes + (0, utils_1.buf2hex)(signatureBuffer); return { bytes, sig: (0, utils_1.b58cencode)(signature, utils_1.prefix.sig), prefixSig: (0, utils_1.b58cencode)(signature, utils_1.prefix.edsig), sbytes, }; }); } /** * @returns Encoded public key */ publicKey() { return __awaiter(this, void 0, void 0, function* () { yield this.isInit; return (0, utils_1.b58cencode)(this._publicKey, utils_1.prefix['edpk']); }); } /** * @returns Encoded public key hash */ publicKeyHash() { return __awaiter(this, void 0, void 0, function* () { yield this.isInit; return (0, utils_1.b58cencode)((0, blake2b_1.hash)(new Uint8Array(this._publicKey), 20), utils_1.prefix.tz1); }); } /** * @returns Encoded private key */ secretKey() { return __awaiter(this, void 0, void 0, function* () { yield this.isInit; let key = this._key; const { secretKey } = (0, ed25519_1.generateKeyPairFromSeed)(new Uint8Array(key).slice(0, 32)); key = (0, typedarray_to_buffer_1.default)(secretKey); return (0, utils_1.b58cencode)(key, utils_1.prefix[`edsk`]); }); } } exports.Tz1 = Tz1;