@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.
Files changed (102) hide show
  1. package/.vscode/settings.json +5 -0
  2. package/CHANGELOG.md +4 -0
  3. package/build.ts +23 -0
  4. package/bun.lockb +0 -0
  5. package/dist/abi.d.ts +1 -2
  6. package/dist/abi.js +569 -563
  7. package/dist/address.d.ts +0 -1
  8. package/dist/address.js +22 -21
  9. package/dist/api.d.ts +0 -1
  10. package/dist/api.js +235 -120
  11. package/dist/app.d.ts +1 -2
  12. package/dist/app.js +2030 -276
  13. package/dist/availability.d.ts +0 -1
  14. package/dist/availability.js +126 -56
  15. package/dist/cli.d.ts +0 -1
  16. package/dist/cli.js +28 -24
  17. package/dist/const.d.ts +0 -1
  18. package/dist/const.js +153 -132
  19. package/dist/databricks.d.ts +0 -1
  20. package/dist/databricks.js +198 -58
  21. package/dist/date.d.ts +0 -1
  22. package/dist/date.js +48 -21
  23. package/dist/deploy.d.ts +0 -1
  24. package/dist/deploy.js +427 -292
  25. package/dist/dynamodb.d.ts +3 -4
  26. package/dist/dynamodb.js +432 -281
  27. package/dist/env.d.ts +2 -3
  28. package/dist/env.js +16 -9
  29. package/dist/graphql.d.ts +0 -1
  30. package/dist/graphql.js +26 -23
  31. package/dist/indexer.d.ts +0 -1
  32. package/dist/indexer.js +1050 -441
  33. package/dist/log.d.ts +0 -1
  34. package/dist/log.js +53 -52
  35. package/dist/object-hash.d.ts +0 -1
  36. package/dist/object-hash.js +49 -59
  37. package/dist/opsgenie.d.ts +1 -1
  38. package/dist/opsgenie.js +31 -30
  39. package/dist/por.d.ts +0 -1
  40. package/dist/por.js +113 -123
  41. package/dist/s3.d.ts +7 -8
  42. package/dist/s3.js +103 -91
  43. package/dist/search.d.ts +0 -1
  44. package/dist/search.js +100 -25
  45. package/dist/selector.d.ts +0 -1
  46. package/dist/selector.js +34 -38
  47. package/dist/slack.d.ts +0 -1
  48. package/dist/slack.js +27 -21
  49. package/dist/util.d.ts +0 -1
  50. package/dist/util.js +21 -20
  51. package/examples/api.ts +1 -1
  52. package/examples/indexer.ts +1 -1
  53. package/examples/mode-indexer.ts +1 -1
  54. package/package.json +5 -4
  55. package/{graphql.ts → src/graphql.ts} +1 -1
  56. package/{opsgenie.ts → src/opsgenie.ts} +2 -1
  57. package/tsconfig.build.json +2 -5
  58. package/tsconfig.json +11 -20
  59. package/dist/abi.d.ts.map +0 -1
  60. package/dist/address.d.ts.map +0 -1
  61. package/dist/api.d.ts.map +0 -1
  62. package/dist/app.d.ts.map +0 -1
  63. package/dist/availability.d.ts.map +0 -1
  64. package/dist/cli.d.ts.map +0 -1
  65. package/dist/const.d.ts.map +0 -1
  66. package/dist/databricks.d.ts.map +0 -1
  67. package/dist/date.d.ts.map +0 -1
  68. package/dist/deploy.d.ts.map +0 -1
  69. package/dist/dynamodb.d.ts.map +0 -1
  70. package/dist/env.d.ts.map +0 -1
  71. package/dist/graphql.d.ts.map +0 -1
  72. package/dist/indexer.d.ts.map +0 -1
  73. package/dist/log.d.ts.map +0 -1
  74. package/dist/object-hash.d.ts.map +0 -1
  75. package/dist/opsgenie.d.ts.map +0 -1
  76. package/dist/por.d.ts.map +0 -1
  77. package/dist/s3.d.ts.map +0 -1
  78. package/dist/search.d.ts.map +0 -1
  79. package/dist/selector.d.ts.map +0 -1
  80. package/dist/slack.d.ts.map +0 -1
  81. package/dist/util.d.ts.map +0 -1
  82. /package/{abi.ts → src/abi.ts} +0 -0
  83. /package/{address.ts → src/address.ts} +0 -0
  84. /package/{api.ts → src/api.ts} +0 -0
  85. /package/{app.ts → src/app.ts} +0 -0
  86. /package/{availability.ts → src/availability.ts} +0 -0
  87. /package/{cli.ts → src/cli.ts} +0 -0
  88. /package/{const.ts → src/const.ts} +0 -0
  89. /package/{databricks.ts → src/databricks.ts} +0 -0
  90. /package/{date.ts → src/date.ts} +0 -0
  91. /package/{deploy.ts → src/deploy.ts} +0 -0
  92. /package/{dynamodb.ts → src/dynamodb.ts} +0 -0
  93. /package/{env.ts → src/env.ts} +0 -0
  94. /package/{indexer.ts → src/indexer.ts} +0 -0
  95. /package/{log.ts → src/log.ts} +0 -0
  96. /package/{object-hash.ts → src/object-hash.ts} +0 -0
  97. /package/{por.ts → src/por.ts} +0 -0
  98. /package/{s3.ts → src/s3.ts} +0 -0
  99. /package/{search.ts → src/search.ts} +0 -0
  100. /package/{selector.ts → src/selector.ts} +0 -0
  101. /package/{slack.ts → src/slack.ts} +0 -0
  102. /package/{util.ts → src/util.ts} +0 -0
