@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.
- package/dist/index.d.ts +4 -1
- package/dist/index.js +301 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +287 -5
- package/dist/index.mjs.map +1 -1
- package/dist/libs/circom/index.d.ts +8 -1
- package/dist/libs/circom/types.d.ts +7 -0
- package/dist/libs/contract/contract.d.ts +4 -0
- package/dist/libs/indexer/indexer.d.ts +2 -2
- package/dist/libs/maci/index.d.ts +0 -0
- package/dist/libs/maci/maci.d.ts +62 -0
- package/dist/libs/maci/types.d.ts +6 -0
- package/dist/libs/query/account.d.ts +1 -1
- package/dist/libs/query/index.d.ts +1 -1
- package/dist/maci.d.ts +2 -0
- package/package.json +11 -7
- package/src/index.ts +4 -1
- package/src/libs/circom/circomlib.ts +308 -0
- package/src/libs/circom/index.ts +85 -0
- package/src/libs/circom/types.ts +8 -0
- package/src/libs/contract/contract.ts +4 -0
- package/src/libs/contract/utils.ts +2 -2
- package/src/libs/indexer/indexer.ts +3 -3
- package/src/libs/maci/index.ts +0 -0
- package/src/libs/maci/maci.ts +284 -0
- package/src/libs/maci/types.ts +6 -0
- package/src/libs/oracle-certificate/oracle-certificate.ts +1 -1
- package/src/libs/query/account.ts +1 -1
- package/src/libs/query/index.ts +1 -1
- package/src/maci.ts +3 -0
package/dist/index.mjs
CHANGED
|
@@ -232,7 +232,7 @@ var ERROR = {
|
|
|
232
232
|
};
|
|
233
233
|
|
|
234
234
|
// src/libs/query/account.ts
|
|
235
|
-
var
|
|
235
|
+
var UserAccount = class {
|
|
236
236
|
constructor(http) {
|
|
237
237
|
this.http = http;
|
|
238
238
|
}
|
|
@@ -1438,7 +1438,7 @@ var Indexer = class {
|
|
|
1438
1438
|
this.apiEndpoint = apiEndpoint;
|
|
1439
1439
|
this.registryAddress = registryAddress;
|
|
1440
1440
|
this.round = new Round(this.http);
|
|
1441
|
-
this.account = new
|
|
1441
|
+
this.account = new UserAccount(this.http);
|
|
1442
1442
|
this.circuit = new Circuit(this.http);
|
|
1443
1443
|
this.operator = new Operator(this.http, this.registryAddress);
|
|
1444
1444
|
this.proof = new Proof(this.http);
|
|
@@ -3673,8 +3673,8 @@ function getContractParams(type, circuitType, proofSystem, maxVoter, maxOption)
|
|
|
3673
3673
|
case "2" /* ORACLE_MACI */:
|
|
3674
3674
|
return {
|
|
3675
3675
|
parameters: CIRCUIT_INFO["9-4-3-625"].parameter,
|
|
3676
|
-
groth16ProcessVkey
|
|
3677
|
-
groth16TallyVkey
|
|
3676
|
+
groth16ProcessVkey,
|
|
3677
|
+
groth16TallyVkey,
|
|
3678
3678
|
plonkProcessVkey: null,
|
|
3679
3679
|
plonkTallyVkey: null,
|
|
3680
3680
|
maciVoteType,
|
|
@@ -3940,6 +3940,272 @@ var Contract = class {
|
|
|
3940
3940
|
contractAddress
|
|
3941
3941
|
});
|
|
3942
3942
|
}
|
|
3943
|
+
async contractClient({ signer }) {
|
|
3944
|
+
return createContractClientByWallet(this.rpcEndpoint, signer);
|
|
3945
|
+
}
|
|
3946
|
+
};
|
|
3947
|
+
|
|
3948
|
+
// src/libs/circom/index.ts
|
|
3949
|
+
import {
|
|
3950
|
+
isOfflineDirectSigner
|
|
3951
|
+
} from "@cosmjs/proto-signing";
|
|
3952
|
+
|
|
3953
|
+
// src/libs/circom/circomlib.ts
|
|
3954
|
+
import { randomBytes } from "crypto";
|
|
3955
|
+
import {
|
|
3956
|
+
babyJub,
|
|
3957
|
+
eddsa,
|
|
3958
|
+
poseidon,
|
|
3959
|
+
poseidonEncrypt,
|
|
3960
|
+
Tree
|
|
3961
|
+
} from "@dorafactory/circomlib";
|
|
3962
|
+
import { Scalar, utils } from "ffjavascript";
|
|
3963
|
+
import createBlakeHash from "blake-hash";
|
|
3964
|
+
import { solidityPackedSha256 } from "ethers";
|
|
3965
|
+
var SNARK_FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
3966
|
+
var bigInt2Buffer = (i) => {
|
|
3967
|
+
let hex = i.toString(16);
|
|
3968
|
+
if (hex.length % 2 === 1) {
|
|
3969
|
+
hex = "0" + hex;
|
|
3970
|
+
}
|
|
3971
|
+
return Buffer.from(hex, "hex");
|
|
3972
|
+
};
|
|
3973
|
+
var genRandomKey = () => {
|
|
3974
|
+
const min = 6350874878119819312338956282401532410528162663560392320966563075034087161851n;
|
|
3975
|
+
let rand;
|
|
3976
|
+
while (true) {
|
|
3977
|
+
rand = BigInt("0x" + randomBytes(32).toString("hex"));
|
|
3978
|
+
if (rand >= min) {
|
|
3979
|
+
break;
|
|
3980
|
+
}
|
|
3981
|
+
}
|
|
3982
|
+
const privKey = rand % SNARK_FIELD_SIZE;
|
|
3983
|
+
return privKey;
|
|
3984
|
+
};
|
|
3985
|
+
var genPubKey = (privKey) => {
|
|
3986
|
+
return eddsa.prv2pub(bigInt2Buffer(privKey));
|
|
3987
|
+
};
|
|
3988
|
+
var stringizing = (o, path = []) => {
|
|
3989
|
+
if (path.includes(o)) {
|
|
3990
|
+
throw new Error("loop nesting!");
|
|
3991
|
+
}
|
|
3992
|
+
const newPath = [...path, o];
|
|
3993
|
+
if (Array.isArray(o)) {
|
|
3994
|
+
return o.map((item) => stringizing(item, newPath));
|
|
3995
|
+
} else if (typeof o === "object") {
|
|
3996
|
+
const output = {};
|
|
3997
|
+
for (const key in o) {
|
|
3998
|
+
output[key] = stringizing(o[key], newPath);
|
|
3999
|
+
}
|
|
4000
|
+
return output;
|
|
4001
|
+
} else {
|
|
4002
|
+
return o.toString();
|
|
4003
|
+
}
|
|
4004
|
+
};
|
|
4005
|
+
var genKeypair = (pkey) => {
|
|
4006
|
+
const privKey = pkey ? pkey % SNARK_FIELD_SIZE : genRandomKey();
|
|
4007
|
+
const pubKey = genPubKey(privKey);
|
|
4008
|
+
const formatedPrivKey = formatPrivKeyForBabyJub(privKey);
|
|
4009
|
+
return { privKey, pubKey, formatedPrivKey };
|
|
4010
|
+
};
|
|
4011
|
+
var formatPrivKeyForBabyJub = (privKey) => {
|
|
4012
|
+
const sBuff = eddsa.pruneBuffer(
|
|
4013
|
+
createBlakeHash("blake512").update(bigInt2Buffer(privKey)).digest().slice(0, 32)
|
|
4014
|
+
);
|
|
4015
|
+
const s = utils.leBuff2int(sBuff);
|
|
4016
|
+
return Scalar.shr(s, 3);
|
|
4017
|
+
};
|
|
4018
|
+
var genEcdhSharedKey = (privKey, pubKey) => {
|
|
4019
|
+
const sharedKey = babyJub.mulPointEscalar(
|
|
4020
|
+
pubKey,
|
|
4021
|
+
formatPrivKeyForBabyJub(privKey)
|
|
4022
|
+
);
|
|
4023
|
+
if (sharedKey[0] === 0n) {
|
|
4024
|
+
return [0n, 1n];
|
|
4025
|
+
} else {
|
|
4026
|
+
return sharedKey;
|
|
4027
|
+
}
|
|
4028
|
+
};
|
|
4029
|
+
var genMessageFactory = (stateIdx, signPriKey, signPubKey, coordPubKey) => (encPriKey, nonce, voIdx, newVotes, isLastCmd, salt) => {
|
|
4030
|
+
if (!salt) {
|
|
4031
|
+
salt = BigInt("0x" + randomBytes(7).toString("hex"));
|
|
4032
|
+
}
|
|
4033
|
+
const packaged = BigInt(nonce) + (BigInt(stateIdx) << 32n) + (BigInt(voIdx) << 64n) + (BigInt(newVotes) << 96n) + (BigInt(salt) << 192n);
|
|
4034
|
+
let newPubKey = [...signPubKey];
|
|
4035
|
+
if (isLastCmd) {
|
|
4036
|
+
newPubKey = [0n, 0n];
|
|
4037
|
+
}
|
|
4038
|
+
const hash = poseidon([packaged, ...newPubKey]);
|
|
4039
|
+
const signature = eddsa.signPoseidon(bigInt2Buffer(signPriKey), hash);
|
|
4040
|
+
const command = [packaged, ...newPubKey, ...signature.R8, signature.S];
|
|
4041
|
+
const message = poseidonEncrypt(
|
|
4042
|
+
command,
|
|
4043
|
+
genEcdhSharedKey(encPriKey, coordPubKey),
|
|
4044
|
+
0n
|
|
4045
|
+
);
|
|
4046
|
+
return message;
|
|
4047
|
+
};
|
|
4048
|
+
var batchGenMessage = (stateIdx, account, coordPubKey, plan) => {
|
|
4049
|
+
const genMessage = genMessageFactory(
|
|
4050
|
+
stateIdx,
|
|
4051
|
+
account.privKey,
|
|
4052
|
+
account.pubKey,
|
|
4053
|
+
coordPubKey
|
|
4054
|
+
);
|
|
4055
|
+
const payload = [];
|
|
4056
|
+
for (let i = plan.length - 1; i >= 0; i--) {
|
|
4057
|
+
const p = plan[i];
|
|
4058
|
+
const encAccount = genKeypair();
|
|
4059
|
+
const msg = genMessage(
|
|
4060
|
+
encAccount.privKey,
|
|
4061
|
+
i + 1,
|
|
4062
|
+
p[0],
|
|
4063
|
+
p[1],
|
|
4064
|
+
i === plan.length - 1
|
|
4065
|
+
);
|
|
4066
|
+
payload.push({
|
|
4067
|
+
msg,
|
|
4068
|
+
encPubkeys: encAccount.pubKey
|
|
4069
|
+
});
|
|
4070
|
+
}
|
|
4071
|
+
return payload;
|
|
4072
|
+
};
|
|
4073
|
+
var privateKeyFromTxt = (txt) => {
|
|
4074
|
+
if (typeof txt !== "string") {
|
|
4075
|
+
return;
|
|
4076
|
+
}
|
|
4077
|
+
const key = txt.split("\n")[1] || "";
|
|
4078
|
+
if (key.length !== 512) {
|
|
4079
|
+
return;
|
|
4080
|
+
}
|
|
4081
|
+
const keys = key.match(/[0-9a-f]{128}/g);
|
|
4082
|
+
if (!keys || keys.length !== 4) {
|
|
4083
|
+
return;
|
|
4084
|
+
}
|
|
4085
|
+
const priKey = poseidon(keys.map((k) => BigInt("0x" + k)));
|
|
4086
|
+
return genKeypair(priKey % SNARK_FIELD_SIZE);
|
|
4087
|
+
};
|
|
4088
|
+
var rerandomize = (pubKey, ciphertext, randomVal = genRandomKey()) => {
|
|
4089
|
+
const d1 = babyJub.addPoint(
|
|
4090
|
+
babyJub.mulPointEscalar(babyJub.Base8, randomVal),
|
|
4091
|
+
ciphertext.c1
|
|
4092
|
+
);
|
|
4093
|
+
const d2 = babyJub.addPoint(
|
|
4094
|
+
babyJub.mulPointEscalar(pubKey, randomVal),
|
|
4095
|
+
ciphertext.c2
|
|
4096
|
+
);
|
|
4097
|
+
return {
|
|
4098
|
+
d1,
|
|
4099
|
+
d2
|
|
4100
|
+
};
|
|
4101
|
+
};
|
|
4102
|
+
var genAddKeyProof = async (depth, {
|
|
4103
|
+
coordPubKey,
|
|
4104
|
+
oldKey,
|
|
4105
|
+
deactivates
|
|
4106
|
+
}) => {
|
|
4107
|
+
const sharedKeyHash = poseidon(genEcdhSharedKey(oldKey.privKey, coordPubKey));
|
|
4108
|
+
const randomVal = genRandomKey();
|
|
4109
|
+
const deactivateIdx = deactivates.findIndex((d) => d[4] === sharedKeyHash);
|
|
4110
|
+
if (deactivateIdx < 0) {
|
|
4111
|
+
return null;
|
|
4112
|
+
}
|
|
4113
|
+
const deactivateLeaf = deactivates[deactivateIdx];
|
|
4114
|
+
const c1 = [deactivateLeaf[0], deactivateLeaf[1]];
|
|
4115
|
+
const c2 = [deactivateLeaf[2], deactivateLeaf[3]];
|
|
4116
|
+
const { d1, d2 } = rerandomize(coordPubKey, { c1, c2 }, randomVal);
|
|
4117
|
+
const nullifier = poseidon([oldKey.formatedPrivKey, 1444992409218394441042n]);
|
|
4118
|
+
const tree = new Tree(5, depth, 0n);
|
|
4119
|
+
const leaves = deactivates.map((d) => poseidon(d));
|
|
4120
|
+
tree.initLeaves(leaves);
|
|
4121
|
+
const deactivateRoot = tree.root;
|
|
4122
|
+
const deactivateLeafPathElements = tree.pathElementOf(deactivateIdx);
|
|
4123
|
+
const inputHash = BigInt(
|
|
4124
|
+
solidityPackedSha256(
|
|
4125
|
+
new Array(7).fill("uint256"),
|
|
4126
|
+
stringizing([
|
|
4127
|
+
deactivateRoot,
|
|
4128
|
+
poseidon(coordPubKey),
|
|
4129
|
+
nullifier,
|
|
4130
|
+
d1[0],
|
|
4131
|
+
d1[1],
|
|
4132
|
+
d2[0],
|
|
4133
|
+
d2[1]
|
|
4134
|
+
])
|
|
4135
|
+
)
|
|
4136
|
+
) % SNARK_FIELD_SIZE;
|
|
4137
|
+
const input = {
|
|
4138
|
+
inputHash,
|
|
4139
|
+
coordPubKey,
|
|
4140
|
+
deactivateRoot,
|
|
4141
|
+
deactivateIndex: deactivateIdx,
|
|
4142
|
+
deactivateLeaf: poseidon(deactivateLeaf),
|
|
4143
|
+
c1,
|
|
4144
|
+
c2,
|
|
4145
|
+
randomVal,
|
|
4146
|
+
d1,
|
|
4147
|
+
d2,
|
|
4148
|
+
deactivateLeafPathElements,
|
|
4149
|
+
nullifier,
|
|
4150
|
+
oldPrivateKey: oldKey.formatedPrivKey
|
|
4151
|
+
};
|
|
4152
|
+
return input;
|
|
4153
|
+
};
|
|
4154
|
+
|
|
4155
|
+
// src/libs/circom/index.ts
|
|
4156
|
+
var Circom = class {
|
|
4157
|
+
constructor({ network }) {
|
|
4158
|
+
this.network = network;
|
|
4159
|
+
this.chainId = getDefaultParams(network).chainId;
|
|
4160
|
+
}
|
|
4161
|
+
async signMessage(signer, address, message) {
|
|
4162
|
+
const accounts = await signer.getAccounts();
|
|
4163
|
+
const account = accounts.find((acc) => acc.address === address);
|
|
4164
|
+
if (!account) {
|
|
4165
|
+
throw new Error(`Address ${address} not found in wallet`);
|
|
4166
|
+
}
|
|
4167
|
+
if (isOfflineDirectSigner(signer)) {
|
|
4168
|
+
const signDoc = {
|
|
4169
|
+
bodyBytes: new TextEncoder().encode(message),
|
|
4170
|
+
authInfoBytes: new Uint8Array(),
|
|
4171
|
+
chainId: this.chainId,
|
|
4172
|
+
accountNumber: BigInt(0)
|
|
4173
|
+
};
|
|
4174
|
+
const { signature } = await signer.signDirect(address, signDoc);
|
|
4175
|
+
return {
|
|
4176
|
+
signature: signature.signature,
|
|
4177
|
+
pubkey: account.pubkey
|
|
4178
|
+
};
|
|
4179
|
+
} else {
|
|
4180
|
+
const signDoc = {
|
|
4181
|
+
chain_id: this.chainId,
|
|
4182
|
+
account_number: "0",
|
|
4183
|
+
sequence: "0",
|
|
4184
|
+
fee: {
|
|
4185
|
+
gas: "0",
|
|
4186
|
+
amount: []
|
|
4187
|
+
},
|
|
4188
|
+
msgs: [],
|
|
4189
|
+
memo: message
|
|
4190
|
+
};
|
|
4191
|
+
const { signature } = await signer.signAmino(address, signDoc);
|
|
4192
|
+
return {
|
|
4193
|
+
signature: signature.signature,
|
|
4194
|
+
pubkey: account.pubkey
|
|
4195
|
+
};
|
|
4196
|
+
}
|
|
4197
|
+
}
|
|
4198
|
+
async genKeypairFromSign(signer, address) {
|
|
4199
|
+
const sig = await this.signMessage(
|
|
4200
|
+
signer,
|
|
4201
|
+
address,
|
|
4202
|
+
"Generate_MACI_Private_Key"
|
|
4203
|
+
);
|
|
4204
|
+
const sign = BigInt(
|
|
4205
|
+
"0x" + Buffer.from(sig.signature, "base64").toString("hex")
|
|
4206
|
+
);
|
|
4207
|
+
return genKeypair(sign);
|
|
4208
|
+
}
|
|
3943
4209
|
};
|
|
3944
4210
|
|
|
3945
4211
|
// src/maci.ts
|
|
@@ -3990,6 +4256,7 @@ var MaciClient2 = class {
|
|
|
3990
4256
|
feegrantOperator: this.feegrantOperator,
|
|
3991
4257
|
whitelistBackendPubkey: this.whitelistBackendPubkey
|
|
3992
4258
|
});
|
|
4259
|
+
this.circom = new Circom({ network });
|
|
3993
4260
|
}
|
|
3994
4261
|
async oracleMaciClient({
|
|
3995
4262
|
signer,
|
|
@@ -4168,8 +4435,12 @@ var MaciClient2 = class {
|
|
|
4168
4435
|
return await this.indexer.proof.getProofByContractAddress(address);
|
|
4169
4436
|
}
|
|
4170
4437
|
};
|
|
4438
|
+
|
|
4439
|
+
// src/index.ts
|
|
4440
|
+
import { Scalar as Scalar2, utils as utils2 } from "ffjavascript";
|
|
4441
|
+
import { default as default2 } from "blake-hash";
|
|
4171
4442
|
export {
|
|
4172
|
-
|
|
4443
|
+
Circom,
|
|
4173
4444
|
Circuit,
|
|
4174
4445
|
Contract,
|
|
4175
4446
|
Http,
|
|
@@ -4181,13 +4452,24 @@ export {
|
|
|
4181
4452
|
Operator,
|
|
4182
4453
|
Proof,
|
|
4183
4454
|
Round,
|
|
4455
|
+
Scalar2 as Scalar,
|
|
4184
4456
|
Transaction,
|
|
4457
|
+
UserAccount,
|
|
4458
|
+
batchGenMessage,
|
|
4185
4459
|
circuits,
|
|
4186
4460
|
compressPublicKey,
|
|
4461
|
+
default2 as createBlakeHash,
|
|
4187
4462
|
decompressPublicKey,
|
|
4463
|
+
genAddKeyProof,
|
|
4464
|
+
genEcdhSharedKey,
|
|
4465
|
+
genKeypair,
|
|
4466
|
+
genMessageFactory,
|
|
4188
4467
|
getDefaultParams,
|
|
4189
4468
|
hexToDecimalString,
|
|
4190
4469
|
isValidAddress,
|
|
4470
|
+
privateKeyFromTxt,
|
|
4471
|
+
stringizing,
|
|
4472
|
+
utils2 as utils,
|
|
4191
4473
|
validator_operator_set
|
|
4192
4474
|
};
|
|
4193
4475
|
//# sourceMappingURL=index.mjs.map
|