@silvana-one/mina-utils 0.2.0

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 (174) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1 -0
  3. package/dist/node/config.d.ts +4 -0
  4. package/dist/node/config.js +5 -0
  5. package/dist/node/config.js.map +1 -0
  6. package/dist/node/index.cjs +1325 -0
  7. package/dist/node/index.d.ts +6 -0
  8. package/dist/node/index.js +7 -0
  9. package/dist/node/index.js.map +1 -0
  10. package/dist/node/networks.d.ts +32 -0
  11. package/dist/node/networks.js +81 -0
  12. package/dist/node/networks.js.map +1 -0
  13. package/dist/node/storage/index.d.ts +2 -0
  14. package/dist/node/storage/index.js +3 -0
  15. package/dist/node/storage/index.js.map +1 -0
  16. package/dist/node/storage/ipfs.d.ts +5 -0
  17. package/dist/node/storage/ipfs.js +16 -0
  18. package/dist/node/storage/ipfs.js.map +1 -0
  19. package/dist/node/storage/pinata.d.ts +6 -0
  20. package/dist/node/storage/pinata.js +40 -0
  21. package/dist/node/storage/pinata.js.map +1 -0
  22. package/dist/node/transactions/account.d.ts +4 -0
  23. package/dist/node/transactions/account.js +44 -0
  24. package/dist/node/transactions/account.js.map +1 -0
  25. package/dist/node/transactions/blockberry.d.ts +21 -0
  26. package/dist/node/transactions/blockberry.js +130 -0
  27. package/dist/node/transactions/blockberry.js.map +1 -0
  28. package/dist/node/transactions/chain.d.ts +1 -0
  29. package/dist/node/transactions/chain.js +2 -0
  30. package/dist/node/transactions/chain.js.map +1 -0
  31. package/dist/node/transactions/index.d.ts +8 -0
  32. package/dist/node/transactions/index.js +9 -0
  33. package/dist/node/transactions/index.js.map +1 -0
  34. package/dist/node/transactions/nonce.d.ts +17 -0
  35. package/dist/node/transactions/nonce.js +91 -0
  36. package/dist/node/transactions/nonce.js.map +1 -0
  37. package/dist/node/transactions/send.d.ts +31 -0
  38. package/dist/node/transactions/send.js +154 -0
  39. package/dist/node/transactions/send.js.map +1 -0
  40. package/dist/node/transactions/tiny-contract.d.ts +5 -0
  41. package/dist/node/transactions/tiny-contract.js +22 -0
  42. package/dist/node/transactions/tiny-contract.js.map +1 -0
  43. package/dist/node/transactions/transaction.d.ts +21 -0
  44. package/dist/node/transactions/transaction.js +222 -0
  45. package/dist/node/transactions/transaction.js.map +1 -0
  46. package/dist/node/transactions/txstatus.d.ts +8 -0
  47. package/dist/node/transactions/txstatus.js +16 -0
  48. package/dist/node/transactions/txstatus.js.map +1 -0
  49. package/dist/node/utils/base64-field.d.ts +5 -0
  50. package/dist/node/utils/base64-field.js +29 -0
  51. package/dist/node/utils/base64-field.js.map +1 -0
  52. package/dist/node/utils/base64.d.ts +6 -0
  53. package/dist/node/utils/base64.js +83 -0
  54. package/dist/node/utils/base64.js.map +1 -0
  55. package/dist/node/utils/fee.d.ts +6 -0
  56. package/dist/node/utils/fee.js +11 -0
  57. package/dist/node/utils/fee.js.map +1 -0
  58. package/dist/node/utils/fetch.d.ts +44 -0
  59. package/dist/node/utils/fetch.js +101 -0
  60. package/dist/node/utils/fetch.js.map +1 -0
  61. package/dist/node/utils/fields.d.ts +13 -0
  62. package/dist/node/utils/fields.js +39 -0
  63. package/dist/node/utils/fields.js.map +1 -0
  64. package/dist/node/utils/index.d.ts +8 -0
  65. package/dist/node/utils/index.js +9 -0
  66. package/dist/node/utils/index.js.map +1 -0
  67. package/dist/node/utils/indexed-map.d.ts +32 -0
  68. package/dist/node/utils/indexed-map.js +124 -0
  69. package/dist/node/utils/indexed-map.js.map +1 -0
  70. package/dist/node/utils/mina.d.ts +39 -0
  71. package/dist/node/utils/mina.js +123 -0
  72. package/dist/node/utils/mina.js.map +1 -0
  73. package/dist/node/utils/utils.d.ts +8 -0
  74. package/dist/node/utils/utils.js +61 -0
  75. package/dist/node/utils/utils.js.map +1 -0
  76. package/dist/tsconfig.tsbuildinfo +1 -0
  77. package/dist/tsconfig.web.tsbuildinfo +1 -0
  78. package/dist/web/config.d.ts +4 -0
  79. package/dist/web/config.js +5 -0
  80. package/dist/web/config.js.map +1 -0
  81. package/dist/web/index.d.ts +6 -0
  82. package/dist/web/index.js +7 -0
  83. package/dist/web/index.js.map +1 -0
  84. package/dist/web/networks.d.ts +32 -0
  85. package/dist/web/networks.js +81 -0
  86. package/dist/web/networks.js.map +1 -0
  87. package/dist/web/storage/index.d.ts +2 -0
  88. package/dist/web/storage/index.js +3 -0
  89. package/dist/web/storage/index.js.map +1 -0
  90. package/dist/web/storage/ipfs.d.ts +5 -0
  91. package/dist/web/storage/ipfs.js +16 -0
  92. package/dist/web/storage/ipfs.js.map +1 -0
  93. package/dist/web/storage/pinata.d.ts +6 -0
  94. package/dist/web/storage/pinata.js +40 -0
  95. package/dist/web/storage/pinata.js.map +1 -0
  96. package/dist/web/transactions/account.d.ts +4 -0
  97. package/dist/web/transactions/account.js +44 -0
  98. package/dist/web/transactions/account.js.map +1 -0
  99. package/dist/web/transactions/blockberry.d.ts +21 -0
  100. package/dist/web/transactions/blockberry.js +130 -0
  101. package/dist/web/transactions/blockberry.js.map +1 -0
  102. package/dist/web/transactions/chain.d.ts +1 -0
  103. package/dist/web/transactions/chain.js +2 -0
  104. package/dist/web/transactions/chain.js.map +1 -0
  105. package/dist/web/transactions/index.d.ts +8 -0
  106. package/dist/web/transactions/index.js +9 -0
  107. package/dist/web/transactions/index.js.map +1 -0
  108. package/dist/web/transactions/nonce.d.ts +17 -0
  109. package/dist/web/transactions/nonce.js +91 -0
  110. package/dist/web/transactions/nonce.js.map +1 -0
  111. package/dist/web/transactions/send.d.ts +31 -0
  112. package/dist/web/transactions/send.js +154 -0
  113. package/dist/web/transactions/send.js.map +1 -0
  114. package/dist/web/transactions/tiny-contract.d.ts +5 -0
  115. package/dist/web/transactions/tiny-contract.js +22 -0
  116. package/dist/web/transactions/tiny-contract.js.map +1 -0
  117. package/dist/web/transactions/transaction.d.ts +21 -0
  118. package/dist/web/transactions/transaction.js +222 -0
  119. package/dist/web/transactions/transaction.js.map +1 -0
  120. package/dist/web/transactions/txstatus.d.ts +8 -0
  121. package/dist/web/transactions/txstatus.js +16 -0
  122. package/dist/web/transactions/txstatus.js.map +1 -0
  123. package/dist/web/utils/base64-field.d.ts +5 -0
  124. package/dist/web/utils/base64-field.js +29 -0
  125. package/dist/web/utils/base64-field.js.map +1 -0
  126. package/dist/web/utils/base64.d.ts +6 -0
  127. package/dist/web/utils/base64.js +83 -0
  128. package/dist/web/utils/base64.js.map +1 -0
  129. package/dist/web/utils/fee.d.ts +6 -0
  130. package/dist/web/utils/fee.js +11 -0
  131. package/dist/web/utils/fee.js.map +1 -0
  132. package/dist/web/utils/fetch.d.ts +44 -0
  133. package/dist/web/utils/fetch.js +101 -0
  134. package/dist/web/utils/fetch.js.map +1 -0
  135. package/dist/web/utils/fields.d.ts +13 -0
  136. package/dist/web/utils/fields.js +39 -0
  137. package/dist/web/utils/fields.js.map +1 -0
  138. package/dist/web/utils/index.d.ts +8 -0
  139. package/dist/web/utils/index.js +9 -0
  140. package/dist/web/utils/index.js.map +1 -0
  141. package/dist/web/utils/indexed-map.d.ts +32 -0
  142. package/dist/web/utils/indexed-map.js +124 -0
  143. package/dist/web/utils/indexed-map.js.map +1 -0
  144. package/dist/web/utils/mina.d.ts +39 -0
  145. package/dist/web/utils/mina.js +123 -0
  146. package/dist/web/utils/mina.js.map +1 -0
  147. package/dist/web/utils/utils.d.ts +8 -0
  148. package/dist/web/utils/utils.js +61 -0
  149. package/dist/web/utils/utils.js.map +1 -0
  150. package/package.json +62 -0
  151. package/src/config.ts +5 -0
  152. package/src/index.ts +6 -0
  153. package/src/networks.ts +130 -0
  154. package/src/storage/index.ts +2 -0
  155. package/src/storage/ipfs.ts +20 -0
  156. package/src/storage/pinata.ts +56 -0
  157. package/src/transactions/account.ts +57 -0
  158. package/src/transactions/blockberry.ts +198 -0
  159. package/src/transactions/chain.ts +1 -0
  160. package/src/transactions/index.ts +8 -0
  161. package/src/transactions/nonce.ts +121 -0
  162. package/src/transactions/send.ts +228 -0
  163. package/src/transactions/tiny-contract.ts +9 -0
  164. package/src/transactions/transaction.ts +319 -0
  165. package/src/transactions/txstatus.ts +26 -0
  166. package/src/utils/base64-field.ts +34 -0
  167. package/src/utils/base64.ts +87 -0
  168. package/src/utils/fee.ts +11 -0
  169. package/src/utils/fetch.ts +130 -0
  170. package/src/utils/fields.ts +39 -0
  171. package/src/utils/index.ts +8 -0
  172. package/src/utils/indexed-map.ts +170 -0
  173. package/src/utils/mina.ts +171 -0
  174. package/src/utils/utils.ts +79 -0
