zkcloudworker 0.18.29 → 0.20.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.
- package/dist/node/index.cjs +160 -1568
- package/dist/node/mina/api/api.js +15 -3
- package/dist/node/mina/api/api.js.map +1 -1
- package/dist/node/mina/token/api.js +1 -4
- package/dist/node/mina/token/api.js.map +1 -1
- package/dist/node/mina/token/index.d.ts +0 -7
- package/dist/node/mina/token/index.js +0 -7
- package/dist/node/mina/token/index.js.map +1 -1
- package/dist/node/mina/transactions/transaction.d.ts +15 -3
- package/dist/node/mina/transactions/transaction.js +49 -6
- package/dist/node/mina/transactions/transaction.js.map +1 -1
- package/dist/tsconfig.web.tsbuildinfo +1 -1
- package/dist/web/mina/api/api.js +15 -3
- package/dist/web/mina/api/api.js.map +1 -1
- package/dist/web/mina/token/api.js +1 -4
- package/dist/web/mina/token/api.js.map +1 -1
- package/dist/web/mina/token/index.d.ts +0 -7
- package/dist/web/mina/token/index.js +0 -7
- package/dist/web/mina/token/index.js.map +1 -1
- package/dist/web/mina/transactions/transaction.d.ts +15 -3
- package/dist/web/mina/transactions/transaction.js +49 -6
- package/dist/web/mina/transactions/transaction.js.map +1 -1
- package/package.json +5 -5
- package/src/mina/api/api.ts +15 -3
- package/src/mina/token/api.ts +2 -4
- package/src/mina/token/index.ts +0 -7
- package/src/mina/transactions/transaction.ts +73 -13
- package/src/mina/token/FungibleTokenAdmin.ts +0 -89
- package/src/mina/token/FungibleTokenContract.ts +0 -327
- package/src/mina/token/FungibleTokenWhitelistedAdmin.ts +0 -112
- package/src/mina/token/bid.ts +0 -173
- package/src/mina/token/build.ts +0 -620
- package/src/mina/token/fee.ts +0 -2
- package/src/mina/token/offer.ts +0 -174
- package/src/mina/token/token.ts +0 -127
- package/src/mina/token/vk.ts +0 -42
- package/src/mina/token/whitelist.ts +0 -204
@@ -1,204 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
Experimental,
|
3
|
-
Struct,
|
4
|
-
Field,
|
5
|
-
Option,
|
6
|
-
Provable,
|
7
|
-
PublicKey,
|
8
|
-
UInt64,
|
9
|
-
Poseidon,
|
10
|
-
Bool,
|
11
|
-
} from "o1js";
|
12
|
-
import {
|
13
|
-
serializeIndexedMap,
|
14
|
-
loadIndexedMerkleMap,
|
15
|
-
} from "../utils/indexed-map.js";
|
16
|
-
import { sleep } from "../../cloud/utils/utils.js";
|
17
|
-
import { Storage } from "../storage/storage.js";
|
18
|
-
import { createIpfsURL } from "../storage/ipfs.js";
|
19
|
-
import { pinJSON } from "../storage/pinata.js";
|
20
|
-
|
21
|
-
const { IndexedMerkleMap } = Experimental;
|
22
|
-
type IndexedMerkleMap = Experimental.IndexedMerkleMap;
|
23
|
-
const WHITELIST_HEIGHT = 20;
|
24
|
-
|
25
|
-
/** Represents the whitelist using an Indexed Merkle Map. */
|
26
|
-
export class WhitelistMap extends IndexedMerkleMap(WHITELIST_HEIGHT) {}
|
27
|
-
export class WhitelistMapOption extends Option(WhitelistMap) {}
|
28
|
-
export class UInt64Option extends Option(UInt64) {}
|
29
|
-
export class WhitelistedAddress extends Struct({
|
30
|
-
address: PublicKey,
|
31
|
-
amount: UInt64, // Maximum permitted amount of the transaction
|
32
|
-
}) {}
|
33
|
-
|
34
|
-
export type WhitelistedAddressList =
|
35
|
-
| WhitelistedAddress[]
|
36
|
-
| { address: string; amount?: number }[];
|
37
|
-
|
38
|
-
export class Whitelist extends Struct({
|
39
|
-
/** The root hash of the Merkle tree representing the whitelist. */
|
40
|
-
root: Field,
|
41
|
-
/** Off-chain storage information, typically an IPFS hash pointing to the whitelist data. */
|
42
|
-
storage: Storage,
|
43
|
-
}) {
|
44
|
-
isNone(): Bool {
|
45
|
-
return this.root
|
46
|
-
.equals(Field(0))
|
47
|
-
.or(Storage.equals(this.storage, Storage.empty()));
|
48
|
-
}
|
49
|
-
|
50
|
-
isSome(): Bool {
|
51
|
-
return this.isNone().not();
|
52
|
-
}
|
53
|
-
|
54
|
-
async load(): Promise<WhitelistMapOption> {
|
55
|
-
const isNone = this.isNone();
|
56
|
-
const map = await Provable.witnessAsync(WhitelistMapOption, async () => {
|
57
|
-
if (isNone.toBoolean()) return WhitelistMapOption.none();
|
58
|
-
else
|
59
|
-
return WhitelistMapOption.fromValue(
|
60
|
-
await loadIndexedMerkleMap({
|
61
|
-
url: createIpfsURL({ hash: this.storage.toString() }),
|
62
|
-
type: WhitelistMap,
|
63
|
-
})
|
64
|
-
);
|
65
|
-
});
|
66
|
-
isNone.assertEquals(map.isSome.not());
|
67
|
-
const root = Provable.if(
|
68
|
-
map.isSome,
|
69
|
-
map.orElse(new WhitelistMap()).root,
|
70
|
-
Field(0)
|
71
|
-
);
|
72
|
-
root.equals(this.root);
|
73
|
-
return map;
|
74
|
-
}
|
75
|
-
|
76
|
-
/**
|
77
|
-
* The function fetches a whitelisted amount associated with a given address using a map and returns it
|
78
|
-
* as a UInt64Option.
|
79
|
-
* @param {PublicKey} address - The `address` parameter is of type `PublicKey`, which represents a
|
80
|
-
* public key used in cryptography for various purposes such as encryption, digital signatures, and
|
81
|
-
* authentication. In the context of the `fetchWhitelistedAmount` function, the `address` parameter is
|
82
|
-
* used to retrieve a whitelisted amount
|
83
|
-
* @returns The `fetchWhitelistedAmount` function returns a `Promise` that resolves to a `UInt64Option`
|
84
|
-
* object. This object contains a `value` property representing the amount retrieved from a map based
|
85
|
-
* on the provided address. The `isSome` property indicates whether the value is present or not.
|
86
|
-
* The value is not present if the whitelist is NOT empty and the address is NOT whitelisted.
|
87
|
-
* The value is present if the whitelist is NOT empty or the address IS whitelisted.
|
88
|
-
* The value is present and equals to UInt64.MAXINT() if the whitelist IS empty.
|
89
|
-
*/
|
90
|
-
async getWhitelistedAmount(address: PublicKey): Promise<UInt64Option> {
|
91
|
-
const map = await this.load();
|
92
|
-
const key = Poseidon.hashPacked(PublicKey, address);
|
93
|
-
const value = map.orElse(new WhitelistMap()).getOption(key);
|
94
|
-
const valueField = value.orElse(UInt64.MAXINT().value);
|
95
|
-
valueField.assertLessThanOrEqual(UInt64.MAXINT().value);
|
96
|
-
const amount = UInt64.Unsafe.fromField(valueField);
|
97
|
-
return new UInt64Option({
|
98
|
-
value: amount,
|
99
|
-
isSome: value.isSome.or(this.isNone()),
|
100
|
-
});
|
101
|
-
}
|
102
|
-
|
103
|
-
static empty(): Whitelist {
|
104
|
-
return new Whitelist({
|
105
|
-
root: Field(0),
|
106
|
-
storage: Storage.empty(),
|
107
|
-
});
|
108
|
-
}
|
109
|
-
|
110
|
-
/**
|
111
|
-
* Creates a new whitelist and pins it to IPFS.
|
112
|
-
* @param params - The parameters for creating the whitelist.
|
113
|
-
* @returns A new `Whitelist` instance.
|
114
|
-
*/
|
115
|
-
static async create(params: {
|
116
|
-
list: WhitelistedAddress[] | { address: string; amount?: number }[];
|
117
|
-
name?: string;
|
118
|
-
keyvalues?: object;
|
119
|
-
timeout?: number;
|
120
|
-
attempts?: number;
|
121
|
-
auth?: string;
|
122
|
-
}): Promise<Whitelist> {
|
123
|
-
const {
|
124
|
-
name = "whitelist.json",
|
125
|
-
keyvalues,
|
126
|
-
timeout = 60 * 1000,
|
127
|
-
attempts = 5,
|
128
|
-
auth,
|
129
|
-
} = params;
|
130
|
-
const list: WhitelistedAddress[] =
|
131
|
-
typeof params.list[0].address === "string"
|
132
|
-
? (params.list as { address: string; amount?: number }[]).map(
|
133
|
-
(item) =>
|
134
|
-
new WhitelistedAddress({
|
135
|
-
address: PublicKey.fromBase58(item.address),
|
136
|
-
amount: item.amount
|
137
|
-
? UInt64.from(item.amount)
|
138
|
-
: UInt64.MAXINT(),
|
139
|
-
})
|
140
|
-
)
|
141
|
-
: (params.list as WhitelistedAddress[]);
|
142
|
-
const map = new WhitelistMap();
|
143
|
-
for (const item of list) {
|
144
|
-
map.insert(
|
145
|
-
Poseidon.hashPacked(PublicKey, item.address),
|
146
|
-
item.amount.toBigInt()
|
147
|
-
);
|
148
|
-
}
|
149
|
-
const serializedMap = serializeIndexedMap(map);
|
150
|
-
const json = {
|
151
|
-
map: serializedMap,
|
152
|
-
whitelist: list.map((item) => ({
|
153
|
-
address: item.address.toBase58(),
|
154
|
-
amount: Number(item.amount.toBigInt()),
|
155
|
-
})),
|
156
|
-
};
|
157
|
-
let attempt = 0;
|
158
|
-
const start = Date.now();
|
159
|
-
if (process.env.DEBUG === "true")
|
160
|
-
console.log(
|
161
|
-
"Whitelist.create:",
|
162
|
-
{ json, name, keyvalues, auth },
|
163
|
-
json.whitelist
|
164
|
-
);
|
165
|
-
let hash = await pinJSON({
|
166
|
-
data: json,
|
167
|
-
name,
|
168
|
-
keyvalues,
|
169
|
-
auth,
|
170
|
-
});
|
171
|
-
while (!hash && attempt < attempts && Date.now() - start < timeout) {
|
172
|
-
attempt++;
|
173
|
-
await sleep(5000 * attempt); // handle rate-limits
|
174
|
-
hash = await pinJSON({
|
175
|
-
data: json,
|
176
|
-
name,
|
177
|
-
keyvalues,
|
178
|
-
auth,
|
179
|
-
});
|
180
|
-
}
|
181
|
-
if (!hash) throw new Error("Failed to pin whitelist");
|
182
|
-
|
183
|
-
return new Whitelist({
|
184
|
-
root: map.root,
|
185
|
-
storage: Storage.fromString(hash),
|
186
|
-
});
|
187
|
-
}
|
188
|
-
|
189
|
-
toString(): string {
|
190
|
-
return JSON.stringify(
|
191
|
-
{ root: this.root.toJSON(), storage: this.storage.toString() },
|
192
|
-
null,
|
193
|
-
2
|
194
|
-
);
|
195
|
-
}
|
196
|
-
|
197
|
-
static fromString(str: string): Whitelist {
|
198
|
-
const json = JSON.parse(str);
|
199
|
-
return new Whitelist({
|
200
|
-
root: Field.fromJSON(json.root),
|
201
|
-
storage: Storage.fromString(json.storage),
|
202
|
-
});
|
203
|
-
}
|
204
|
-
}
|