package/dist/log.d.ts CHANGED
@@ -11,4 +11,3 @@ declare const logger: {
11
11
  error: (...args: unknown[]) => void;
12
12
  };
13
13
  export { print, getLine, inline, logger };
14
- //# sourceMappingURL=log.d.ts.map
package/dist/log.js CHANGED
@@ -1,62 +1,63 @@
1
+ // src/log.ts
1
2
  function isObject(a) {
2
- return !!a && a.constructor === Object;
3
+ return !!a && a.constructor === Object;
3
4
  }
4
5
  function print(o) {
5
- if (Array.isArray(o)) {
6
- return `[${o.map(print).join(", ")}]`;
7
- }
8
- if (isObject(o)) {
9
- return `{${Object.keys(o)
10
- .map((k) => `${k}: ${o[k]}`)
11
- .join(", ")}}`;
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
- let line = "";
17
- // Convert to string and filter out newline to tabs (AWS Athena)
18
- for (let i = 0, l = params.length; i < l; i++) {
19
- // Certain objects don't get converted
20
- // Note using JSON.stringfy may be too slow for large objects
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
- return new Date().toISOString();
22
+ return new Date().toISOString();
27
23
  }
28
- const inline = {
29
- debug: function (...args) {
30
- if (process.env.NODE_ENV === "development") {
31
- console.log(`${timestamp()} ${getLine(args)}`);
32
- }
33
- },
34
- log: function (...args) {
35
- if (process.env.NODE_ENV !== "test") {
36
- console.log(`${timestamp()} ${getLine(args)}`);
37
- }
38
- },
39
- error: function (...args) {
40
- if (process.env.NODE_ENV !== "test") {
41
- console.error(`${timestamp()} ${getLine(args)}`);
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
- const logger = {
46
- debug: function (...args) {
47
- if (process.env.NODE_ENV === "development") {
48
- console.log(`[${timestamp()}]`, ...args);
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 };
@@ -1,2 +1 @@
1
1
  export declare function getHash(obj: unknown): string;
2
- //# sourceMappingURL=object-hash.d.ts.map
@@ -1,71 +1,61 @@
1
+ // src/object-hash.ts
1
2
  import xh from "@node-rs/xxhash";
2
- export function getHash(obj) {
3
- const xxh3 = xh.xxh3.Xxh3.withSeed();
4
- hash(obj, xxh3);
5
- return xxh3.digest().toString(16);
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
- if (obj === null) {
9
- xxh3.update("null");
10
- }
11
- else if (obj === undefined) {
12
- xxh3.update("undefined");
13
- }
14
- else if (typeof obj === "string") {
15
- xxh3.update(obj);
16
- }
17
- else if (typeof obj === "number") {
18
- xxh3.update(obj.toString());
19
- }
20
- else if (typeof obj === "boolean") {
21
- xxh3.update(obj.toString());
22
- }
23
- else if (typeof obj === "bigint") {
24
- xxh3.update(obj.toString());
25
- }
26
- else if (obj instanceof Date) {
27
- xxh3.update(obj.toISOString());
28
- }
29
- else if (Array.isArray(obj)) {
30
- arrayHash(obj, xxh3);
31
- }
32
- else if (obj instanceof Set) {
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
- xxh3.update("[");
47
- for (const obj of array) {
48
- hash(obj, xxh3);
49
- }
50
- xxh3.update("]");
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
- throw new Error("Set hashing not implemented");
43
+ throw new Error("Set hashing not implemented");
55
44
  }
56
45
  function mapHash(map, xxh3) {
57
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
- const array = Array.from(map.entries()).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
59
- for (const [key, value] of array) {
60
- hash(key, xxh3);
61
- hash(value, xxh3);
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
- const array = Object.entries(obj).sort(([aKey], [bKey]) => aKey.localeCompare(bKey));
67
- for (const [key, value] of array) {
68
- hash(key, xxh3);
69
- hash(value, xxh3);
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
+ };
@@ -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
- return key || process.env["SKYNET_OPSGENIE_API_KEY"];
4
+ return key || process.env["SKYNET_OPSGENIE_API_KEY"];
4
5
  }
5
6
  function getGenieEndPoint() {
6
- return process.env["SKYNET_OPSGENIE_END_POINT"] || "https://api.opsgenie.com/v2/alerts";
7
+ return process.env["SKYNET_OPSGENIE_END_POINT"] || "https://api.opsgenie.com/v2/alerts";
7
8
  }
8
- export async function postGenieMessage(body, apiKey, verbose) {
9
- try {
10
- const genieKey = apiKey || getGenieKey();
11
- const genieEndPoint = getGenieEndPoint();
12
- // Prevents duplicate alerts (See Opsgenie doc about alias)
13
- if (!body.alias) {
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
- catch (error) {
35
- console.error("Failed to make opsgenie API call", error);
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
@@ -35,4 +35,3 @@ export declare class MerkleTreePoR {
35
35
  generateProof(userHash: string, token?: string): MerkleProof;
36
36
  static verifyProof(merkleProof: MerkleProof): boolean;
37
37
  }
38
- //# sourceMappingURL=por.d.ts.map
package/dist/por.js CHANGED
@@ -1,130 +1,120 @@
1
- import { CryptoHasher } from "bun";
2
- export class MerkleTreePoR {
3
- constructor(users, hashFunction) {
4
- this.leaves = [];
5
- this.tree = [];
6
- this.userMap = new Map();
7
- this.userData = [];
8
- this.userData = [...users];
9
- this.hashFunction = hashFunction || this.defaultSHA256Hash;
10
- this.buildTree();
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
- // Default SHA-256 hash function
13
- static defaultSHA256Hash(data) {
14
- const hasher = new CryptoHasher("sha256");
15
- hasher.update(data);
16
- return hasher.digest("hex");
17
- }
18
- defaultSHA256Hash(data) {
19
- return MerkleTreePoR.defaultSHA256Hash(data);
20
- }
21
- // Configurable hash function
22
- hash(data) {
23
- return this.hashFunction(data);
24
- }
25
- // hash user data
26
- createLeafHash(user) {
27
- const leafData = `${user.userHash}:${user.balance}:${user.token || ''}`;
28
- return this.hash(leafData);
29
- }
30
- buildTree() {
31
- if (this.userData.length === 0) {
32
- throw new Error('Cannot build tree with empty user data');
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
- // Start with leaf level
41
- let currentLevel = this.leaves;
42
- this.tree.push(currentLevel.map(hash => ({ hash })));
43
- // Build tree bottom-up
44
- while (currentLevel.length > 1) {
45
- const nextLevel = [];
46
- const nextLevelNodes = [];
47
- for (let i = 0; i < currentLevel.length; i += 2) {
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
- // get root hash of created tree
69
- getRootHash() {
70
- if (this.tree.length === 0) {
71
- throw new Error('Merkle tree not built');
72
- }
73
- return this.tree[this.tree.length - 1][0].hash;
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
- // generate merkle proof
76
- generateProof(userHash, token) {
77
- const key = `${userHash}:${token || ''}`;
78
- const leafIndex = this.userMap.get(key);
79
- if (leafIndex === undefined) {
80
- throw new Error(`${userHash} not found in merkle tree for token ${token}`);
81
- }
82
- const user = this.userData[leafIndex];
83
- const proof = [];
84
- const positions = [];
85
- let currentIndex = leafIndex;
86
- // Walk up the tree collecting sibling hashes
87
- for (let level = 0; level < this.tree.length - 1; level++) {
88
- const currentLevelSize = this.tree[level].length;
89
- const isRightNode = currentIndex % 2 === 1;
90
- const siblingIndex = isRightNode ? currentIndex - 1 : currentIndex + 1;
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
- // verify inclusion using proof
114
- static verifyProof(merkleProof) {
115
- const leafData = `${merkleProof.userHash}:${merkleProof.balance}:${merkleProof.token || ''}`;
116
- const hashFunction = merkleProof.hashFunction || MerkleTreePoR.defaultSHA256Hash;
117
- let currentHash = hashFunction(leafData);
118
- for (let i = 0; i < merkleProof.proof.length; i++) {
119
- const siblingHash = merkleProof.proof[i];
120
- const position = merkleProof.positions[i];
121
- if (position === 'left') {
122
- currentHash = hashFunction(siblingHash + currentHash);
123
- }
124
- else {
125
- currentHash = hashFunction(currentHash + siblingHash);
126
- }
127
- }
128
- return currentHash === merkleProof.rootHash;
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<any>;
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: any[];
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