1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PrivateKey = void 0;
|
4 | var eosjs_numeric_1 = require("./eosjs-numeric");
|
5 | var eosjs_key_conversions_1 = require("./eosjs-key-conversions");
|
6 |
|
7 | var PrivateKey = (function () {
|
8 | function PrivateKey(key, ec) {
|
9 | this.key = key;
|
10 | this.ec = ec;
|
11 | }
|
12 |
|
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 |
|
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 |
|
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 |
|
38 | PrivateKey.prototype.toString = function () {
|
39 | return eosjs_numeric_1.privateKeyToString(this.key);
|
40 | };
|
41 |
|
42 | PrivateKey.prototype.getType = function () {
|
43 | return this.key.type;
|
44 | };
|
45 |
|
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 |
|
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 |
|
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 | }());
|
95 | exports.PrivateKey = PrivateKey;
|
96 |
|
\ | No newline at end of file |