@certik/skynet 0.22.1 → 0.22.2
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.
- package/.vscode/settings.json +5 -0
- package/CHANGELOG.md +4 -0
- package/build.ts +23 -0
- package/bun.lockb +0 -0
- package/dist/abi.d.ts +1 -2
- package/dist/abi.js +569 -563
- package/dist/address.d.ts +0 -1
- package/dist/address.js +22 -21
- package/dist/api.d.ts +0 -1
- package/dist/api.js +235 -120
- package/dist/app.d.ts +1 -2
- package/dist/app.js +2030 -276
- package/dist/availability.d.ts +0 -1
- package/dist/availability.js +126 -56
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +28 -24
- package/dist/const.d.ts +0 -1
- package/dist/const.js +153 -132
- package/dist/databricks.d.ts +0 -1
- package/dist/databricks.js +198 -58
- package/dist/date.d.ts +0 -1
- package/dist/date.js +48 -21
- package/dist/deploy.d.ts +0 -1
- package/dist/deploy.js +427 -292
- package/dist/dynamodb.d.ts +3 -4
- package/dist/dynamodb.js +432 -281
- package/dist/env.d.ts +2 -3
- package/dist/env.js +16 -9
- package/dist/graphql.d.ts +0 -1
- package/dist/graphql.js +26 -23
- package/dist/indexer.d.ts +0 -1
- package/dist/indexer.js +1050 -441
- package/dist/log.d.ts +0 -1
- package/dist/log.js +53 -52
- package/dist/object-hash.d.ts +0 -1
- package/dist/object-hash.js +49 -59
- package/dist/opsgenie.d.ts +1 -1
- package/dist/opsgenie.js +31 -30
- package/dist/por.d.ts +0 -1
- package/dist/por.js +113 -123
- package/dist/s3.d.ts +7 -8
- package/dist/s3.js +103 -91
- package/dist/search.d.ts +0 -1
- package/dist/search.js +100 -25
- package/dist/selector.d.ts +0 -1
- package/dist/selector.js +34 -38
- package/dist/slack.d.ts +0 -1
- package/dist/slack.js +27 -21
- package/dist/util.d.ts +0 -1
- package/dist/util.js +21 -20
- package/examples/api.ts +1 -1
- package/examples/indexer.ts +1 -1
- package/examples/mode-indexer.ts +1 -1
- package/package.json +5 -4
- package/{graphql.ts → src/graphql.ts} +1 -1
- package/{opsgenie.ts → src/opsgenie.ts} +2 -1
- package/tsconfig.build.json +2 -5
- package/tsconfig.json +11 -20
- package/dist/abi.d.ts.map +0 -1
- package/dist/address.d.ts.map +0 -1
- package/dist/api.d.ts.map +0 -1
- package/dist/app.d.ts.map +0 -1
- package/dist/availability.d.ts.map +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/const.d.ts.map +0 -1
- package/dist/databricks.d.ts.map +0 -1
- package/dist/date.d.ts.map +0 -1
- package/dist/deploy.d.ts.map +0 -1
- package/dist/dynamodb.d.ts.map +0 -1
- package/dist/env.d.ts.map +0 -1
- package/dist/graphql.d.ts.map +0 -1
- package/dist/indexer.d.ts.map +0 -1
- package/dist/log.d.ts.map +0 -1
- package/dist/object-hash.d.ts.map +0 -1
- package/dist/opsgenie.d.ts.map +0 -1
- package/dist/por.d.ts.map +0 -1
- package/dist/s3.d.ts.map +0 -1
- package/dist/search.d.ts.map +0 -1
- package/dist/selector.d.ts.map +0 -1
- package/dist/slack.d.ts.map +0 -1
- package/dist/util.d.ts.map +0 -1
- /package/{abi.ts → src/abi.ts} +0 -0
- /package/{address.ts → src/address.ts} +0 -0
- /package/{api.ts → src/api.ts} +0 -0
- /package/{app.ts → src/app.ts} +0 -0
- /package/{availability.ts → src/availability.ts} +0 -0
- /package/{cli.ts → src/cli.ts} +0 -0
- /package/{const.ts → src/const.ts} +0 -0
- /package/{databricks.ts → src/databricks.ts} +0 -0
- /package/{date.ts → src/date.ts} +0 -0
- /package/{deploy.ts → src/deploy.ts} +0 -0
- /package/{dynamodb.ts → src/dynamodb.ts} +0 -0
- /package/{env.ts → src/env.ts} +0 -0
- /package/{indexer.ts → src/indexer.ts} +0 -0
- /package/{log.ts → src/log.ts} +0 -0
- /package/{object-hash.ts → src/object-hash.ts} +0 -0
- /package/{por.ts → src/por.ts} +0 -0
- /package/{s3.ts → src/s3.ts} +0 -0
- /package/{search.ts → src/search.ts} +0 -0
- /package/{selector.ts → src/selector.ts} +0 -0
- /package/{slack.ts → src/slack.ts} +0 -0
- /package/{util.ts → src/util.ts} +0 -0
package/dist/log.d.ts
CHANGED
package/dist/log.js
CHANGED
|
@@ -1,62 +1,63 @@
|
|
|
1
|
+
// src/log.ts
|
|
1
2
|
function isObject(a) {
|
|
2
|
-
|
|
3
|
+
return !!a && a.constructor === Object;
|
|
3
4
|
}
|
|
4
5
|
function print(o) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
return `${o}`;
|
|
6
|
+
if (Array.isArray(o)) {
|
|
7
|
+
return `[${o.map(print).join(", ")}]`;
|
|
8
|
+
}
|
|
9
|
+
if (isObject(o)) {
|
|
10
|
+
return `{${Object.keys(o).map((k) => `${k}: ${o[k]}`).join(", ")}}`;
|
|
11
|
+
}
|
|
12
|
+
return `${o}`;
|
|
14
13
|
}
|
|
15
14
|
function getLine(params) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
line += `${print(params[i])} `.replace(/\n/gm, "\t");
|
|
22
|
-
}
|
|
23
|
-
return line.trim();
|
|
15
|
+
let line = "";
|
|
16
|
+
for (let i = 0, l = params.length;i < l; i++) {
|
|
17
|
+
line += `${print(params[i])} `.replace(/\n/gm, "\t");
|
|
18
|
+
}
|
|
19
|
+
return line.trim();
|
|
24
20
|
}
|
|
25
21
|
function timestamp() {
|
|
26
|
-
|
|
22
|
+
return new Date().toISOString();
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
24
|
+
var inline = {
|
|
25
|
+
debug: function(...args) {
|
|
26
|
+
if (true) {
|
|
27
|
+
console.log(`${timestamp()} ${getLine(args)}`);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
log: function(...args) {
|
|
31
|
+
if (true) {
|
|
32
|
+
console.log(`${timestamp()} ${getLine(args)}`);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
error: function(...args) {
|
|
36
|
+
if (true) {
|
|
37
|
+
console.error(`${timestamp()} ${getLine(args)}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var logger = {
|
|
42
|
+
debug: function(...args) {
|
|
43
|
+
if (true) {
|
|
44
|
+
console.log(`[${timestamp()}]`, ...args);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
log: function(...args) {
|
|
48
|
+
if (true) {
|
|
49
|
+
console.log(`[${timestamp()}]`, ...args);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
error: function(...args) {
|
|
53
|
+
if (true) {
|
|
54
|
+
console.error(`[${timestamp()}]`, ...args);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
44
57
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
},
|
|
51
|
-
log: function (...args) {
|
|
52
|
-
if (process.env.NODE_ENV !== "test") {
|
|
53
|
-
console.log(`[${timestamp()}]`, ...args);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
error: function (...args) {
|
|
57
|
-
if (process.env.NODE_ENV !== "test") {
|
|
58
|
-
console.error(`[${timestamp()}]`, ...args);
|
|
59
|
-
}
|
|
60
|
-
},
|
|
58
|
+
export {
|
|
59
|
+
print,
|
|
60
|
+
logger,
|
|
61
|
+
inline,
|
|
62
|
+
getLine
|
|
61
63
|
};
|
|
62
|
-
export { print, getLine, inline, logger };
|
package/dist/object-hash.d.ts
CHANGED
package/dist/object-hash.js
CHANGED
|
@@ -1,71 +1,61 @@
|
|
|
1
|
+
// src/object-hash.ts
|
|
1
2
|
import xh from "@node-rs/xxhash";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
function getHash(obj) {
|
|
4
|
+
const xxh3 = xh.xxh3.Xxh3.withSeed();
|
|
5
|
+
hash(obj, xxh3);
|
|
6
|
+
return xxh3.digest().toString(16);
|
|
6
7
|
}
|
|
7
8
|
function hash(obj, xxh3) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
setHash(obj, xxh3);
|
|
34
|
-
}
|
|
35
|
-
else if (obj instanceof Map) {
|
|
36
|
-
mapHash(obj, xxh3);
|
|
37
|
-
}
|
|
38
|
-
else if (typeof obj === "object") {
|
|
39
|
-
objectHash(obj, xxh3);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
throw new Error(`Unsupported type: ${obj}`);
|
|
43
|
-
}
|
|
9
|
+
if (obj === null) {
|
|
10
|
+
xxh3.update("null");
|
|
11
|
+
} else if (obj === undefined) {
|
|
12
|
+
xxh3.update("undefined");
|
|
13
|
+
} else if (typeof obj === "string") {
|
|
14
|
+
xxh3.update(obj);
|
|
15
|
+
} else if (typeof obj === "number") {
|
|
16
|
+
xxh3.update(obj.toString());
|
|
17
|
+
} else if (typeof obj === "boolean") {
|
|
18
|
+
xxh3.update(obj.toString());
|
|
19
|
+
} else if (typeof obj === "bigint") {
|
|
20
|
+
xxh3.update(obj.toString());
|
|
21
|
+
} else if (obj instanceof Date) {
|
|
22
|
+
xxh3.update(obj.toISOString());
|
|
23
|
+
} else if (Array.isArray(obj)) {
|
|
24
|
+
arrayHash(obj, xxh3);
|
|
25
|
+
} else if (obj instanceof Set) {
|
|
26
|
+
setHash(obj, xxh3);
|
|
27
|
+
} else if (obj instanceof Map) {
|
|
28
|
+
mapHash(obj, xxh3);
|
|
29
|
+
} else if (typeof obj === "object") {
|
|
30
|
+
objectHash(obj, xxh3);
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error(`Unsupported type: ${obj}`);
|
|
33
|
+
}
|
|
44
34
|
}
|
|
45
35
|
function arrayHash(array, xxh3) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
36
|
+
xxh3.update("[");
|
|
37
|
+
for (const obj of array) {
|
|
38
|
+
hash(obj, xxh3);
|
|
39
|
+
}
|
|
40
|
+
xxh3.update("]");
|
|
51
41
|
}
|
|
52
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
53
42
|
function setHash(_set, _xxh3) {
|
|
54
|
-
|
|
43
|
+
throw new Error("Set hashing not implemented");
|
|
55
44
|
}
|
|
56
45
|
function mapHash(map, xxh3) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
46
|
+
const array = Array.from(map.entries()).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
|
|
47
|
+
for (const [key, value] of array) {
|
|
48
|
+
hash(key, xxh3);
|
|
49
|
+
hash(value, xxh3);
|
|
50
|
+
}
|
|
63
51
|
}
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
65
52
|
function objectHash(obj, xxh3) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
53
|
+
const array = Object.entries(obj).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
|
|
54
|
+
for (const [key, value] of array) {
|
|
55
|
+
hash(key, xxh3);
|
|
56
|
+
hash(value, xxh3);
|
|
57
|
+
}
|
|
71
58
|
}
|
|
59
|
+
export {
|
|
60
|
+
getHash
|
|
61
|
+
};
|
package/dist/opsgenie.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ type OpsgenieResponse = {
|
|
|
11
11
|
};
|
|
12
12
|
took: number;
|
|
13
13
|
requestId: string;
|
|
14
|
+
result: string;
|
|
14
15
|
};
|
|
15
16
|
export declare function postGenieMessage(body: {
|
|
16
17
|
alias?: string;
|
|
@@ -18,4 +19,3 @@ export declare function postGenieMessage(body: {
|
|
|
18
19
|
description?: string;
|
|
19
20
|
}, apiKey?: string, verbose?: boolean): Promise<OpsgenieResponse>;
|
|
20
21
|
export {};
|
|
21
|
-
//# sourceMappingURL=opsgenie.d.ts.map
|
package/dist/opsgenie.js
CHANGED
|
@@ -1,38 +1,39 @@
|
|
|
1
|
+
// src/opsgenie.ts
|
|
1
2
|
import md5 from "md5";
|
|
2
3
|
function getGenieKey(key) {
|
|
3
|
-
|
|
4
|
+
return key || process.env["SKYNET_OPSGENIE_API_KEY"];
|
|
4
5
|
}
|
|
5
6
|
function getGenieEndPoint() {
|
|
6
|
-
|
|
7
|
+
return process.env["SKYNET_OPSGENIE_END_POINT"] || "https://api.opsgenie.com/v2/alerts";
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
body.alias = md5(body.message);
|
|
15
|
-
}
|
|
16
|
-
if (verbose) {
|
|
17
|
-
console.log(`Making API call to Opsgenie`, JSON.stringify(body, null, 2));
|
|
18
|
-
}
|
|
19
|
-
// Makes the call using fetch and ENV variables
|
|
20
|
-
const response = await fetch(genieEndPoint, {
|
|
21
|
-
method: "POST",
|
|
22
|
-
headers: {
|
|
23
|
-
"Content-Type": "application/json",
|
|
24
|
-
Authorization: `GenieKey ${genieKey}`,
|
|
25
|
-
},
|
|
26
|
-
body: JSON.stringify(body),
|
|
27
|
-
});
|
|
28
|
-
const result = await response.json();
|
|
29
|
-
if (verbose) {
|
|
30
|
-
console.log(`Result of API call to Opsgenie... ${result}`);
|
|
31
|
-
}
|
|
32
|
-
return result;
|
|
9
|
+
async function postGenieMessage(body, apiKey, verbose) {
|
|
10
|
+
try {
|
|
11
|
+
const genieKey = apiKey || getGenieKey();
|
|
12
|
+
const genieEndPoint = getGenieEndPoint();
|
|
13
|
+
if (!body.alias) {
|
|
14
|
+
body.alias = md5(body.message);
|
|
33
15
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
throw error;
|
|
16
|
+
if (verbose) {
|
|
17
|
+
console.log(`Making API call to Opsgenie`, JSON.stringify(body, null, 2));
|
|
37
18
|
}
|
|
19
|
+
const response = await fetch(genieEndPoint, {
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers: {
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
Authorization: `GenieKey ${genieKey}`
|
|
24
|
+
},
|
|
25
|
+
body: JSON.stringify(body)
|
|
26
|
+
});
|
|
27
|
+
const result = await response.json();
|
|
28
|
+
if (verbose) {
|
|
29
|
+
console.log(`Result of API call to Opsgenie... ${result}`);
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error("Failed to make opsgenie API call", error);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
38
36
|
}
|
|
37
|
+
export {
|
|
38
|
+
postGenieMessage
|
|
39
|
+
};
|
package/dist/por.d.ts
CHANGED
package/dist/por.js
CHANGED
|
@@ -1,130 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/por.ts
|
|
3
|
+
var {CryptoHasher } = globalThis.Bun;
|
|
4
|
+
|
|
5
|
+
class MerkleTreePoR {
|
|
6
|
+
leaves = [];
|
|
7
|
+
tree = [];
|
|
8
|
+
userMap = new Map;
|
|
9
|
+
userData = [];
|
|
10
|
+
hashFunction;
|
|
11
|
+
constructor(users, hashFunction) {
|
|
12
|
+
this.userData = [...users];
|
|
13
|
+
this.hashFunction = hashFunction || this.defaultSHA256Hash;
|
|
14
|
+
this.buildTree();
|
|
15
|
+
}
|
|
16
|
+
static defaultSHA256Hash(data) {
|
|
17
|
+
const hasher = new CryptoHasher("sha256");
|
|
18
|
+
hasher.update(data);
|
|
19
|
+
return hasher.digest("hex");
|
|
20
|
+
}
|
|
21
|
+
defaultSHA256Hash(data) {
|
|
22
|
+
return MerkleTreePoR.defaultSHA256Hash(data);
|
|
23
|
+
}
|
|
24
|
+
hash(data) {
|
|
25
|
+
return this.hashFunction(data);
|
|
26
|
+
}
|
|
27
|
+
createLeafHash(user) {
|
|
28
|
+
const leafData = `${user.userHash}:${user.balance}:${user.token || ""}`;
|
|
29
|
+
return this.hash(leafData);
|
|
30
|
+
}
|
|
31
|
+
buildTree() {
|
|
32
|
+
if (this.userData.length === 0) {
|
|
33
|
+
throw new Error("Cannot build tree with empty user data");
|
|
11
34
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
// Create leaf hashes and store user mapping
|
|
35
|
-
this.leaves = this.userData.map((user, index) => {
|
|
36
|
-
const leafHash = this.createLeafHash(user);
|
|
37
|
-
this.userMap.set(`${user.userHash}:${user.token || ''}`, index);
|
|
38
|
-
return leafHash;
|
|
35
|
+
this.leaves = this.userData.map((user, index) => {
|
|
36
|
+
const leafHash = this.createLeafHash(user);
|
|
37
|
+
this.userMap.set(`${user.userHash}:${user.token || ""}`, index);
|
|
38
|
+
return leafHash;
|
|
39
|
+
});
|
|
40
|
+
let currentLevel = this.leaves;
|
|
41
|
+
this.tree.push(currentLevel.map((hash) => ({ hash })));
|
|
42
|
+
while (currentLevel.length > 1) {
|
|
43
|
+
const nextLevel = [];
|
|
44
|
+
const nextLevelNodes = [];
|
|
45
|
+
for (let i = 0;i < currentLevel.length; i += 2) {
|
|
46
|
+
const left = currentLevel[i];
|
|
47
|
+
const right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left;
|
|
48
|
+
const parentHash = this.hash(left + right);
|
|
49
|
+
nextLevel.push(parentHash);
|
|
50
|
+
const leftNode = this.tree[this.tree.length - 1][i];
|
|
51
|
+
const rightNode = i + 1 < currentLevel.length ? this.tree[this.tree.length - 1][i + 1] : leftNode;
|
|
52
|
+
nextLevelNodes.push({
|
|
53
|
+
hash: parentHash,
|
|
54
|
+
left: leftNode,
|
|
55
|
+
right: rightNode
|
|
39
56
|
});
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const left = currentLevel[i];
|
|
49
|
-
const right = i + 1 < currentLevel.length
|
|
50
|
-
? currentLevel[i + 1]
|
|
51
|
-
: left; // Duplicate if odd number
|
|
52
|
-
const parentHash = this.hash(left + right);
|
|
53
|
-
nextLevel.push(parentHash);
|
|
54
|
-
const leftNode = this.tree[this.tree.length - 1][i];
|
|
55
|
-
const rightNode = i + 1 < currentLevel.length
|
|
56
|
-
? this.tree[this.tree.length - 1][i + 1]
|
|
57
|
-
: leftNode;
|
|
58
|
-
nextLevelNodes.push({
|
|
59
|
-
hash: parentHash,
|
|
60
|
-
left: leftNode,
|
|
61
|
-
right: rightNode
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
currentLevel = nextLevel;
|
|
65
|
-
this.tree.push(nextLevelNodes);
|
|
66
|
-
}
|
|
57
|
+
}
|
|
58
|
+
currentLevel = nextLevel;
|
|
59
|
+
this.tree.push(nextLevelNodes);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
getRootHash() {
|
|
63
|
+
if (this.tree.length === 0) {
|
|
64
|
+
throw new Error("Merkle tree not built");
|
|
67
65
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
66
|
+
return this.tree[this.tree.length - 1][0].hash;
|
|
67
|
+
}
|
|
68
|
+
generateProof(userHash, token) {
|
|
69
|
+
const key = `${userHash}:${token || ""}`;
|
|
70
|
+
const leafIndex = this.userMap.get(key);
|
|
71
|
+
if (leafIndex === undefined) {
|
|
72
|
+
throw new Error(`${userHash} not found in merkle tree for token ${token}`);
|
|
74
73
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (siblingIndex < currentLevelSize) {
|
|
92
|
-
proof.push(this.tree[level][siblingIndex].hash);
|
|
93
|
-
positions.push(isRightNode ? 'left' : 'right');
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
// When odd number, the last node is duplicated
|
|
97
|
-
proof.push(this.tree[level][currentIndex].hash);
|
|
98
|
-
positions.push(isRightNode ? 'left' : 'right');
|
|
99
|
-
}
|
|
100
|
-
currentIndex = Math.floor(currentIndex / 2);
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
userHash: user.userHash,
|
|
104
|
-
balance: user.balance,
|
|
105
|
-
token: user.token,
|
|
106
|
-
proof,
|
|
107
|
-
positions,
|
|
108
|
-
rootHash: this.getRootHash(),
|
|
109
|
-
leafIndex,
|
|
110
|
-
hashFunction: this.hashFunction !== this.defaultSHA256Hash ? this.hashFunction : undefined
|
|
111
|
-
};
|
|
74
|
+
const user = this.userData[leafIndex];
|
|
75
|
+
const proof = [];
|
|
76
|
+
const positions = [];
|
|
77
|
+
let currentIndex = leafIndex;
|
|
78
|
+
for (let level = 0;level < this.tree.length - 1; level++) {
|
|
79
|
+
const currentLevelSize = this.tree[level].length;
|
|
80
|
+
const isRightNode = currentIndex % 2 === 1;
|
|
81
|
+
const siblingIndex = isRightNode ? currentIndex - 1 : currentIndex + 1;
|
|
82
|
+
if (siblingIndex < currentLevelSize) {
|
|
83
|
+
proof.push(this.tree[level][siblingIndex].hash);
|
|
84
|
+
positions.push(isRightNode ? "left" : "right");
|
|
85
|
+
} else {
|
|
86
|
+
proof.push(this.tree[level][currentIndex].hash);
|
|
87
|
+
positions.push(isRightNode ? "left" : "right");
|
|
88
|
+
}
|
|
89
|
+
currentIndex = Math.floor(currentIndex / 2);
|
|
112
90
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
91
|
+
return {
|
|
92
|
+
userHash: user.userHash,
|
|
93
|
+
balance: user.balance,
|
|
94
|
+
token: user.token,
|
|
95
|
+
proof,
|
|
96
|
+
positions,
|
|
97
|
+
rootHash: this.getRootHash(),
|
|
98
|
+
leafIndex,
|
|
99
|
+
hashFunction: this.hashFunction !== this.defaultSHA256Hash ? this.hashFunction : undefined
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
static verifyProof(merkleProof) {
|
|
103
|
+
const leafData = `${merkleProof.userHash}:${merkleProof.balance}:${merkleProof.token || ""}`;
|
|
104
|
+
const hashFunction = merkleProof.hashFunction || MerkleTreePoR.defaultSHA256Hash;
|
|
105
|
+
let currentHash = hashFunction(leafData);
|
|
106
|
+
for (let i = 0;i < merkleProof.proof.length; i++) {
|
|
107
|
+
const siblingHash = merkleProof.proof[i];
|
|
108
|
+
const position = merkleProof.positions[i];
|
|
109
|
+
if (position === "left") {
|
|
110
|
+
currentHash = hashFunction(siblingHash + currentHash);
|
|
111
|
+
} else {
|
|
112
|
+
currentHash = hashFunction(currentHash + siblingHash);
|
|
113
|
+
}
|
|
129
114
|
}
|
|
115
|
+
return currentHash === merkleProof.rootHash;
|
|
116
|
+
}
|
|
130
117
|
}
|
|
118
|
+
export {
|
|
119
|
+
MerkleTreePoR
|
|
120
|
+
};
|
package/dist/s3.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { S3Client, ObjectCannedACL } from "@aws-sdk/client-s3";
|
|
2
2
|
declare function getS3(forceNew?: boolean): S3Client;
|
|
3
|
-
declare function readFile(bucketName: string, key: string, verbose?: boolean): Promise<string>;
|
|
3
|
+
declare function readFile(bucketName: string, key: string, verbose?: boolean): Promise<string | null | undefined>;
|
|
4
4
|
declare function hasFile(bucketName: string, key: string): Promise<boolean>;
|
|
5
|
-
declare function getFileSize(bucketName: string, key: string): Promise<number>;
|
|
5
|
+
declare function getFileSize(bucketName: string, key: string): Promise<number | undefined>;
|
|
6
6
|
declare function writeFile(bucketName: string, key: string, body: string, options?: {
|
|
7
7
|
verbose?: boolean;
|
|
8
8
|
skipIfExists?: boolean;
|
|
9
9
|
acl?: ObjectCannedACL;
|
|
10
10
|
contentType?: string;
|
|
11
11
|
}): Promise<void>;
|
|
12
|
-
declare function deleteFile(bucketName: string, key: string, verbose?: boolean): Promise<
|
|
12
|
+
declare function deleteFile(bucketName: string, key: string, verbose?: boolean): Promise<null | undefined>;
|
|
13
13
|
declare function listKeys(bucketName: string, prefix?: string, continuationToken?: string): Promise<{
|
|
14
|
-
keys:
|
|
14
|
+
keys: never[];
|
|
15
15
|
continuationToken?: undefined;
|
|
16
16
|
} | {
|
|
17
|
-
keys: string[];
|
|
18
|
-
continuationToken: string;
|
|
19
|
-
}>;
|
|
17
|
+
keys: (string | undefined)[];
|
|
18
|
+
continuationToken: string | undefined;
|
|
19
|
+
} | null>;
|
|
20
20
|
export { getS3, hasFile, readFile, writeFile, deleteFile, getFileSize, listKeys };
|
|
21
|
-
//# sourceMappingURL=s3.d.ts.map
|