zkcloudworker 0.12.1 → 0.14.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 (62) hide show
  1. package/lib/ts/src/cloud/worker/cloud.d.ts +2 -2
  2. package/lib/ts/src/mina/index.d.ts +2 -0
  3. package/lib/ts/src/mina/index.js +2 -0
  4. package/lib/ts/src/mina/local/local.d.ts +2 -2
  5. package/lib/ts/src/mina/local/local.js +8 -4
  6. package/lib/ts/src/mina/token/FungibleToken.d.ts +338 -0
  7. package/lib/ts/src/mina/token/FungibleToken.js +290 -0
  8. package/lib/ts/src/mina/token/FungibleTokenAdmin.d.ts +31 -0
  9. package/lib/ts/src/mina/token/FungibleTokenAdmin.js +102 -0
  10. package/lib/ts/src/mina/token/index.d.ts +2 -0
  11. package/lib/ts/src/mina/token/index.js +18 -0
  12. package/lib/ts/src/mina/transactions/blockberry.d.ts +16 -0
  13. package/lib/ts/src/mina/transactions/blockberry.js +80 -0
  14. package/lib/ts/src/mina/transactions/chain.d.ts +1 -0
  15. package/lib/ts/src/mina/transactions/chain.js +2 -0
  16. package/lib/ts/src/mina/transactions/index.d.ts +5 -0
  17. package/lib/ts/src/mina/transactions/index.js +21 -0
  18. package/lib/ts/src/mina/transactions/nonce.d.ts +10 -0
  19. package/lib/ts/src/mina/transactions/nonce.js +60 -0
  20. package/lib/ts/src/mina/transactions/transaction.d.ts +9 -0
  21. package/lib/ts/src/mina/transactions/transaction.js +69 -0
  22. package/lib/ts/src/mina/transactions/txstatus.d.ts +8 -0
  23. package/lib/ts/src/mina/transactions/txstatus.js +18 -0
  24. package/lib/ts/src/mina/utils/fetch.js +1 -1
  25. package/lib/ts/tsconfig.tsbuildinfo +1 -1
  26. package/lib/web/src/cloud/worker/cloud.d.ts +2 -2
  27. package/lib/web/src/mina/index.d.ts +2 -0
  28. package/lib/web/src/mina/index.js +2 -0
  29. package/lib/web/src/mina/index.js.map +1 -1
  30. package/lib/web/src/mina/local/local.d.ts +2 -2
  31. package/lib/web/src/mina/local/local.js +8 -4
  32. package/lib/web/src/mina/local/local.js.map +1 -1
  33. package/lib/web/src/mina/token/FungibleToken.d.ts +338 -0
  34. package/lib/web/src/mina/token/FungibleToken.js +274 -0
  35. package/lib/web/src/mina/token/FungibleToken.js.map +1 -0
  36. package/lib/web/src/mina/token/FungibleTokenAdmin.d.ts +31 -0
  37. package/lib/web/src/mina/token/FungibleTokenAdmin.js +91 -0
  38. package/lib/web/src/mina/token/FungibleTokenAdmin.js.map +1 -0
  39. package/lib/web/src/mina/token/index.d.ts +2 -0
  40. package/lib/web/src/mina/token/index.js +3 -0
  41. package/lib/web/src/mina/token/index.js.map +1 -0
  42. package/lib/web/src/mina/transactions/blockberry.d.ts +16 -0
  43. package/lib/web/src/mina/transactions/blockberry.js +76 -0
  44. package/lib/web/src/mina/transactions/blockberry.js.map +1 -0
  45. package/lib/web/src/mina/transactions/chain.d.ts +1 -0
  46. package/lib/web/src/mina/transactions/chain.js +2 -0
  47. package/lib/web/src/mina/transactions/chain.js.map +1 -0
  48. package/lib/web/src/mina/transactions/index.d.ts +5 -0
  49. package/lib/web/src/mina/transactions/index.js +6 -0
  50. package/lib/web/src/mina/transactions/index.js.map +1 -0
  51. package/lib/web/src/mina/transactions/nonce.d.ts +10 -0
  52. package/lib/web/src/mina/transactions/nonce.js +58 -0
  53. package/lib/web/src/mina/transactions/nonce.js.map +1 -0
  54. package/lib/web/src/mina/transactions/transaction.d.ts +9 -0
  55. package/lib/web/src/mina/transactions/transaction.js +65 -0
  56. package/lib/web/src/mina/transactions/transaction.js.map +1 -0
  57. package/lib/web/src/mina/transactions/txstatus.d.ts +8 -0
  58. package/lib/web/src/mina/transactions/txstatus.js +16 -0
  59. package/lib/web/src/mina/transactions/txstatus.js.map +1 -0
  60. package/lib/web/src/mina/utils/fetch.js +1 -1
  61. package/lib/web/tsconfig.web.tsbuildinfo +1 -1
  62. package/package.json +5 -5
