1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Signature = void 0;
|
4 | var BN = require("bn.js");
|
5 | var eosjs_numeric_1 = require("./eosjs-numeric");
|
6 | var eosjs_key_conversions_1 = require("./eosjs-key-conversions");
|
7 |
|
8 | var Signature = (function () {
|
9 | function Signature(signature, ec) {
|
10 | this.signature = signature;
|
11 | this.ec = ec;
|
12 | }
|
13 |
|
14 | Signature.fromString = function (sig, ec) {
|
15 | var signature = eosjs_numeric_1.stringToSignature(sig);
|
16 | if (!ec) {
|
17 | ec = eosjs_key_conversions_1.constructElliptic(signature.type);
|
18 | }
|
19 | return new Signature(signature, ec);
|
20 | };
|
21 |
|
22 | Signature.fromElliptic = function (ellipticSig, keyType, ec) {
|
23 | var r = ellipticSig.r.toArray('be', 32);
|
24 | var s = ellipticSig.s.toArray('be', 32);
|
25 | var eosioRecoveryParam;
|
26 | if (keyType === eosjs_numeric_1.KeyType.k1 || keyType === eosjs_numeric_1.KeyType.r1) {
|
27 | eosioRecoveryParam = ellipticSig.recoveryParam + 27;
|
28 | if (ellipticSig.recoveryParam <= 3) {
|
29 | eosioRecoveryParam += 4;
|
30 | }
|
31 | }
|
32 | else if (keyType === eosjs_numeric_1.KeyType.wa) {
|
33 | eosioRecoveryParam = ellipticSig.recoveryParam;
|
34 | }
|
35 | var sigData = new Uint8Array([eosioRecoveryParam].concat(r, s));
|
36 | if (!ec) {
|
37 | ec = eosjs_key_conversions_1.constructElliptic(keyType);
|
38 | }
|
39 | return new Signature({
|
40 | type: keyType,
|
41 | data: sigData,
|
42 | }, ec);
|
43 | };
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Signature.prototype.toElliptic = function () {
|
51 | var lengthOfR = 32;
|
52 | var lengthOfS = 32;
|
53 | var r = new BN(this.signature.data.slice(1, lengthOfR + 1));
|
54 | var s = new BN(this.signature.data.slice(lengthOfR + 1, lengthOfR + lengthOfS + 1));
|
55 | var ellipticRecoveryBitField;
|
56 | if (this.signature.type === eosjs_numeric_1.KeyType.k1 || this.signature.type === eosjs_numeric_1.KeyType.r1) {
|
57 | ellipticRecoveryBitField = this.signature.data[0] - 27;
|
58 | if (ellipticRecoveryBitField > 3) {
|
59 | ellipticRecoveryBitField -= 4;
|
60 | }
|
61 | }
|
62 | else if (this.signature.type === eosjs_numeric_1.KeyType.wa) {
|
63 | ellipticRecoveryBitField = this.signature.data[0];
|
64 | }
|
65 | var recoveryParam = ellipticRecoveryBitField & 3;
|
66 | return { r: r, s: s, recoveryParam: recoveryParam };
|
67 | };
|
68 |
|
69 | Signature.prototype.toString = function () {
|
70 | return eosjs_numeric_1.signatureToString(this.signature);
|
71 | };
|
72 |
|
73 | Signature.prototype.toBinary = function () {
|
74 | return this.signature.data;
|
75 | };
|
76 |
|
77 | Signature.prototype.getType = function () {
|
78 | return this.signature.type;
|
79 | };
|
80 |
|
81 | Signature.prototype.verify = function (data, publicKey, shouldHash, encoding) {
|
82 | if (shouldHash === void 0) { shouldHash = true; }
|
83 | if (encoding === void 0) { encoding = 'utf8'; }
|
84 | if (shouldHash) {
|
85 | if (typeof data === 'string') {
|
86 | data = Buffer.from(data, encoding);
|
87 | }
|
88 | data = this.ec.hash().update(data).digest();
|
89 | }
|
90 | var ellipticSignature = this.toElliptic();
|
91 | var ellipticPublicKey = publicKey.toElliptic();
|
92 | return this.ec.verify(data, ellipticSignature, ellipticPublicKey, encoding);
|
93 | };
|
94 |
|
95 | Signature.prototype.recover = function (data, shouldHash, encoding) {
|
96 | if (shouldHash === void 0) { shouldHash = true; }
|
97 | if (encoding === void 0) { encoding = 'utf8'; }
|
98 | if (shouldHash) {
|
99 | if (typeof data === 'string') {
|
100 | data = Buffer.from(data, encoding);
|
101 | }
|
102 | data = this.ec.hash().update(data).digest();
|
103 | }
|
104 | var ellipticSignature = this.toElliptic();
|
105 | var recoveredPublicKey = this.ec.recoverPubKey(data, ellipticSignature, ellipticSignature.recoveryParam, encoding);
|
106 | var ellipticKPub = this.ec.keyFromPublic(recoveredPublicKey);
|
107 | return eosjs_key_conversions_1.PublicKey.fromElliptic(ellipticKPub, this.getType(), this.ec);
|
108 | };
|
109 | return Signature;
|
110 | }());
|
111 | exports.Signature = Signature;
|
112 |
|
\ | No newline at end of file |