@dhedge/v2-sdk 1.9.5 → 1.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,172 +0,0 @@
1
- /* eslint-disable @typescript-eslint/ban-ts-comment */
2
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
3
- /* eslint-disable @typescript-eslint/no-explicit-any */
4
- // Shamelessly adapted from OpenZeppelin-contracts test utils
5
- import BigNumber from "bignumber.js";
6
- import { keccak256, keccakFromString, bufferToHex } from "ethereumjs-util";
7
- import { hexToBytes, soliditySha3 } from "web3-utils";
8
- import { scale } from "./index";
9
-
10
- // Merkle tree called with 32 byte hex values
11
- export class MerkleTree {
12
- public elements: any;
13
- public layers: any;
14
-
15
- constructor(elements: any[]) {
16
- this.elements = elements
17
- .filter((el: any) => el)
18
- .map(el => Buffer.from(hexToBytes(el)));
19
-
20
- // Sort elements
21
- this.elements.sort(Buffer.compare);
22
- // Deduplicate elements
23
- this.elements = this.bufDedup(this.elements);
24
-
25
- // Create layers
26
- this.layers = this.getLayers(this.elements);
27
- }
28
-
29
- getLayers(elements: string | any[]) {
30
- if (elements.length === 0) {
31
- return [[""]];
32
- }
33
-
34
- const layers = [];
35
- layers.push(elements);
36
-
37
- // Get next layer until we reach the root=
38
- while (layers[layers.length - 1].length > 1) {
39
- // @ts-ignore
40
- layers.push(this.getNextLayer(layers[layers.length - 1]));
41
- }
42
-
43
- return layers;
44
- }
45
-
46
- getNextLayer(elements: any[]) {
47
- return elements.reduce(
48
- (layer: any[], el: any, idx: number, arr: { [x: string]: any }) => {
49
- if (idx % 2 === 0) {
50
- // Hash the current element with its pair element
51
- layer.push(this.combinedHash(el, arr[idx + 1]));
52
- }
53
-
54
- return layer;
55
- },
56
- []
57
- );
58
- }
59
-
60
- combinedHash(first: any, second: any) {
61
- if (!first) {
62
- return second;
63
- }
64
- if (!second) {
65
- return first;
66
- }
67
-
68
- return keccak256(this.sortAndConcat(first, second));
69
- }
70
-
71
- getRoot() {
72
- return this.layers[this.layers.length - 1][0];
73
- }
74
-
75
- getHexRoot() {
76
- return bufferToHex(this.getRoot());
77
- }
78
-
79
- getProof(el: any) {
80
- let idx = this.bufIndexOf(el, this.elements);
81
-
82
- if (idx === -1) {
83
- throw new Error("Element does not exist in Merkle tree");
84
- }
85
-
86
- return this.layers.reduce((proof: any[], layer: any) => {
87
- const pairElement = this.getPairElement(idx, layer);
88
-
89
- if (pairElement) {
90
- proof.push(pairElement);
91
- }
92
-
93
- idx = Math.floor(idx / 2);
94
-
95
- return proof;
96
- }, []);
97
- }
98
-
99
- // external call - convert to buffer
100
- getHexProof(_el: any) {
101
- const el = Buffer.from(hexToBytes(_el));
102
-
103
- const proof = this.getProof(el);
104
-
105
- return this.bufArrToHexArr(proof);
106
- }
107
-
108
- getPairElement(idx: number, layer: string | any[]) {
109
- const pairIdx = idx % 2 === 0 ? idx + 1 : idx - 1;
110
-
111
- if (pairIdx < layer.length) {
112
- return layer[pairIdx];
113
- } else {
114
- return null;
115
- }
116
- }
117
-
118
- bufIndexOf(el: string | any[], arr: string | any[]) {
119
- let hash;
120
-
121
- // Convert element to 32 byte hash if it is not one already
122
- if (el.length !== 32 || !Buffer.isBuffer(el)) {
123
- hash = keccakFromString(el as string);
124
- } else {
125
- hash = el;
126
- }
127
-
128
- for (let i = 0; i < arr.length; i++) {
129
- if (hash.equals(arr[i])) {
130
- return i;
131
- }
132
- }
133
-
134
- return -1;
135
- }
136
-
137
- bufDedup(elements: any[]) {
138
- return elements.filter((el: any, idx: number) => {
139
- return idx === 0 || !elements[idx - 1].equals(el);
140
- });
141
- }
142
-
143
- bufArrToHexArr(arr: any[]) {
144
- if (arr.some((el: any) => !Buffer.isBuffer(el))) {
145
- throw new Error("Array is not an array of buffers");
146
- }
147
-
148
- return arr.map(
149
- (el: { toString: (arg0: string) => string }) => "0x" + el.toString("hex")
150
- );
151
- }
152
-
153
- sortAndConcat(...args: any[]) {
154
- return Buffer.concat([...args].sort(Buffer.compare));
155
- }
156
- }
157
-
158
- export function loadTree(
159
- balances: { [x: string]: string | BigNumber },
160
- decimals = 18
161
- ) {
162
- const elements: (string | null)[] = [];
163
- Object.keys(balances).forEach(address => {
164
- const balance: string = scale(balances[address], decimals).toString(10);
165
- const leaf = soliditySha3(
166
- { t: "address", v: address },
167
- { t: "uint", v: balance }
168
- );
169
- elements.push(leaf);
170
- });
171
- return new MerkleTree(elements);
172
- }