@dorafactory/maci-sdk 0.0.6 → 0.0.7

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.
@@ -0,0 +1,284 @@
1
+ import { OfflineSigner } from '@cosmjs/proto-signing';
2
+ import { batchGenMessage, Circom, PublicKey, stringizing } from '../circom';
3
+ import { Contract } from '../contract';
4
+ import {
5
+ MsgExecuteContractEncodeObject,
6
+ SigningCosmWasmClient,
7
+ } from '@cosmjs/cosmwasm-stargate';
8
+ import { GasPrice, calculateFee, StdFee } from '@cosmjs/stargate';
9
+ import { MsgExecuteContract } from 'cosmjs-types/cosmwasm/wasm/v1/tx';
10
+
11
+ export class MACI {
12
+ public circom: Circom;
13
+ public contract: Contract;
14
+ constructor({ circom, contract }: { circom: Circom; contract: Contract }) {
15
+ this.circom = circom;
16
+ this.contract = contract;
17
+ }
18
+
19
+ async signup({
20
+ signer,
21
+ address,
22
+ contractAddress,
23
+ oracleCertificate,
24
+ gasStation = false,
25
+ }: {
26
+ signer: OfflineSigner;
27
+ address: string;
28
+ contractAddress: string;
29
+ oracleCertificate?: {
30
+ amount: string;
31
+ signature: string;
32
+ };
33
+ gasStation?: boolean;
34
+ }) {
35
+ try {
36
+ const maciAccount = await this.circom.genKeypairFromSign(signer, address);
37
+
38
+ const client = await this.contract.contractClient({
39
+ signer,
40
+ });
41
+
42
+ if (oracleCertificate) {
43
+ await this.signupOracle({
44
+ client,
45
+ address,
46
+ pubKey: maciAccount.pubKey,
47
+ contractAddress,
48
+ oracleCertificate,
49
+ gasStation,
50
+ });
51
+ } else {
52
+ await this.signupSimple({
53
+ client,
54
+ address,
55
+ pubKey: maciAccount.pubKey,
56
+ contractAddress,
57
+ gasStation,
58
+ });
59
+ }
60
+ } catch (error) {
61
+ throw Error(`Signup failed! ${error}`);
62
+ }
63
+ }
64
+
65
+ submit = async ({
66
+ signer,
67
+ address,
68
+ stateIdx,
69
+ contractAddress,
70
+ selectedOptions,
71
+ operatorCoordPubKey,
72
+ gasStation = false,
73
+ }: {
74
+ signer: OfflineSigner;
75
+ address: string;
76
+ stateIdx: number;
77
+ contractAddress: string;
78
+ selectedOptions: {
79
+ idx: number;
80
+ vc: number;
81
+ }[];
82
+ operatorCoordPubKey: PublicKey;
83
+ gasStation?: boolean;
84
+ }) => {
85
+ const options = selectedOptions.filter((o) => !!o.vc);
86
+
87
+ try {
88
+ const maciAccount = await this.circom.genKeypairFromSign(signer, address);
89
+
90
+ const plan = options.map((o) => {
91
+ return [o.idx, o.vc] as [number, number];
92
+ });
93
+
94
+ const payload = batchGenMessage(
95
+ stateIdx,
96
+ maciAccount,
97
+ operatorCoordPubKey,
98
+ plan
99
+ );
100
+
101
+ const client = await this.contract.contractClient({
102
+ signer,
103
+ });
104
+
105
+ await this.submitPlan({
106
+ client,
107
+ address,
108
+ payload,
109
+ contractAddress,
110
+ gasStation,
111
+ });
112
+ } catch (error) {
113
+ throw Error(`Submit failed! ${error}`);
114
+ }
115
+ };
116
+
117
+ async submitPlan({
118
+ client,
119
+ address,
120
+ payload,
121
+ contractAddress,
122
+ gasStation,
123
+ }: {
124
+ client: SigningCosmWasmClient;
125
+ address: string;
126
+ payload: {
127
+ msg: bigint[];
128
+ encPubkeys: PublicKey;
129
+ }[];
130
+ contractAddress: string;
131
+ gasStation: boolean;
132
+ }) {
133
+ const msgs: MsgExecuteContractEncodeObject[] = payload.map(
134
+ ({ msg, encPubkeys }) => ({
135
+ typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
136
+ value: MsgExecuteContract.fromPartial({
137
+ sender: address,
138
+ contract: contractAddress,
139
+ msg: new TextEncoder().encode(
140
+ JSON.stringify(
141
+ stringizing({
142
+ publish_message: {
143
+ enc_pub_key: {
144
+ x: encPubkeys[0],
145
+ y: encPubkeys[1],
146
+ },
147
+ message: {
148
+ data: msg,
149
+ },
150
+ },
151
+ })
152
+ )
153
+ ),
154
+ }),
155
+ })
156
+ );
157
+
158
+ const gasPrice = GasPrice.fromString('100000000000peaka');
159
+ const fee = calculateFee(20000000 * msgs.length, gasPrice);
160
+
161
+ if (gasStation) {
162
+ const grantFee: StdFee = {
163
+ amount: fee.amount,
164
+ gas: fee.gas,
165
+ granter: contractAddress,
166
+ };
167
+ return client.signAndBroadcast(address, msgs, grantFee);
168
+ }
169
+ return client.signAndBroadcast(address, msgs, fee);
170
+ }
171
+
172
+ async signupSimple({
173
+ client,
174
+ address,
175
+ pubKey,
176
+ contractAddress,
177
+ gasStation,
178
+ }: {
179
+ client: SigningCosmWasmClient;
180
+ address: string;
181
+ pubKey: PublicKey;
182
+ contractAddress: string;
183
+ gasStation?: boolean;
184
+ }) {
185
+ const gasPrice = GasPrice.fromString('100000000000peaka');
186
+ const fee = calculateFee(60000000, gasPrice);
187
+
188
+ if (gasStation === true) {
189
+ const grantFee: StdFee = {
190
+ amount: fee.amount,
191
+ gas: fee.gas,
192
+ granter: contractAddress,
193
+ };
194
+ return client.execute(
195
+ address,
196
+ contractAddress,
197
+ {
198
+ sign_up: {
199
+ pubkey: {
200
+ x: pubKey[0].toString(),
201
+ y: pubKey[1].toString(),
202
+ },
203
+ },
204
+ },
205
+ grantFee
206
+ );
207
+ }
208
+
209
+ return client.execute(
210
+ address,
211
+ contractAddress,
212
+ {
213
+ sign_up: {
214
+ pubkey: {
215
+ x: pubKey[0].toString(),
216
+ y: pubKey[1].toString(),
217
+ },
218
+ },
219
+ },
220
+ fee
221
+ );
222
+ }
223
+
224
+ async signupOracle({
225
+ client,
226
+ address,
227
+ pubKey,
228
+ contractAddress,
229
+ oracleCertificate,
230
+ gasStation,
231
+ }: {
232
+ client: SigningCosmWasmClient;
233
+ address: string;
234
+ pubKey: PublicKey;
235
+ contractAddress: string;
236
+ oracleCertificate: {
237
+ amount: string;
238
+ signature: string;
239
+ };
240
+ gasStation?: boolean;
241
+ }) {
242
+ const gasPrice = GasPrice.fromString('100000000000peaka');
243
+ const fee = calculateFee(60000000, gasPrice);
244
+
245
+ if (gasStation === true) {
246
+ const grantFee: StdFee = {
247
+ amount: fee.amount,
248
+ gas: fee.gas,
249
+ granter: contractAddress,
250
+ };
251
+ return client.execute(
252
+ address,
253
+ contractAddress,
254
+ {
255
+ sign_up: {
256
+ pubkey: {
257
+ x: pubKey[0].toString(),
258
+ y: pubKey[1].toString(),
259
+ },
260
+ amount: oracleCertificate.amount,
261
+ certificate: oracleCertificate.signature,
262
+ },
263
+ },
264
+ grantFee
265
+ );
266
+ }
267
+
268
+ return client.execute(
269
+ address,
270
+ contractAddress,
271
+ {
272
+ sign_up: {
273
+ pubkey: {
274
+ x: pubKey[0].toString(),
275
+ y: pubKey[1].toString(),
276
+ },
277
+ amount: oracleCertificate.amount,
278
+ certificate: oracleCertificate.signature,
279
+ },
280
+ },
281
+ fee
282
+ );
283
+ }
284
+ }
@@ -0,0 +1,6 @@
1
+ export type IAccountStatus = {
2
+ stateIdx: number;
3
+ vcbTotal: number;
4
+ whitelistCommitment: number;
5
+ feegrantStatus?: string;
6
+ };
@@ -13,7 +13,7 @@ import {
13
13
  import { Http } from '../http';
14
14
  import {
15
15
  Round,
16
- Account,
16
+ UserAccount,
17
17
  Circuit,
18
18
  Operator,
19
19
  Proof,
@@ -3,7 +3,7 @@ import { BalanceResponse } from '../../types';
3
3
  import { handleError, ErrorType } from '../errors';
4
4
  import { ERROR } from '../errors/types';
5
5
 
6
- export class Account {
6
+ export class UserAccount {
7
7
  public http: Http;
8
8
 
9
9
  constructor(http: Http) {
@@ -1,4 +1,4 @@
1
- export { Account } from './account';
1
+ export { UserAccount } from './account';
2
2
  export { Circuit } from './circuit';
3
3
  export { Operator } from './operator';
4
4
  export { Round } from './round';
package/src/maci.ts CHANGED
@@ -19,6 +19,7 @@ import {
19
19
  CreateOracleMaciRoundParams,
20
20
  } from './libs/contract/types';
21
21
  import { OfflineSigner } from '@cosmjs/proto-signing';
22
+ import { Circom } from './libs/circom';
22
23
 
23
24
  /**
24
25
  * @class MaciClient
@@ -35,6 +36,7 @@ export class MaciClient {
35
36
  public http: Http;
36
37
  public indexer: Indexer;
37
38
  public contract: Contract;
39
+ public circom: Circom;
38
40
 
39
41
  public feegrantOperator: string;
40
42
  public whitelistBackendPubkey: string;
@@ -89,6 +91,7 @@ export class MaciClient {
89
91
  feegrantOperator: this.feegrantOperator,
90
92
  whitelistBackendPubkey: this.whitelistBackendPubkey,
91
93
  });
94
+ this.circom = new Circom({ network });
92
95
  }
93
96
 
94
97
  async oracleMaciClient({