UNPKG

3.93 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PrivateKey = void 0;
4var eosjs_numeric_1 = require("./eosjs-numeric");
5var eosjs_key_conversions_1 = require("./eosjs-key-conversions");
6/** Represents/stores a private key and provides easy conversion for use with `elliptic` lib */
7var PrivateKey = /** @class */ (function () {
8 function PrivateKey(key, ec) {
9 this.key = key;
10 this.ec = ec;
11 }
12 /** Instantiate private key from an `elliptic`-format private key */
13 PrivateKey.fromElliptic = function (privKey, keyType, ec) {
14 if (!ec) {
15 ec = eosjs_key_conversions_1.constructElliptic(keyType);
16 }
17 return new PrivateKey({
18 type: keyType,
19 data: privKey.getPrivate().toArrayLike(Buffer, 'be', 32),
20 }, ec);
21 };
22 /** Instantiate private key from an EOSIO-format private key */
23 PrivateKey.fromString = function (keyString, ec) {
24 var privateKey = eosjs_numeric_1.stringToPrivateKey(keyString);
25 if (!ec) {
26 ec = eosjs_key_conversions_1.constructElliptic(privateKey.type);
27 }
28 return new PrivateKey(privateKey, ec);
29 };
30 /** Export private key as `elliptic`-format private key */
31 PrivateKey.prototype.toElliptic = function () {
32 return this.ec.keyFromPrivate(this.key.data);
33 };
34 PrivateKey.prototype.toLegacyString = function () {
35 return eosjs_numeric_1.privateKeyToLegacyString(this.key);
36 };
37 /** Export private key as EOSIO-format private key */
38 PrivateKey.prototype.toString = function () {
39 return eosjs_numeric_1.privateKeyToString(this.key);
40 };
41 /** Get key type from key */
42 PrivateKey.prototype.getType = function () {
43 return this.key.type;
44 };
45 /** Retrieve the public key from a private key */
46 PrivateKey.prototype.getPublicKey = function () {
47 var ellipticPrivateKey = this.toElliptic();
48 return eosjs_key_conversions_1.PublicKey.fromElliptic(ellipticPrivateKey, this.getType(), this.ec);
49 };
50 /** Sign a message or hashed message digest with private key */
51 PrivateKey.prototype.sign = function (data, shouldHash, encoding) {
52 var _this = this;
53 if (shouldHash === void 0) { shouldHash = true; }
54 if (encoding === void 0) { encoding = 'utf8'; }
55 if (shouldHash) {
56 if (typeof data === 'string') {
57 data = Buffer.from(data, encoding);
58 }
59 data = this.ec.hash().update(data).digest();
60 }
61 var tries = 0;
62 var signature;
63 var isCanonical = function (sigData) {
64 return !(sigData[1] & 0x80) && !(sigData[1] === 0 && !(sigData[2] & 0x80))
65 && !(sigData[33] & 0x80) && !(sigData[33] === 0 && !(sigData[34] & 0x80));
66 };
67 var constructSignature = function (options) {
68 var ellipticPrivateKey = _this.toElliptic();
69 var ellipticSignature = ellipticPrivateKey.sign(data, options);
70 return eosjs_key_conversions_1.Signature.fromElliptic(ellipticSignature, _this.getType(), _this.ec);
71 };
72 if (this.key.type === eosjs_numeric_1.KeyType.k1) {
73 do {
74 signature = constructSignature({ canonical: true, pers: [++tries] });
75 } while (!isCanonical(signature.toBinary()));
76 }
77 else {
78 signature = constructSignature({ canonical: true });
79 }
80 return signature;
81 };
82 /** Validate a private key */
83 PrivateKey.prototype.isValid = function () {
84 try {
85 var ellipticPrivateKey = this.toElliptic();
86 var validationObj = ellipticPrivateKey.validate();
87 return validationObj.result;
88 }
89 catch (_a) {
90 return false;
91 }
92 };
93 return PrivateKey;
94}());
95exports.PrivateKey = PrivateKey;
96//# sourceMappingURL=PrivateKey.js.map
\No newline at end of file