@@ -0,0 +1,11 @@
1
+ import { UInt64 } from "o1js";
2
+ import config from "../config.js";
3
+ /**
4
+ * Calculate the fee for a transaction
5
+ * @returns the fee for a transaction
6
+ */
7
+ export async function fee() {
8
+ //TODO: update after mainnet launch and resolution of the issue https://github.com/o1-labs/o1js/issues/1626
9
+ return UInt64.fromJSON(config.MINAFEE);
10
+ }
11
+ //# sourceMappingURL=fee.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fee.js","sourceRoot":"","sources":["../../../src/utils/fee.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO,MAAM,MAAM,cAAc,CAAC;AAElC;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,GAAG;IACvB,2GAA2G;IAC3G,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC"}
@@ -0,0 +1,44 @@
1
+ import { PublicKey, Field } from "o1js";
2
+ /**
3
+ * Fetches the Mina account for a given public key with error handling
4
+ * @param params the parameters for fetching the account
5
+ * @param params.publicKey the public key of the account
6
+ * @param params.tokenId the token id of the account
7
+ * @param params.force whether to force the fetch - use it only if you are sure the account exists
8
+ * @returns the account object
9
+ */
10
+ export declare function fetchMinaAccount(params: {
11
+ publicKey: string | PublicKey;
12
+ tokenId?: string | Field | undefined;
13
+ force?: boolean;
14
+ }): Promise<{
15
+ account: undefined;
16
+ } | {
17
+ account: import("node_modules/o1js/dist/node/bindings/mina-transaction/gen/transaction.js").Account;
18
+ error: undefined;
19
+ }>;
20
+ /**
21
+ * Fetches the Mina actions for a given public key with error handling
22
+ * @param publicKey the public key of the contract
23
+ * @param fromActionState the starting action state
24
+ * @param endActionState the ending action state
25
+ * @returns the actions array
26
+ */
27
+ export declare function fetchMinaActions(publicKey: PublicKey, fromActionState: Field, endActionState?: Field): Promise<{
28
+ actions: string[][];
29
+ hash: string;
30
+ }[] | undefined>;
31
+ /**
32
+ * Fetches the Mina transaction for a given hash with error handling
33
+ * @param hash the hash of the transaction
34
+ * @returns the transaction object
35
+ */
36
+ export declare function checkMinaZkappTransaction(hash: string): Promise<{
37
+ success: boolean;
38
+ failureReason: string[][][];
39
+ } | {
40
+ success: boolean;
41
+ failureReason: null;
42
+ } | {
43
+ success: boolean;
44
+ }>;
@@ -0,0 +1,101 @@
1
+ import { Mina, fetchAccount, checkZkappTransaction, } from "o1js";
2
+ import { sleep } from "./utils.js";
3
+ /**
4
+ * Fetches the Mina account for a given public key with error handling
5
+ * @param params the parameters for fetching the account
6
+ * @param params.publicKey the public key of the account
7
+ * @param params.tokenId the token id of the account
8
+ * @param params.force whether to force the fetch - use it only if you are sure the account exists
9
+ * @returns the account object
10
+ */
11
+ export async function fetchMinaAccount(params) {
12
+ const { publicKey, tokenId, force = false } = params;
13
+ const timeout = 1000 * 60 * 3; // 3 minutes
14
+ let attempt = 0;
15
+ const startTime = Date.now();
16
+ let result = { account: undefined };
17
+ while (Date.now() - startTime < timeout) {
18
+ try {
19
+ const result = await fetchAccount({
20
+ publicKey,
21
+ tokenId,
22
+ }, undefined, { timeout: 5 * 1000 });
23
+ return result;
24
+ }
25
+ catch (error) {
26
+ if (force === true)
27
+ console.log("Error in fetchMinaAccount:", {
28
+ error,
29
+ publicKey: typeof publicKey === "string" ? publicKey : publicKey.toBase58(),
30
+ tokenId: tokenId?.toString(),
31
+ force,
32
+ });
33
+ else {
34
+ console.log("fetchMinaAccount error", {
35
+ error,
36
+ publicKey: typeof publicKey === "string" ? publicKey : publicKey.toBase58(),
37
+ tokenId: tokenId?.toString(),
38
+ force,
39
+ });
40
+ return result;
41
+ }
42
+ }
43
+ attempt++;
44
+ await sleep(1000 * 6 * attempt); // to handle rate limit we increase the interval
45
+ }
46
+ if (force === true)
47
+ throw new Error(`fetchMinaAccount timeout
48
+ ${{
49
+ publicKey: typeof publicKey === "string" ? publicKey : publicKey.toBase58(),
50
+ tokenId: tokenId?.toString(),
51
+ force,
52
+ }}`);
53
+ else
54
+ console.log("fetchMinaAccount timeout", typeof publicKey === "string" ? publicKey : publicKey.toBase58(), tokenId?.toString(), force);
55
+ return result;
56
+ }
57
+ /**
58
+ * Fetches the Mina actions for a given public key with error handling
59
+ * @param publicKey the public key of the contract
60
+ * @param fromActionState the starting action state
61
+ * @param endActionState the ending action state
62
+ * @returns the actions array
63
+ */
64
+ export async function fetchMinaActions(publicKey, fromActionState, endActionState) {
65
+ const timeout = 1000 * 60 * 600; // 10 hours
66
+ const startTime = Date.now();
67
+ while (Date.now() - startTime < timeout) {
68
+ try {
69
+ let actions = await Mina.fetchActions(publicKey, {
70
+ fromActionState,
71
+ endActionState,
72
+ });
73
+ if (Array.isArray(actions))
74
+ return actions;
75
+ else
76
+ console.log("Cannot fetch actions - wrong format");
77
+ }
78
+ catch (error) {
79
+ console.log("Error in fetchMinaActions", error.toString().substring(0, 300));
80
+ }
81
+ await sleep(1000 * 60 * 2);
82
+ }
83
+ console.log("Timeout in fetchMinaActions");
84
+ return undefined;
85
+ }
86
+ /**
87
+ * Fetches the Mina transaction for a given hash with error handling
88
+ * @param hash the hash of the transaction
89
+ * @returns the transaction object
90
+ */
91
+ export async function checkMinaZkappTransaction(hash) {
92
+ try {
93
+ const result = await checkZkappTransaction(hash);
94
+ return result;
95
+ }
96
+ catch (error) {
97
+ console.error("Error in checkZkappTransaction:", error);
98
+ return { success: false };
99
+ }
100
+ }
101
+ //# sourceMappingURL=fetch.js.map
@@ -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CAAC,CAAC;YACH,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;gBAAE,OAAO,OAAO,CAAC;;gBACtC,OAAO,CAAC,GAAG,CAAC,qCAAqC,CAAC,CAAC;QAC1D,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACpB,OAAO,CAAC,GAAG,CACT,2BAA2B,EAC3B,KAAK,CAAC,QAAQ,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,CACnC,CAAC;QACJ,CAAC;QACD,MAAM,KAAK,CAAC,IAAI,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;IAC3C,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAAC,IAAY;IAC1D,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,qBAAqB,CAAC,IAAI,CAAC,CAAC;QACjD,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;QACxD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;AACH,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { Field } from "o1js";
2
+ /**
3
+ * Serialize fields to a string using base64 URL-friendly encoding
4
+ * @param fields the fields array to serialize
5
+ * @returns the serialized string
6
+ */
7
+ export declare function serializeFields(fields: Field[]): string;
8
+ /**
9
+ * Deserialize fields from a string using base64 URL-friendly encoding
10
+ * @param s the string to deserialize
11
+ * @returns the deserialized fields array
12
+ */
13
+ export declare function deserializeFields(s: string): Field[];
@@ -0,0 +1,39 @@
1
+ import { Field, Poseidon } from "o1js";
2
+ import { fieldToBase64, fieldFromBase64 } from "./base64-field.js";
3
+ /**
4
+ * Serialize fields to a string using base64 URL-friendly encoding
5
+ * @param fields the fields array to serialize
6
+ * @returns the serialized string
7
+ */
8
+ export function serializeFields(fields) {
9
+ const hash = Poseidon.hash(fields);
10
+ const value = [Field(fields.length), hash, ...fields];
11
+ //return value.map((f) => f.toBigInt().toString(36)).join(".");
12
+ return value.map((f) => fieldToBase64(f)).join(".");
13
+ }
14
+ /**
15
+ * Deserialize fields from a string using base64 URL-friendly encoding
16
+ * @param s the string to deserialize
17
+ * @returns the deserialized fields array
18
+ */
19
+ export function deserializeFields(s) {
20
+ try {
21
+ //const value = s.split(".").map((n) => Field(BigInt(convert(n, 36))));
22
+ const value = s.split(".").map((n) => fieldFromBase64(n));
23
+ const length = value[0];
24
+ if (Field(value.length - 2)
25
+ .equals(length)
26
+ .toBoolean() === false)
27
+ throw new Error("deserializeFields: invalid length");
28
+ const hash = Poseidon.hash(value.slice(2));
29
+ if (hash.equals(value[1]).toBoolean()) {
30
+ return value.slice(2);
31
+ }
32
+ else
33
+ throw new Error("deserializeFields: invalid hash: data mismatch");
34
+ }
35
+ catch (e) {
36
+ throw new Error(`deserializeFields: invalid string: ${s}: ${e}`);
37
+ }
38
+ }
39
+ //# sourceMappingURL=fields.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fields.js","sourceRoot":"","sources":["../../../src/utils/fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEnE;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,MAAe;IAC7C,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACnC,MAAM,KAAK,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC,CAAC;IACtD,+DAA+D;IAC/D,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACtD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,CAAS;IACzC,IAAI,CAAC;QACH,uEAAuE;QACvE,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1D,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACxB,IACE,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;aACpB,MAAM,CAAC,MAAM,CAAC;aACd,SAAS,EAAE,KAAK,KAAK;YAExB,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;QACvD,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3C,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC;YACtC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACxB,CAAC;;YAAM,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IAC3E,CAAC;IAAC,OAAO,CAAM,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACnE,CAAC;AACH,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from "./base64-field.js";
2
+ export * from "./base64.js";
3
+ export * from "./fetch.js";
4
+ export * from "./fields.js";
5
+ export * from "./fee.js";
6
+ export * from "./mina.js";
7
+ export * from "./indexed-map.js";
8
+ export * from "./utils.js";
@@ -0,0 +1,9 @@
1
+ export * from "./base64-field.js";
2
+ export * from "./base64.js";
3
+ export * from "./fetch.js";
4
+ export * from "./fields.js";
5
+ export * from "./fee.js";
6
+ export * from "./mina.js";
7
+ export * from "./indexed-map.js";
8
+ export * from "./utils.js";
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { Experimental } from "o1js";
2
+ declare const IndexedMerkleMap: typeof import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMap;
3
+ type IndexedMerkleMap = Experimental.IndexedMerkleMap;
4
+ export interface IndexedMapSerialized {
5
+ height: number;
6
+ root: string;
7
+ length: string;
8
+ nodes: string;
9
+ sortedLeaves: string;
10
+ }
11
+ export declare function loadIndexedMerkleMap(params: {
12
+ url: string;
13
+ type: ReturnType<typeof IndexedMerkleMap>;
14
+ timeout?: number;
15
+ attempts?: number;
16
+ }): Promise<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase>;
17
+ export declare function saveIndexedMerkleMap(params: {
18
+ map: IndexedMerkleMap;
19
+ name?: string;
20
+ keyvalues?: {
21
+ key: string;
22
+ value: string;
23
+ }[];
24
+ auth: string;
25
+ }): Promise<string | undefined>;
26
+ export declare function serializeIndexedMap(map: IndexedMerkleMap): IndexedMapSerialized;
27
+ export declare function deserializeIndexedMerkleMap(params: {
28
+ serializedIndexedMap: IndexedMapSerialized;
29
+ type?: ReturnType<typeof IndexedMerkleMap>;
30
+ }): InstanceType<ReturnType<typeof IndexedMerkleMap>> | undefined;
31
+ export declare function parseIndexedMapSerialized(serializedMap: string): IndexedMapSerialized;
32
+ export {};
@@ -0,0 +1,124 @@
1
+ import { Experimental, Field } from "o1js";
2
+ import { bigintToBase64, bigintFromBase64 } from "./base64.js";
3
+ import { sleep } from "./utils.js";
4
+ import { pinJSON } from "../storage/pinata.js";
5
+ const { IndexedMerkleMap } = Experimental;
6
+ export async function loadIndexedMerkleMap(params) {
7
+ const { url, type, timeout = 60000, attempts = 5 } = params;
8
+ let attempt = 0;
9
+ const start = Date.now();
10
+ let response = await fetch(url);
11
+ while (!response.ok && attempt < attempts && Date.now() - start < timeout) {
12
+ attempt++;
13
+ await sleep(5000 * attempt); // handle rate limiting
14
+ response = await fetch(url);
15
+ }
16
+ if (!response.ok) {
17
+ throw new Error("Failed to fetch IndexedMerkleMap");
18
+ }
19
+ const json = await response.json();
20
+ const serializedIndexedMap = json.map;
21
+ if (!serializedIndexedMap)
22
+ throw new Error("wrong IndexedMerkleMap json format");
23
+ const map = deserializeIndexedMerkleMapInternal({
24
+ serializedIndexedMap,
25
+ type,
26
+ });
27
+ if (!map) {
28
+ throw new Error("Failed to deserialize whitelist");
29
+ }
30
+ return map;
31
+ }
32
+ export async function saveIndexedMerkleMap(params) {
33
+ const { map, name = "indexed-map", keyvalues, auth } = params;
34
+ const serialized = serializeIndexedMap(map);
35
+ const ipfsHash = await pinJSON({
36
+ data: { map: serialized },
37
+ name,
38
+ keyvalues,
39
+ auth,
40
+ });
41
+ return ipfsHash;
42
+ }
43
+ export function serializeIndexedMap(map) {
44
+ return {
45
+ height: map.height,
46
+ root: map.root.toJSON(),
47
+ length: map.length.toJSON(),
48
+ nodes: JSON.stringify(map.data.get().nodes, (_, v) => typeof v === "bigint" ? "n" + bigintToBase64(v) : v),
49
+ sortedLeaves: JSON.stringify(map.data
50
+ .get()
51
+ .sortedLeaves.map((v) => [
52
+ bigintToBase64(v.key),
53
+ bigintToBase64(v.nextKey),
54
+ bigintToBase64(v.value),
55
+ bigintToBase64(BigInt(v.index)),
56
+ ])),
57
+ };
58
+ }
59
+ export function deserializeIndexedMerkleMap(params) {
60
+ try {
61
+ const { serializedIndexedMap, type } = params;
62
+ return deserializeIndexedMerkleMapInternal({
63
+ serializedIndexedMap,
64
+ type: type ?? IndexedMerkleMap(serializedIndexedMap.height),
65
+ });
66
+ }
67
+ catch (error) {
68
+ console.error("Error deserializing map:", error?.message ?? error);
69
+ return undefined;
70
+ }
71
+ }
72
+ export function parseIndexedMapSerialized(serializedMap) {
73
+ const json = JSON.parse(serializedMap);
74
+ if (json.height === undefined ||
75
+ json.root === undefined ||
76
+ json.length === undefined ||
77
+ json.nodes === undefined ||
78
+ json.sortedLeaves === undefined)
79
+ throw new Error("wrong IndexedMerkleMap json format");
80
+ if (typeof json.height !== "number")
81
+ throw new Error("wrong IndexedMerkleMap height format");
82
+ if (typeof json.root !== "string")
83
+ throw new Error("wrong IndexedMerkleMap root format");
84
+ if (typeof json.length !== "string")
85
+ throw new Error("wrong IndexedMerkleMap length format");
86
+ if (typeof json.nodes !== "string")
87
+ throw new Error("wrong IndexedMerkleMap nodes format");
88
+ if (typeof json.sortedLeaves !== "string")
89
+ throw new Error("wrong IndexedMerkleMap sortedLeaves format");
90
+ return json;
91
+ }
92
+ function deserializeIndexedMerkleMapInternal(params) {
93
+ const { serializedIndexedMap, type } = params;
94
+ const map = new type();
95
+ if (serializedIndexedMap.height !== map.height) {
96
+ throw new Error("wrong IndexedMap height");
97
+ }
98
+ const nodes = JSON.parse(serializedIndexedMap.nodes, (_, v) => {
99
+ // Check if the value is a string that represents a BigInt
100
+ if (typeof v === "string" && v[0] === "n") {
101
+ // Remove the first 'n' and convert the string to a BigInt
102
+ return bigintFromBase64(v.slice(1));
103
+ }
104
+ return v;
105
+ });
106
+ const sortedLeaves = JSON.parse(serializedIndexedMap.sortedLeaves).map((row) => {
107
+ return {
108
+ key: bigintFromBase64(row[0]),
109
+ nextKey: bigintFromBase64(row[1]),
110
+ value: bigintFromBase64(row[2]),
111
+ index: Number(bigintFromBase64(row[3])),
112
+ };
113
+ });
114
+ map.root = Field.fromJSON(serializedIndexedMap.root);
115
+ map.length = Field.fromJSON(serializedIndexedMap.length);
116
+ map.data.updateAsProver(() => {
117
+ return {
118
+ nodes: nodes.map((row) => [...row]),
119
+ sortedLeaves: [...sortedLeaves],
120
+ };
121
+ });
122
+ return map;
123
+ }
124
+ //# sourceMappingURL=indexed-map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"indexed-map.js","sourceRoot":"","sources":["../../../src/utils/indexed-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,MAAM,EAAE,gBAAgB,EAAE,GAAG,YAAY,CAAC;AAW1C,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,MAK1C;IACC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,GAAG,KAAK,EAAE,QAAQ,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC;IAC5D,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACzB,IAAI,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,EAAE,IAAI,OAAO,GAAG,QAAQ,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,OAAO,EAAE,CAAC;QAC1E,OAAO,EAAE,CAAC;QACV,MAAM,KAAK,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,uBAAuB;QACpD,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IACD,IAAI,CAAC,QAAQ,CAAC,EAA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,CACpE,CAAC,GAAQ,EAAE,EAAE;QACX,OAAO;YACL,GAAG,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC7B,OAAO,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACjC,KAAK,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC/B,KAAK,EAAE,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;SACxC,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;IACrD,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;IACzD,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE;QAC3B,OAAO;YACL,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;YACxC,YAAY,EAAE,CAAC,GAAG,YAAY,CAAC;SAChC,CAAC;IACJ,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,39 @@
1
+ export { initBlockchain, accountBalance, accountBalanceMina, MinaNetworkInstance, currentNetwork, getNetworkIdHash, getCurrentNetwork, getDeployer, };
2
+ import { Mina, PublicKey, UInt64, Field } from "o1js";
3
+ import { blockchain, MinaNetwork } from "../networks.js";
4
+ /**
5
+ * MinaNetworkInstance is the data structure for a Mina network instance, keeping track of the keys, network, and network ID hash.
6
+ */
7
+ interface MinaNetworkInstance {
8
+ /** The keys for the deployers */
9
+ keys: Mina.TestPublicKey[];
10
+ /** The network */
11
+ network: MinaNetwork;
12
+ /** The network ID hash */
13
+ networkIdHash: Field;
14
+ }
15
+ declare let currentNetwork: MinaNetworkInstance | undefined;
16
+ declare function getNetworkIdHash(): Field;
17
+ declare function getCurrentNetwork(): MinaNetworkInstance;
18
+ declare function getDeployer(): Mina.TestPublicKey | undefined;
19
+ /**
20
+ * Initializes the Mina blockchain network
21
+ * Due to the limitations of the Mina SDK, only one network can be initialized at a time
22
+ * This function should be called before any other Mina functions
23
+ * @param instance the blockchain instance to initialize
24
+ * @param deployersNumber the number of deployers to use for the network (only for local and lightnet networks)
25
+ * @returns the Mina network instance
26
+ */
27
+ declare function initBlockchain(instance: blockchain, deployersNumber?: number, proofsEnabled?: boolean): Promise<MinaNetworkInstance>;
28
+ /**
29
+ * Fetches the account balance for a given public key
30
+ * @param address the public key
31
+ * @returns the account balance
32
+ */
33
+ declare function accountBalance(address: PublicKey): Promise<UInt64>;
34
+ /**
35
+ * Fetches the account balance for a given public key and returns it in Mina
36
+ * @param address the public key
37
+ * @returns the account balance in MINA
38
+ */
39
+ declare function accountBalanceMina(address: PublicKey): Promise<number>;
@@ -0,0 +1,123 @@
1
+ export { initBlockchain, accountBalance, accountBalanceMina, currentNetwork, getNetworkIdHash, getCurrentNetwork, getDeployer, };
2
+ import { Mina, PrivateKey, UInt64, fetchAccount, Lightnet, CircuitString, } from "o1js";
3
+ import { networks, Local } from "../networks.js";
4
+ let currentNetwork = undefined;
5
+ function getNetworkIdHash() {
6
+ if (currentNetwork === undefined) {
7
+ throw new Error("Network is not initialized");
8
+ }
9
+ return currentNetwork.networkIdHash;
10
+ }
11
+ function getCurrentNetwork() {
12
+ if (currentNetwork === undefined) {
13
+ throw new Error("Network is not initialized");
14
+ }
15
+ return currentNetwork;
16
+ }
17
+ function getDeployer() {
18
+ if (currentNetwork === undefined) {
19
+ throw new Error("Network is not initialized");
20
+ }
21
+ if (currentNetwork.keys.length < 1)
22
+ return undefined;
23
+ return currentNetwork.keys[0];
24
+ }
25
+ /**
26
+ * Initializes the Mina blockchain network
27
+ * Due to the limitations of the Mina SDK, only one network can be initialized at a time
28
+ * This function should be called before any other Mina functions
29
+ * @param instance the blockchain instance to initialize
30
+ * @param deployersNumber the number of deployers to use for the network (only for local and lightnet networks)
31
+ * @returns the Mina network instance
32
+ */
33
+ async function initBlockchain(instance, deployersNumber = 0, proofsEnabled = true) {
34
+ /*
35
+ if (instance === "mainnet") {
36
+ throw new Error("Mainnet is not supported yet by zkApps");
37
+ }
38
+ */
39
+ if (currentNetwork !== undefined) {
40
+ if (currentNetwork?.network.chainId === instance) {
41
+ return currentNetwork;
42
+ }
43
+ else {
44
+ throw new Error(`Network is already initialized to different chain ${currentNetwork.network.chainId}, cannot initialize to ${instance}`);
45
+ }
46
+ }
47
+ const networkIdHash = CircuitString.fromString(instance).hash();
48
+ // await used for compatibility with future versions of o1js
49
+ if (instance === "local") {
50
+ const local = await Mina.LocalBlockchain({
51
+ proofsEnabled,
52
+ });
53
+ Mina.setActiveInstance(local);
54
+ if (deployersNumber > local.testAccounts.length)
55
+ throw new Error("Not enough test accounts");
56
+ currentNetwork = {
57
+ keys: local.testAccounts,
58
+ network: Local,
59
+ networkIdHash,
60
+ };
61
+ return currentNetwork;
62
+ }
63
+ const network = networks.find((n) => n.chainId === instance);
64
+ if (network === undefined) {
65
+ throw new Error("Unknown network");
66
+ }
67
+ const networkInstance = Mina.Network({
68
+ mina: network.mina,
69
+ archive: network.archive,
70
+ lightnetAccountManager: network.accountManager,
71
+ networkId: instance === "mainnet" ? "mainnet" : "testnet",
72
+ });
73
+ Mina.setActiveInstance(networkInstance);
74
+ const keys = [];
75
+ if (deployersNumber > 0) {
76
+ if (instance === "lightnet") {
77
+ for (let i = 0; i < deployersNumber; i++) {
78
+ const keyPair = await Lightnet.acquireKeyPair();
79
+ const key = Mina.TestPublicKey(keyPair.privateKey);
80
+ keys.push(key);
81
+ }
82
+ }
83
+ else {
84
+ const deployers = process.env.DEPLOYERS;
85
+ if (deployers === undefined ||
86
+ Array.isArray(deployers) === false ||
87
+ deployers.length < deployersNumber)
88
+ throw new Error("Deployers are not set");
89
+ for (let i = 0; i < deployersNumber; i++) {
90
+ const privateKey = PrivateKey.fromBase58(deployers[i]);
91
+ const key = Mina.TestPublicKey(privateKey);
92
+ keys.push(key);
93
+ }
94
+ }
95
+ }
96
+ currentNetwork = {
97
+ keys,
98
+ network,
99
+ networkIdHash,
100
+ };
101
+ return currentNetwork;
102
+ }
103
+ /**
104
+ * Fetches the account balance for a given public key
105
+ * @param address the public key
106
+ * @returns the account balance
107
+ */
108
+ async function accountBalance(address) {
109
+ await fetchAccount({ publicKey: address });
110
+ if (Mina.hasAccount(address))
111
+ return Mina.getBalance(address);
112
+ else
113
+ return UInt64.from(0);
114
+ }
115
+ /**
116
+ * Fetches the account balance for a given public key and returns it in Mina
117
+ * @param address the public key
118
+ * @returns the account balance in MINA
119
+ */
120
+ async function accountBalanceMina(address) {
121
+ return Number((await accountBalance(address)).toBigInt()) / 1e9;
122
+ }
123
+ //# sourceMappingURL=mina.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mina.js","sourceRoot":"","sources":["../../../src/utils/mina.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,kBAAkB,EAElB,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,GACZ,CAAC;AAEF,OAAO,EACL,IAAI,EAEJ,UAAU,EACV,MAAM,EACN,YAAY,EAEZ,QAAQ,EACR,aAAa,GACd,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,QAAQ,EAA2B,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAgB1E,IAAI,cAAc,GAAoC,SAAS,CAAC;AAEhE,SAAS,gBAAgB;IACvB,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,CAAC;IACD,OAAO,cAAc,CAAC,aAAa,CAAC;AACtC,CAAC;AAED,SAAS,iBAAiB;IACxB,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,4BAA4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K,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,eAAe,EAAE,CAAC,EAAE,EAAE,CAAC;gBACzC,MAAM,UAAU,GAAG,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;gBACvD,MAAM,GAAG,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;gBAC3C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACjB,CAAC;QACH,CAAC;IACH,CAAC;IAED,cAAc,GAAG;QACf,IAAI;QACJ,OAAO;QACP,aAAa;KACd,CAAC;IACF,OAAO,cAAc,CAAC;AACxB,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,cAAc,CAAC,OAAkB;IAC9C,MAAM,YAAY,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3C,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC;QAAE,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;;QACzD,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC7B,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,kBAAkB,CAAC,OAAkB;IAClD,OAAO,MAAM,CAAC,CAAC,MAAM,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,GAAG,GAAG,CAAC;AAClE,CAAC"}
@@ -0,0 +1,8 @@
1
+ export declare function sleep(ms: number): Promise<unknown>;
2
+ export declare function makeString(length: number): string;
3
+ export declare function formatTime(ms: number): string;
4
+ export declare class Memory {
5
+ static rss: number;
6
+ constructor();
7
+ static info(description?: string, fullInfo?: boolean): void;
8
+ }