1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.rlphash = exports.ripemd160FromArray = exports.ripemd160FromString = exports.ripemd160 = exports.sha256FromArray = exports.sha256FromString = exports.sha256 = exports.keccakFromArray = exports.keccakFromHexString = exports.keccakFromString = exports.keccak256 = exports.keccak = void 0;
|
23 | const keccak_1 = require("ethereum-cryptography/keccak");
|
24 | const createHash = require('create-hash');
|
25 | const rlp = __importStar(require("rlp"));
|
26 | const bytes_1 = require("./bytes");
|
27 | const helpers_1 = require("./helpers");
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const keccak = function (a, bits = 256) {
|
34 | (0, helpers_1.assertIsBuffer)(a);
|
35 | switch (bits) {
|
36 | case 224: {
|
37 | return (0, keccak_1.keccak224)(a);
|
38 | }
|
39 | case 256: {
|
40 | return (0, keccak_1.keccak256)(a);
|
41 | }
|
42 | case 384: {
|
43 | return (0, keccak_1.keccak384)(a);
|
44 | }
|
45 | case 512: {
|
46 | return (0, keccak_1.keccak512)(a);
|
47 | }
|
48 | default: {
|
49 | throw new Error(`Invald algorithm: keccak${bits}`);
|
50 | }
|
51 | }
|
52 | };
|
53 | exports.keccak = keccak;
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | const keccak256 = function (a) {
|
59 | return (0, exports.keccak)(a);
|
60 | };
|
61 | exports.keccak256 = keccak256;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | const keccakFromString = function (a, bits = 256) {
|
68 | (0, helpers_1.assertIsString)(a);
|
69 | const buf = Buffer.from(a, 'utf8');
|
70 | return (0, exports.keccak)(buf, bits);
|
71 | };
|
72 | exports.keccakFromString = keccakFromString;
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | const keccakFromHexString = function (a, bits = 256) {
|
79 | (0, helpers_1.assertIsHexString)(a);
|
80 | return (0, exports.keccak)((0, bytes_1.toBuffer)(a), bits);
|
81 | };
|
82 | exports.keccakFromHexString = keccakFromHexString;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | const keccakFromArray = function (a, bits = 256) {
|
89 | (0, helpers_1.assertIsArray)(a);
|
90 | return (0, exports.keccak)((0, bytes_1.toBuffer)(a), bits);
|
91 | };
|
92 | exports.keccakFromArray = keccakFromArray;
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | const _sha256 = function (a) {
|
98 | a = (0, bytes_1.toBuffer)(a);
|
99 | return createHash('sha256').update(a).digest();
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const sha256 = function (a) {
|
106 | (0, helpers_1.assertIsBuffer)(a);
|
107 | return _sha256(a);
|
108 | };
|
109 | exports.sha256 = sha256;
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | const sha256FromString = function (a) {
|
115 | (0, helpers_1.assertIsString)(a);
|
116 | return _sha256(a);
|
117 | };
|
118 | exports.sha256FromString = sha256FromString;
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | const sha256FromArray = function (a) {
|
124 | (0, helpers_1.assertIsArray)(a);
|
125 | return _sha256(a);
|
126 | };
|
127 | exports.sha256FromArray = sha256FromArray;
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | const _ripemd160 = function (a, padded) {
|
134 | a = (0, bytes_1.toBuffer)(a);
|
135 | const hash = createHash('rmd160').update(a).digest();
|
136 | if (padded === true) {
|
137 | return (0, bytes_1.setLengthLeft)(hash, 32);
|
138 | }
|
139 | else {
|
140 | return hash;
|
141 | }
|
142 | };
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | const ripemd160 = function (a, padded) {
|
149 | (0, helpers_1.assertIsBuffer)(a);
|
150 | return _ripemd160(a, padded);
|
151 | };
|
152 | exports.ripemd160 = ripemd160;
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | const ripemd160FromString = function (a, padded) {
|
159 | (0, helpers_1.assertIsString)(a);
|
160 | return _ripemd160(a, padded);
|
161 | };
|
162 | exports.ripemd160FromString = ripemd160FromString;
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | const ripemd160FromArray = function (a, padded) {
|
169 | (0, helpers_1.assertIsArray)(a);
|
170 | return _ripemd160(a, padded);
|
171 | };
|
172 | exports.ripemd160FromArray = ripemd160FromArray;
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | const rlphash = function (a) {
|
178 | return (0, exports.keccak)(rlp.encode(a));
|
179 | };
|
180 | exports.rlphash = rlphash;
|
181 |
|
\ | No newline at end of file |