@@ -0,0 +1,290 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BalanceChangeEvent = exports.BurnEvent = exports.MintEvent = exports.PauseEvent = exports.SetAdminEvent = exports.FungibleToken = exports.FungibleTokenErrors = void 0;
13
+ const o1js_1 = require("o1js");
14
+ const FungibleTokenAdmin_js_1 = require("./FungibleTokenAdmin.js");
15
+ exports.FungibleTokenErrors = {
16
+ noAdminKey: "could not fetch admin contract key",
17
+ noPermissionToChangeAdmin: "Not allowed to change admin contract",
18
+ tokenPaused: "Token is currently paused",
19
+ noPermissionToMint: "Not allowed to mint tokens",
20
+ noPermissionToPause: "Not allowed to pause token",
21
+ noPermissionToResume: "Not allowed to resume token",
22
+ noTransferFromCirculation: "Can't transfer to/from the circulation account",
23
+ noPermissionChangeAllowed: "Can't change permissions for access or receive on token accounts",
24
+ flashMinting: "Flash-minting or unbalanced transaction detected. Please make sure that your transaction is balanced, and that your `AccountUpdate`s are ordered properly, so that tokens are not received before they are sent.",
25
+ unbalancedTransaction: "Transaction is unbalanced",
26
+ };
27
+ class FungibleToken extends o1js_1.TokenContractV2 {
28
+ constructor() {
29
+ super(...arguments);
30
+ this.decimals = (0, o1js_1.State)();
31
+ this.admin = (0, o1js_1.State)();
32
+ this.paused = (0, o1js_1.State)();
33
+ this.events = {
34
+ SetAdmin: SetAdminEvent,
35
+ Pause: PauseEvent,
36
+ Mint: MintEvent,
37
+ Burn: BurnEvent,
38
+ BalanceChange: BalanceChangeEvent,
39
+ };
40
+ }
41
+ async deploy(props) {
42
+ await super.deploy(props);
43
+ this.paused.set((0, o1js_1.Bool)(true));
44
+ this.account.zkappUri.set(props.src);
45
+ this.account.tokenSymbol.set(props.symbol);
46
+ this.account.permissions.set({
47
+ ...o1js_1.Permissions.default(),
48
+ setVerificationKey: o1js_1.Permissions.VerificationKey.impossibleDuringCurrentVersion(),
49
+ setPermissions: o1js_1.Permissions.impossible(),
50
+ access: o1js_1.Permissions.proof(),
51
+ });
52
+ }
53
+ /** Update the verification key.
54
+ * Note that because we have set the permissions for setting the verification key to `impossibleDuringCurrentVersion()`, this will only be possible in case of a protocol update that requires an update.
55
+ */
56
+ async updateVerificationKey(vk) {
57
+ this.account.verificationKey.set(vk);
58
+ }
59
+ /** Initializes the account for tracking total circulation.
60
+ * @argument {PublicKey} admin - public key where the admin contract is deployed
61
+ * @argument {UInt8} decimals - number of decimals for the token
62
+ * @argument {Bool} startPaused - if set to `Bool(true), the contract will start in a mode where token minting and transfers are paused. This should be used for non-atomic deployments
63
+ */
64
+ async initialize(admin, decimals, startPaused) {
65
+ this.account.provedState.requireEquals((0, o1js_1.Bool)(false));
66
+ this.admin.set(admin);
67
+ this.decimals.set(decimals);
68
+ this.paused.set((0, o1js_1.Bool)(false));
69
+ this.paused.set(startPaused);
70
+ const accountUpdate = o1js_1.AccountUpdate.createSigned(this.address, this.deriveTokenId());
71
+ let permissions = o1js_1.Permissions.default();
72
+ // This is necessary in order to allow token holders to burn.
73
+ permissions.send = o1js_1.Permissions.none();
74
+ permissions.setPermissions = o1js_1.Permissions.impossible();
75
+ accountUpdate.account.permissions.set(permissions);
76
+ }
77
+ async getAdminContract() {
78
+ const admin = await o1js_1.Provable.witnessAsync(o1js_1.PublicKey, async () => {
79
+ let pk = await this.admin.fetch();
80
+ (0, o1js_1.assert)(pk !== undefined, exports.FungibleTokenErrors.noAdminKey);
81
+ return pk;
82
+ });
83
+ this.admin.requireEquals(admin);
84
+ return (new FungibleToken.AdminContract(admin));
85
+ }
86
+ async setAdmin(admin) {
87
+ const adminContract = await this.getAdminContract();
88
+ const canChangeAdmin = await adminContract.canChangeAdmin(admin);
89
+ canChangeAdmin.assertTrue(exports.FungibleTokenErrors.noPermissionToChangeAdmin);
90
+ this.admin.set(admin);
91
+ this.emitEvent("SetAdmin", new SetAdminEvent({ adminKey: admin }));
92
+ }
93
+ async mint(recipient, amount) {
94
+ this.paused.getAndRequireEquals().assertFalse(exports.FungibleTokenErrors.tokenPaused);
95
+ const accountUpdate = this.internal.mint({ address: recipient, amount });
96
+ const adminContract = await this.getAdminContract();
97
+ const canMint = await adminContract.canMint(accountUpdate);
98
+ canMint.assertTrue(exports.FungibleTokenErrors.noPermissionToMint);
99
+ recipient.equals(this.address).assertFalse(exports.FungibleTokenErrors.noTransferFromCirculation);
100
+ this.approve(accountUpdate);
101
+ this.emitEvent("Mint", new MintEvent({ recipient, amount }));
102
+ const circulationUpdate = o1js_1.AccountUpdate.create(this.address, this.deriveTokenId());
103
+ circulationUpdate.balanceChange = o1js_1.Int64.fromUnsigned(amount);
104
+ return accountUpdate;
105
+ }
106
+ async burn(from, amount) {
107
+ this.paused.getAndRequireEquals().assertFalse(exports.FungibleTokenErrors.tokenPaused);
108
+ const accountUpdate = this.internal.burn({ address: from, amount });
109
+ const circulationUpdate = o1js_1.AccountUpdate.create(this.address, this.deriveTokenId());
110
+ from.equals(this.address).assertFalse(exports.FungibleTokenErrors.noTransferFromCirculation);
111
+ circulationUpdate.balanceChange = o1js_1.Int64.fromUnsigned(amount).negV2();
112
+ this.emitEvent("Burn", new BurnEvent({ from, amount }));
113
+ return accountUpdate;
114
+ }
115
+ async pause() {
116
+ const adminContract = await this.getAdminContract();
117
+ const canPause = await adminContract.canPause();
118
+ canPause.assertTrue(exports.FungibleTokenErrors.noPermissionToPause);
119
+ this.paused.set((0, o1js_1.Bool)(true));
120
+ this.emitEvent("Pause", new PauseEvent({ isPaused: (0, o1js_1.Bool)(true) }));
121
+ }
122
+ async resume() {
123
+ const adminContract = await this.getAdminContract();
124
+ const canResume = await adminContract.canResume();
125
+ canResume.assertTrue(exports.FungibleTokenErrors.noPermissionToResume);
126
+ this.paused.set((0, o1js_1.Bool)(false));
127
+ this.emitEvent("Pause", new PauseEvent({ isPaused: (0, o1js_1.Bool)(false) }));
128
+ }
129
+ async transfer(from, to, amount) {
130
+ this.paused.getAndRequireEquals().assertFalse(exports.FungibleTokenErrors.tokenPaused);
131
+ from.equals(this.address).assertFalse(exports.FungibleTokenErrors.noTransferFromCirculation);
132
+ to.equals(this.address).assertFalse(exports.FungibleTokenErrors.noTransferFromCirculation);
133
+ this.internal.send({ from, to, amount });
134
+ }
135
+ checkPermissionsUpdate(update) {
136
+ let permissions = update.update.permissions;
137
+ let { access, receive } = permissions.value;
138
+ let accessIsNone = o1js_1.Provable.equal(o1js_1.Types.AuthRequired, access, o1js_1.Permissions.none());
139
+ let receiveIsNone = o1js_1.Provable.equal(o1js_1.Types.AuthRequired, receive, o1js_1.Permissions.none());
140
+ let updateAllowed = accessIsNone.and(receiveIsNone);
141
+ (0, o1js_1.assert)(updateAllowed.or(permissions.isSome.not()), exports.FungibleTokenErrors.noPermissionChangeAllowed);
142
+ }
143
+ /** Approve `AccountUpdate`s that have been created outside of the token contract.
144
+ *
145
+ * @argument {AccountUpdateForest} updates - The `AccountUpdate`s to approve. Note that the forest size is limited by the base token contract, @see TokenContractV2.MAX_ACCOUNT_UPDATES The current limit is 9.
146
+ */
147
+ async approveBase(updates) {
148
+ this.paused.getAndRequireEquals().assertFalse(exports.FungibleTokenErrors.tokenPaused);
149
+ let totalBalance = o1js_1.Int64.from(0);
150
+ this.forEachUpdate(updates, (update, usesToken) => {
151
+ // Make sure that the account permissions are not changed
152
+ this.checkPermissionsUpdate(update);
153
+ this.emitEventIf(usesToken, "BalanceChange", new BalanceChangeEvent({ address: update.publicKey, amount: update.balanceChange }));
154
+ // Don't allow transfers to/from the account that's tracking circulation
155
+ update.publicKey.equals(this.address).and(usesToken).assertFalse(exports.FungibleTokenErrors.noTransferFromCirculation);
156
+ totalBalance = o1js_1.Provable.if(usesToken, totalBalance.add(update.balanceChange), totalBalance);
157
+ totalBalance.isPositiveV2().assertFalse(exports.FungibleTokenErrors.flashMinting);
158
+ });
159
+ totalBalance.assertEquals(o1js_1.Int64.zero, exports.FungibleTokenErrors.unbalancedTransaction);
160
+ }
161
+ async getBalanceOf(address) {
162
+ const account = o1js_1.AccountUpdate.create(address, this.deriveTokenId()).account;
163
+ const balance = account.balance.get();
164
+ account.balance.requireEquals(balance);
165
+ return balance;
166
+ }
167
+ /** Reports the current circulating supply
168
+ * This does take into account currently unreduced actions.
169
+ */
170
+ async getCirculating() {
171
+ let circulating = await this.getBalanceOf(this.address);
172
+ return circulating;
173
+ }
174
+ async getDecimals() {
175
+ return this.decimals.getAndRequireEquals();
176
+ }
177
+ }
178
+ exports.FungibleToken = FungibleToken;
179
+ // This defines the type of the contract that is used to control access to administrative actions.
180
+ // If you want to have a custom contract, overwrite this by setting FungibleToken.AdminContract to
181
+ // your own implementation of FungibleTokenAdminBase.
182
+ FungibleToken.AdminContract = FungibleTokenAdmin_js_1.FungibleTokenAdmin;
183
+ __decorate([
184
+ (0, o1js_1.state)(o1js_1.UInt8),
185
+ __metadata("design:type", Object)
186
+ ], FungibleToken.prototype, "decimals", void 0);
187
+ __decorate([
188
+ (0, o1js_1.state)(o1js_1.PublicKey),
189
+ __metadata("design:type", Object)
190
+ ], FungibleToken.prototype, "admin", void 0);
191
+ __decorate([
192
+ (0, o1js_1.state)(o1js_1.Bool),
193
+ __metadata("design:type", Object)
194
+ ], FungibleToken.prototype, "paused", void 0);
195
+ __decorate([
196
+ o1js_1.method,
197
+ __metadata("design:type", Function),
198
+ __metadata("design:paramtypes", [o1js_1.VerificationKey]),
199
+ __metadata("design:returntype", Promise)
200
+ ], FungibleToken.prototype, "updateVerificationKey", null);
201
+ __decorate([
202
+ o1js_1.method,
203
+ __metadata("design:type", Function),
204
+ __metadata("design:paramtypes", [o1js_1.PublicKey,
205
+ o1js_1.UInt8,
206
+ o1js_1.Bool]),
207
+ __metadata("design:returntype", Promise)
208
+ ], FungibleToken.prototype, "initialize", null);
209
+ __decorate([
210
+ o1js_1.method,
211
+ __metadata("design:type", Function),
212
+ __metadata("design:paramtypes", [o1js_1.PublicKey]),
213
+ __metadata("design:returntype", Promise)
214
+ ], FungibleToken.prototype, "setAdmin", null);
215
+ __decorate([
216
+ o1js_1.method.returns(o1js_1.AccountUpdate),
217
+ __metadata("design:type", Function),
218
+ __metadata("design:paramtypes", [o1js_1.PublicKey, o1js_1.UInt64]),
219
+ __metadata("design:returntype", Promise)
220
+ ], FungibleToken.prototype, "mint", null);
221
+ __decorate([
222
+ o1js_1.method.returns(o1js_1.AccountUpdate),
223
+ __metadata("design:type", Function),
224
+ __metadata("design:paramtypes", [o1js_1.PublicKey, o1js_1.UInt64]),
225
+ __metadata("design:returntype", Promise)
226
+ ], FungibleToken.prototype, "burn", null);
227
+ __decorate([
228
+ o1js_1.method,
229
+ __metadata("design:type", Function),
230
+ __metadata("design:paramtypes", []),
231
+ __metadata("design:returntype", Promise)
232
+ ], FungibleToken.prototype, "pause", null);
233
+ __decorate([
234
+ o1js_1.method,
235
+ __metadata("design:type", Function),
236
+ __metadata("design:paramtypes", []),
237
+ __metadata("design:returntype", Promise)
238
+ ], FungibleToken.prototype, "resume", null);
239
+ __decorate([
240
+ o1js_1.method,
241
+ __metadata("design:type", Function),
242
+ __metadata("design:paramtypes", [o1js_1.PublicKey, o1js_1.PublicKey, o1js_1.UInt64]),
243
+ __metadata("design:returntype", Promise)
244
+ ], FungibleToken.prototype, "transfer", null);
245
+ __decorate([
246
+ o1js_1.method,
247
+ __metadata("design:type", Function),
248
+ __metadata("design:paramtypes", [o1js_1.AccountUpdateForest]),
249
+ __metadata("design:returntype", Promise)
250
+ ], FungibleToken.prototype, "approveBase", null);
251
+ __decorate([
252
+ o1js_1.method.returns(o1js_1.UInt64),
253
+ __metadata("design:type", Function),
254
+ __metadata("design:paramtypes", [o1js_1.PublicKey]),
255
+ __metadata("design:returntype", Promise)
256
+ ], FungibleToken.prototype, "getBalanceOf", null);
257
+ __decorate([
258
+ o1js_1.method.returns(o1js_1.UInt8),
259
+ __metadata("design:type", Function),
260
+ __metadata("design:paramtypes", []),
261
+ __metadata("design:returntype", Promise)
262
+ ], FungibleToken.prototype, "getDecimals", null);
263
+ class SetAdminEvent extends (0, o1js_1.Struct)({
264
+ adminKey: o1js_1.PublicKey,
265
+ }) {
266
+ }
267
+ exports.SetAdminEvent = SetAdminEvent;
268
+ class PauseEvent extends (0, o1js_1.Struct)({
269
+ isPaused: o1js_1.Bool,
270
+ }) {
271
+ }
272
+ exports.PauseEvent = PauseEvent;
273
+ class MintEvent extends (0, o1js_1.Struct)({
274
+ recipient: o1js_1.PublicKey,
275
+ amount: o1js_1.UInt64,
276
+ }) {
277
+ }
278
+ exports.MintEvent = MintEvent;
279
+ class BurnEvent extends (0, o1js_1.Struct)({
280
+ from: o1js_1.PublicKey,
281
+ amount: o1js_1.UInt64,
282
+ }) {
283
+ }
284
+ exports.BurnEvent = BurnEvent;
285
+ class BalanceChangeEvent extends (0, o1js_1.Struct)({
286
+ address: o1js_1.PublicKey,
287
+ amount: o1js_1.Int64,
288
+ }) {
289
+ }
290
+ exports.BalanceChangeEvent = BalanceChangeEvent;
@@ -0,0 +1,31 @@
1
+ import { AccountUpdate, Bool, DeployArgs, PublicKey, SmartContract, VerificationKey } from "o1js";
2
+ export type FungibleTokenAdminBase = SmartContract & {
3
+ canMint(accountUpdate: AccountUpdate): Promise<Bool>;
4
+ canChangeAdmin(admin: PublicKey): Promise<Bool>;
5
+ canPause(): Promise<Bool>;
6
+ canResume(): Promise<Bool>;
7
+ };
8
+ export interface FungibleTokenAdminDeployProps extends Exclude<DeployArgs, undefined> {
9
+ adminPublicKey: PublicKey;
10
+ }
11
+ /** A contract that grants permissions for administrative actions on a token.
12
+ *
13
+ * We separate this out into a dedicated contract. That way, when issuing a token, a user can
14
+ * specify their own rules for administrative actions, without changing the token contract itself.
15
+ *
16
+ * The advantage is that third party applications that only use the token in a non-privileged way
17
+ * can integrate against the unchanged token contract.
18
+ */
19
+ export declare class FungibleTokenAdmin extends SmartContract implements FungibleTokenAdminBase {
20
+ private adminPublicKey;
21
+ deploy(props: FungibleTokenAdminDeployProps): Promise<void>;
22
+ /** Update the verification key.
23
+ * Note that because we have set the permissions for setting the verification key to `impossibleDuringCurrentVersion()`, this will only be possible in case of a protocol update that requires an update.
24
+ */
25
+ updateVerificationKey(vk: VerificationKey): Promise<void>;
26
+ private ensureAdminSignature;
27
+ canMint(_accountUpdate: AccountUpdate): Promise<import("o1js/dist/node/lib/provable/bool").Bool>;
28
+ canChangeAdmin(_admin: PublicKey): Promise<import("o1js/dist/node/lib/provable/bool").Bool>;
29
+ canPause(): Promise<Bool>;
30
+ canResume(): Promise<Bool>;
31
+ }
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.FungibleTokenAdmin = void 0;
13
+ const o1js_1 = require("o1js");
14
+ /** A contract that grants permissions for administrative actions on a token.
15
+ *
16
+ * We separate this out into a dedicated contract. That way, when issuing a token, a user can
17
+ * specify their own rules for administrative actions, without changing the token contract itself.
18
+ *
19
+ * The advantage is that third party applications that only use the token in a non-privileged way
20
+ * can integrate against the unchanged token contract.
21
+ */
22
+ class FungibleTokenAdmin extends o1js_1.SmartContract {
23
+ constructor() {
24
+ super(...arguments);
25
+ this.adminPublicKey = (0, o1js_1.State)();
26
+ }
27
+ async deploy(props) {
28
+ await super.deploy(props);
29
+ this.adminPublicKey.set(props.adminPublicKey);
30
+ this.account.permissions.set({
31
+ ...o1js_1.Permissions.default(),
32
+ setVerificationKey: o1js_1.Permissions.VerificationKey.impossibleDuringCurrentVersion(),
33
+ setPermissions: o1js_1.Permissions.impossible(),
34
+ });
35
+ }
36
+ /** Update the verification key.
37
+ * Note that because we have set the permissions for setting the verification key to `impossibleDuringCurrentVersion()`, this will only be possible in case of a protocol update that requires an update.
38
+ */
39
+ async updateVerificationKey(vk) {
40
+ this.account.verificationKey.set(vk);
41
+ }
42
+ async ensureAdminSignature() {
43
+ const admin = await o1js_1.Provable.witnessAsync(o1js_1.PublicKey, async () => {
44
+ let pk = await this.adminPublicKey.fetch();
45
+ (0, o1js_1.assert)(pk !== undefined, "could not fetch admin public key");
46
+ return pk;
47
+ });
48
+ this.adminPublicKey.requireEquals(admin);
49
+ return o1js_1.AccountUpdate.createSigned(admin);
50
+ }
51
+ async canMint(_accountUpdate) {
52
+ await this.ensureAdminSignature();
53
+ return (0, o1js_1.Bool)(true);
54
+ }
55
+ async canChangeAdmin(_admin) {
56
+ await this.ensureAdminSignature();
57
+ return (0, o1js_1.Bool)(true);
58
+ }
59
+ async canPause() {
60
+ await this.ensureAdminSignature();
61
+ return (0, o1js_1.Bool)(true);
62
+ }
63
+ async canResume() {
64
+ await this.ensureAdminSignature();
65
+ return (0, o1js_1.Bool)(true);
66
+ }
67
+ }
68
+ exports.FungibleTokenAdmin = FungibleTokenAdmin;
69
+ __decorate([
70
+ (0, o1js_1.state)(o1js_1.PublicKey),
71
+ __metadata("design:type", Object)
72
+ ], FungibleTokenAdmin.prototype, "adminPublicKey", void 0);
73
+ __decorate([
74
+ o1js_1.method,
75
+ __metadata("design:type", Function),
76
+ __metadata("design:paramtypes", [o1js_1.VerificationKey]),
77
+ __metadata("design:returntype", Promise)
78
+ ], FungibleTokenAdmin.prototype, "updateVerificationKey", null);
79
+ __decorate([
80
+ o1js_1.method.returns(o1js_1.Bool),
81
+ __metadata("design:type", Function),
82
+ __metadata("design:paramtypes", [o1js_1.AccountUpdate]),
83
+ __metadata("design:returntype", Promise)
84
+ ], FungibleTokenAdmin.prototype, "canMint", null);
85
+ __decorate([
86
+ o1js_1.method.returns(o1js_1.Bool),
87
+ __metadata("design:type", Function),
88
+ __metadata("design:paramtypes", [o1js_1.PublicKey]),
89
+ __metadata("design:returntype", Promise)
90
+ ], FungibleTokenAdmin.prototype, "canChangeAdmin", null);
91
+ __decorate([
92
+ o1js_1.method.returns(o1js_1.Bool),
93
+ __metadata("design:type", Function),
94
+ __metadata("design:paramtypes", []),
95
+ __metadata("design:returntype", Promise)
96
+ ], FungibleTokenAdmin.prototype, "canPause", null);
97
+ __decorate([
98
+ o1js_1.method.returns(o1js_1.Bool),
99
+ __metadata("design:type", Function),
100
+ __metadata("design:paramtypes", []),
101
+ __metadata("design:returntype", Promise)
102
+ ], FungibleTokenAdmin.prototype, "canResume", null);
@@ -0,0 +1,2 @@
1
+ export * from "./FungibleToken";
2
+ export * from "./FungibleTokenAdmin";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./FungibleToken"), exports);
18
+ __exportStar(require("./FungibleTokenAdmin"), exports);
@@ -0,0 +1,16 @@
1
+ import { BlockBerryChain } from "./chain";
2
+ export declare function getZkAppTxsFromBlockBerry(params: {
3
+ account: string;
4
+ chain: BlockBerryChain;
5
+ blockBerryApiKey: string;
6
+ }): Promise<any>;
7
+ export declare function getPaymentTxsFromBlockBerry(params: {
8
+ account: string;
9
+ chain: BlockBerryChain;
10
+ blockBerryApiKey: string;
11
+ }): Promise<any>;
12
+ export declare function getZkAppTxFromBlockBerry(params: {
13
+ hash: string;
14
+ chain: BlockBerryChain;
15
+ blockBerryApiKey: string;
16
+ }): Promise<any>;
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getZkAppTxsFromBlockBerry = getZkAppTxsFromBlockBerry;
4
+ exports.getPaymentTxsFromBlockBerry = getPaymentTxsFromBlockBerry;
5
+ exports.getZkAppTxFromBlockBerry = getZkAppTxFromBlockBerry;
6
+ async function getZkAppTxsFromBlockBerry(params) {
7
+ const { account, chain, blockBerryApiKey } = params;
8
+ const options = {
9
+ method: "GET",
10
+ headers: {
11
+ accept: "application/json",
12
+ "x-api-key": blockBerryApiKey,
13
+ },
14
+ };
15
+ try {
16
+ const response = await fetch(`https://api.blockberry.one/mina-${chain}/v1/zkapps/accounts/${account}/txs?size=10&orderBy=DESC&sortBy=AGE`, options);
17
+ if (!response.ok) {
18
+ console.error("Cannot fetch zkApp txs for account:", account, chain, response.statusText);
19
+ return undefined;
20
+ }
21
+ const result = await response.json();
22
+ //console.log("zkAppTxs", result);
23
+ return result;
24
+ }
25
+ catch (err) {
26
+ console.error("Cannot fetch zkApp txs for account - catch:", account, chain, err);
27
+ return undefined;
28
+ }
29
+ }
30
+ async function getPaymentTxsFromBlockBerry(params) {
31
+ const { account, chain, blockBerryApiKey } = params;
32
+ const options = {
33
+ method: "GET",
34
+ headers: {
35
+ accept: "application/json",
36
+ "x-api-key": blockBerryApiKey,
37
+ },
38
+ };
39
+ try {
40
+ const response = await fetch(`https://api.blockberry.one/mina-${chain}/v1/accounts/` +
41
+ account +
42
+ "/txs?page=0&size=1&orderBy=DESC&sortBy=AGE&direction=OUT", options);
43
+ if (!response.ok) {
44
+ console.error("Cannot fetch payment txs for account:", account, chain, response.statusText);
45
+ return undefined;
46
+ }
47
+ const result = await response.json();
48
+ //console.log("paymentTxs", result);
49
+ return result;
50
+ }
51
+ catch (err) {
52
+ console.error("Cannot fetch payment txs for account - catch:", account, chain, err);
53
+ return undefined;
54
+ }
55
+ }
56
+ async function getZkAppTxFromBlockBerry(params) {
57
+ const { hash, chain, blockBerryApiKey } = params;
58
+ const options = {
59
+ method: "GET",
60
+ headers: {
61
+ accept: "application/json",
62
+ "x-api-key": blockBerryApiKey,
63
+ },
64
+ };
65
+ try {
66
+ const response = await fetch(`https://api.blockberry.one/mina-${chain}/v1/zkapps/txs/${hash}`, options);
67
+ if (response.ok) {
68
+ const result = await response.json();
69
+ return result;
70
+ }
71
+ else {
72
+ console.error("getZkAppTxFromBlockBerry error while getting hash - not ok", { hash, chain, text: response.statusText, status: response.status });
73
+ return undefined;
74
+ }
75
+ }
76
+ catch (err) {
77
+ console.error("getZkAppTxFromBlockBerry error while getting mainnet hash - catch", hash, chain, err);
78
+ return undefined;
79
+ }
80
+ }
@@ -0,0 +1 @@
1
+ export type BlockBerryChain = "mainnet" | "devnet";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export * from "./nonce";
2
+ export * from "./transaction";
3
+ export * from "./txstatus";
4
+ export * from "./chain";
5
+ export * from "./blockberry";
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./nonce"), exports);
18
+ __exportStar(require("./transaction"), exports);
19
+ __exportStar(require("./txstatus"), exports);
20
+ __exportStar(require("./chain"), exports);
21
+ __exportStar(require("./blockberry"), exports);
@@ -0,0 +1,10 @@
1
+ import { BlockBerryChain } from "./chain";
2
+ export declare function getNonce(params: {
3
+ account: string;
4
+ chain: BlockBerryChain;
5
+ blockBerryApiKey: string;
6
+ }): Promise<{
7
+ success: boolean;
8
+ nonce: number;
9
+ message?: string;
10
+ }>;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNonce = getNonce;
4
+ const blockberry_1 = require("./blockberry");
5
+ async function getNonce(params) {
6
+ const { account, chain, blockBerryApiKey } = params;
7
+ try {
8
+ if (account === undefined || account === null || account === "") {
9
+ return {
10
+ success: false,
11
+ nonce: -1,
12
+ message: "Account is required",
13
+ };
14
+ }
15
+ if (blockBerryApiKey === undefined ||
16
+ blockBerryApiKey === null ||
17
+ blockBerryApiKey === "") {
18
+ return {
19
+ success: false,
20
+ nonce: -1,
21
+ message: "blockBerryApiKey is required",
22
+ };
23
+ }
24
+ const zkAppTxsPromise = (0, blockberry_1.getZkAppTxsFromBlockBerry)({
25
+ account,
26
+ chain,
27
+ blockBerryApiKey,
28
+ });
29
+ const paymentTxs = (0, blockberry_1.getPaymentTxsFromBlockBerry)({
30
+ account,
31
+ chain,
32
+ blockBerryApiKey,
33
+ });
34
+ const paymentNonce = (await paymentTxs)?.data[0]?.nonce ?? -1;
35
+ let zkNonce = -1;
36
+ let found = false;
37
+ const zkAppTxs = await zkAppTxsPromise;
38
+ const size = zkAppTxs?.data?.length ?? 0;
39
+ let i = 0;
40
+ while (!found && i < size) {
41
+ if (zkAppTxs?.data[i]?.proverAddress === account) {
42
+ zkNonce = zkAppTxs?.data[i]?.nonce;
43
+ found = true;
44
+ }
45
+ i++;
46
+ }
47
+ const nonce = Math.max(zkNonce, paymentNonce);
48
+ return {
49
+ success: true,
50
+ nonce,
51
+ };
52
+ }
53
+ catch (error) {
54
+ return {
55
+ success: false,
56
+ nonce: -1,
57
+ message: String(error),
58
+ };
59
+ }
60
+ }
@@ -0,0 +1,9 @@
1
+ import { PublicKey, Transaction, Mina, UInt64 } from "o1js";
2
+ export declare function transactionParams(serializedTransaction: string, signedJson: any): {
3
+ fee: UInt64;
4
+ sender: PublicKey;
5
+ nonce: number;
6
+ memo: string;
7
+ };
8
+ export declare function deserializeTransaction(serializedTransaction: string, txNew: Mina.Transaction<false, false>, signedJson: any): Transaction<false, true>;
9
+ export declare function serializeTransaction(tx: Mina.Transaction<false, false>): string;