@dorafactory/maci-sdk 0.1.3-pre.21 → 0.1.3-pre.23
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 +7 -2
- package/dist/index.js +1485 -65
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1470 -74
- package/dist/index.mjs.map +1 -1
- package/dist/libs/account/crypto.d.ts +1 -0
- package/dist/libs/account/index.d.ts +35 -0
- package/dist/libs/account/keypair.d.ts +21 -0
- package/dist/libs/account/util.d.ts +25 -0
- package/dist/libs/api/client.d.ts +173 -0
- package/dist/libs/api/index.d.ts +3 -0
- package/dist/libs/const.d.ts +1 -0
- package/dist/libs/crypto/adapter.d.ts +6 -0
- package/dist/libs/crypto/curve.d.ts +3 -0
- package/dist/libs/crypto/index.d.ts +3 -0
- package/dist/libs/crypto/keys.d.ts +8 -1
- package/dist/libs/crypto/rerandomize.d.ts +12 -0
- package/dist/libs/crypto/sign.d.ts +1 -1
- package/dist/libs/cryptography/index.d.ts +0 -0
- package/dist/libs/cryptography/keypair.d.ts +30 -0
- package/dist/libs/cryptography/mnemonics.d.ts +27 -0
- package/dist/libs/cryptography/publickey.d.ts +23 -0
- package/dist/libs/cryptography/signature-scheme.d.ts +1 -0
- package/dist/libs/index.d.ts +1 -0
- package/dist/libs/keypairs/eddsa-poseidon/index.d.ts +2 -0
- package/dist/libs/keypairs/eddsa-poseidon/keypair.d.ts +187 -0
- package/dist/libs/keypairs/eddsa-poseidon/publickey.d.ts +48 -0
- package/dist/maci.d.ts +90 -31
- package/dist/types/index.d.ts +23 -0
- package/dist/utils/base64.d.ts +6 -0
- package/dist/utils/bech32.d.ts +2 -0
- package/dist/utils/decode-address.d.ts +10 -0
- package/dist/utils/fetch.d.ts +1 -0
- package/dist/utils/hex.d.ts +6 -0
- package/dist/utils/index.d.ts +4 -24
- package/dist/utils/validate-address.d.ts +24 -0
- package/dist/voter.d.ts +230 -0
- package/package.json +13 -61
- package/src/index.ts +11 -13
- package/src/libs/account/crypto.ts +7 -0
- package/src/libs/account/index.ts +70 -0
- package/src/libs/account/keypair.ts +29 -0
- package/src/libs/account/util.ts +55 -0
- package/src/libs/api/client.ts +406 -0
- package/src/libs/api/index.ts +3 -0
- package/src/libs/api/types.d.ts +1493 -0
- package/src/libs/const.ts +37 -54
- package/src/libs/crypto/adapter.ts +41 -0
- package/src/libs/crypto/babyjub.ts +4 -7
- package/src/libs/crypto/constants.ts +2 -5
- package/src/libs/crypto/curve.ts +55 -0
- package/src/libs/crypto/hashing.ts +4 -6
- package/src/libs/crypto/index.ts +3 -0
- package/src/libs/crypto/keys.ts +15 -48
- package/src/libs/crypto/rerandomize.ts +77 -0
- package/src/libs/crypto/sign.ts +8 -17
- package/src/libs/crypto/tree.ts +1 -3
- package/src/libs/cryptography/index.ts +0 -0
- package/src/libs/cryptography/keypair.ts +44 -0
- package/src/libs/cryptography/mnemonics.ts +47 -0
- package/src/libs/cryptography/publickey.ts +44 -0
- package/src/libs/cryptography/signature-scheme.ts +1 -0
- package/src/libs/index.ts +1 -0
- package/src/libs/keypairs/eddsa-poseidon/index.ts +6 -0
- package/src/libs/keypairs/eddsa-poseidon/keypair.ts +452 -0
- package/src/libs/keypairs/eddsa-poseidon/publickey.ts +141 -0
- package/src/maci.ts +157 -101
- package/src/types/index.ts +30 -4
- package/src/types/lib.d.ts +7 -0
- package/src/utils/base64.ts +28 -0
- package/src/utils/bech32.ts +29 -0
- package/src/utils/decode-address.ts +35 -0
- package/src/utils/fetch.ts +28 -0
- package/src/utils/hex.ts +23 -0
- package/src/utils/index.ts +15 -79
- package/src/utils/validate-address.ts +82 -0
- package/src/voter.ts +654 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const generateMnemonic: (numberOfWords?: 12 | 24) => string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EdDSAPoseidonKeypair, EdDSAPoseidonPublicKey } from '../keypairs/eddsa-poseidon';
|
|
2
|
+
import type { AccountMangerParams, DerivePathParams } from 'src/types';
|
|
3
|
+
export { generateMnemonic } from './crypto';
|
|
4
|
+
export { getKeyPair } from './keypair';
|
|
5
|
+
export declare class MaciAccount {
|
|
6
|
+
private mnemonic;
|
|
7
|
+
private secretKey;
|
|
8
|
+
currentKeypair: EdDSAPoseidonKeypair;
|
|
9
|
+
currentPubkey: EdDSAPoseidonPublicKey;
|
|
10
|
+
/**
|
|
11
|
+
* Support the following ways to init the SuiToolkit:
|
|
12
|
+
* 1. mnemonics
|
|
13
|
+
* 2. secretKey (base64 or hex)
|
|
14
|
+
* If none of them is provided, will generate a random mnemonics with 24 words.
|
|
15
|
+
*
|
|
16
|
+
* @param mnemonics, 12 or 24 mnemonics words, separated by space
|
|
17
|
+
* @param secretKey, base64 or hex string or Bech32 string, when mnemonics is provided, secretKey will be ignored
|
|
18
|
+
*/
|
|
19
|
+
constructor({ mnemonic, secretKey }?: AccountMangerParams);
|
|
20
|
+
/**
|
|
21
|
+
* Check if the secretKey starts with bench32 format
|
|
22
|
+
*/
|
|
23
|
+
parseSecretKey(secretKey: string | bigint): EdDSAPoseidonKeypair;
|
|
24
|
+
/**
|
|
25
|
+
* if derivePathParams is not provided or mnemonics is empty, it will return the currentKeyPair.
|
|
26
|
+
* else:
|
|
27
|
+
* it will generate keyPair from the mnemonic with the given derivePathParams.
|
|
28
|
+
*/
|
|
29
|
+
getKeyPair(derivePathParams?: DerivePathParams): EdDSAPoseidonKeypair;
|
|
30
|
+
/**
|
|
31
|
+
* Switch the current account with the given derivePathParams.
|
|
32
|
+
* This is only useful when the mnemonic is provided. For secretKey mode, it will always use the same account.
|
|
33
|
+
*/
|
|
34
|
+
switchAccount(derivePathParams: DerivePathParams): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DerivePathParams } from '../../types';
|
|
2
|
+
import { EdDSAPoseidonKeypair } from '../keypairs/eddsa-poseidon';
|
|
3
|
+
/**
|
|
4
|
+
* @description Get eddsaposeidon derive path for MACI
|
|
5
|
+
* @param derivePathParams
|
|
6
|
+
*/
|
|
7
|
+
export declare const getDerivePathForMACI: (derivePathParams?: DerivePathParams) => string;
|
|
8
|
+
/**
|
|
9
|
+
* the format is m/44'/118'/accountIndex'/${isExternal ? 1 : 0}/addressIndex
|
|
10
|
+
*
|
|
11
|
+
* accountIndex is the index of the account, default is 0.
|
|
12
|
+
*
|
|
13
|
+
* isExternal is the type of the address, default is false. Usually, the external address is used to receive coins. The internal address is used to change coins.
|
|
14
|
+
*
|
|
15
|
+
* addressIndex is the index of the address, default is 0. It's used to generate multiple addresses for one account.
|
|
16
|
+
*
|
|
17
|
+
* @description Get keypair from mnemonics and derive path
|
|
18
|
+
* @param mnemonics
|
|
19
|
+
* @param derivePathParams
|
|
20
|
+
*/
|
|
21
|
+
export declare const getKeyPair: (mnemonics: string, derivePathParams?: DerivePathParams) => EdDSAPoseidonKeypair;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description This regular expression matches any string that contains only hexadecimal digits (0-9, A-F, a-f).
|
|
3
|
+
* @param str
|
|
4
|
+
*/
|
|
5
|
+
export declare const isHex: (str: string) => boolean;
|
|
6
|
+
/**
|
|
7
|
+
* @description This regular expression matches any string that contains only base64 digits (0-9, A-Z, a-z, +, /, =).
|
|
8
|
+
* Note that the "=" signs at the end are optional padding characters that may be present in some base64 encoded strings.
|
|
9
|
+
* @param str
|
|
10
|
+
*/
|
|
11
|
+
export declare const isBase64: (str: string) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Convert a hex string to Uint8Array
|
|
14
|
+
* @param hexStr
|
|
15
|
+
*/
|
|
16
|
+
export declare const fromHEX: (hexStr: string) => Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* normalize a private key
|
|
19
|
+
* A private key is a 32-byte array.
|
|
20
|
+
* But there are two different formats for private keys:
|
|
21
|
+
* 1. A 32-byte array
|
|
22
|
+
* 2. A 64-byte array with the first 32 bytes being the private key and the last 32 bytes being the public key
|
|
23
|
+
* 3. A 33-byte array with the first byte being 0x00 (sui.keystore key is a Base64 string with scheme flag 0x00 at the beginning)
|
|
24
|
+
*/
|
|
25
|
+
export declare const normalizePrivateKey: (key: Uint8Array) => Uint8Array;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type { paths, operations } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* MACI API Client configuration
|
|
4
|
+
*/
|
|
5
|
+
export interface MaciApiClientConfig {
|
|
6
|
+
/** API base URL */
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
/** API key for authentication */
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
/** Custom fetch function (optional) */
|
|
11
|
+
customFetch?: typeof fetch;
|
|
12
|
+
/** Default request timeout in milliseconds */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
/**
|
|
15
|
+
* API key header type
|
|
16
|
+
* - 'x-api-key': Use x-api-key header (default)
|
|
17
|
+
* - 'bearer': Use Authorization: Bearer header
|
|
18
|
+
*/
|
|
19
|
+
apiKeyHeader?: 'x-api-key' | 'bearer';
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Extract request body type from operation
|
|
23
|
+
*/
|
|
24
|
+
type RequestBody<T> = T extends {
|
|
25
|
+
requestBody: {
|
|
26
|
+
content: {
|
|
27
|
+
'application/json': infer U;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
} ? U : T extends {
|
|
31
|
+
requestBody?: {
|
|
32
|
+
content: {
|
|
33
|
+
'application/json': infer U;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
} ? U : never;
|
|
37
|
+
/**
|
|
38
|
+
* Extract response type from operation
|
|
39
|
+
*/
|
|
40
|
+
type ResponseBody<T, Status extends number = 200> = T extends {
|
|
41
|
+
responses: {
|
|
42
|
+
[K in Status]: {
|
|
43
|
+
content: {
|
|
44
|
+
'application/json': infer U;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
} ? U : never;
|
|
49
|
+
/**
|
|
50
|
+
* Extract query parameters from operation
|
|
51
|
+
*/
|
|
52
|
+
type QueryParams<T> = T extends {
|
|
53
|
+
parameters: {
|
|
54
|
+
query?: infer U;
|
|
55
|
+
};
|
|
56
|
+
} ? U : never;
|
|
57
|
+
/**
|
|
58
|
+
* Extract path parameters from operation
|
|
59
|
+
*/
|
|
60
|
+
type PathParams<T> = T extends {
|
|
61
|
+
parameters: {
|
|
62
|
+
path: infer U;
|
|
63
|
+
};
|
|
64
|
+
} ? U : never;
|
|
65
|
+
/**
|
|
66
|
+
* MACI API Client
|
|
67
|
+
* Provides type-safe MACI API call wrapper
|
|
68
|
+
*/
|
|
69
|
+
export declare class MaciApiClient {
|
|
70
|
+
private baseUrl;
|
|
71
|
+
private apiKey?;
|
|
72
|
+
private customFetch;
|
|
73
|
+
private timeout;
|
|
74
|
+
private apiKeyHeader;
|
|
75
|
+
constructor(config: MaciApiClientConfig);
|
|
76
|
+
/**
|
|
77
|
+
* Update API key
|
|
78
|
+
*/
|
|
79
|
+
setApiKey(apiKey: string): void;
|
|
80
|
+
/**
|
|
81
|
+
* Update base URL
|
|
82
|
+
*/
|
|
83
|
+
setBaseUrl(baseUrl: string): void;
|
|
84
|
+
/**
|
|
85
|
+
* Internal fetch method with timeout and error handling
|
|
86
|
+
*/
|
|
87
|
+
private fetch;
|
|
88
|
+
/**
|
|
89
|
+
* Health check
|
|
90
|
+
*/
|
|
91
|
+
health(): Promise<ResponseBody<paths['/health']['get'], 200>>;
|
|
92
|
+
/**
|
|
93
|
+
* Create API key (Admin)
|
|
94
|
+
*/
|
|
95
|
+
createApiKey(data: RequestBody<paths['/admin/keys']['post']>): Promise<ResponseBody<paths['/admin/keys']['post'], 200>>;
|
|
96
|
+
/**
|
|
97
|
+
* Create tenant (Admin)
|
|
98
|
+
*/
|
|
99
|
+
createTenant(data: RequestBody<paths['/admin/tenants']['post']>): Promise<ResponseBody<paths['/admin/tenants']['post'], 201>>;
|
|
100
|
+
/**
|
|
101
|
+
* Get usage for current API key
|
|
102
|
+
*/
|
|
103
|
+
getUsage(params?: QueryParams<paths['/v1/usage']['get']>): Promise<ResponseBody<paths['/v1/usage']['get'], 200>>;
|
|
104
|
+
/**
|
|
105
|
+
* Signup
|
|
106
|
+
*/
|
|
107
|
+
signup(data: RequestBody<operations['signup']>): Promise<ResponseBody<operations['signup'], 202>>;
|
|
108
|
+
/**
|
|
109
|
+
* Vote
|
|
110
|
+
*/
|
|
111
|
+
vote(data: RequestBody<operations['vote']>): Promise<ResponseBody<operations['vote'], 202>>;
|
|
112
|
+
/**
|
|
113
|
+
* Create Round
|
|
114
|
+
*/
|
|
115
|
+
createRound(data: RequestBody<operations['createRound']>): Promise<ResponseBody<operations['createRound'], 202>>;
|
|
116
|
+
/**
|
|
117
|
+
* Create AMaci Round
|
|
118
|
+
*/
|
|
119
|
+
createAmaciRound(data: RequestBody<operations['createAmaciRound']>): Promise<ResponseBody<operations['createAmaciRound'], 202>>;
|
|
120
|
+
/**
|
|
121
|
+
* Set Round Info
|
|
122
|
+
*/
|
|
123
|
+
setRoundInfo(data: RequestBody<operations['setRoundInfo']>): Promise<ResponseBody<operations['setRoundInfo'], 202>>;
|
|
124
|
+
/**
|
|
125
|
+
* Set Vote Options
|
|
126
|
+
*/
|
|
127
|
+
setVoteOptions(data: RequestBody<operations['setVoteOptions']>): Promise<ResponseBody<operations['setVoteOptions'], 202>>;
|
|
128
|
+
/**
|
|
129
|
+
* Deactivate
|
|
130
|
+
*/
|
|
131
|
+
deactivate(data: RequestBody<operations['deactivate']>): Promise<ResponseBody<operations['deactivate'], 202>>;
|
|
132
|
+
/**
|
|
133
|
+
* Add New Key
|
|
134
|
+
*/
|
|
135
|
+
addNewKey(data: RequestBody<operations['addNewKey']>): Promise<ResponseBody<operations['addNewKey'], 202>>;
|
|
136
|
+
/**
|
|
137
|
+
* Pre Add New Key
|
|
138
|
+
*/
|
|
139
|
+
preAddNewKey(data: RequestBody<operations['preAddNewKey']>): Promise<ResponseBody<operations['preAddNewKey'], 202>>;
|
|
140
|
+
/**
|
|
141
|
+
* Get allowlist list
|
|
142
|
+
*/
|
|
143
|
+
getAllowlists(params?: QueryParams<operations['getAllowlists']>): Promise<ResponseBody<operations['getAllowlists'], 200>>;
|
|
144
|
+
/**
|
|
145
|
+
* Create allowlist
|
|
146
|
+
*/
|
|
147
|
+
createAllowlist(data: RequestBody<operations['createAllowlist']>): Promise<ResponseBody<operations['createAllowlist'], 201>>;
|
|
148
|
+
/**
|
|
149
|
+
* Get allowlist details
|
|
150
|
+
*/
|
|
151
|
+
getAllowlistDetail(params: PathParams<operations['getAllowlistDetail']>): Promise<ResponseBody<operations['getAllowlistDetail'], 200>>;
|
|
152
|
+
/**
|
|
153
|
+
* Update allowlist
|
|
154
|
+
*/
|
|
155
|
+
updateAllowlist(params: PathParams<operations['updateAllowlist']>, data: RequestBody<operations['updateAllowlist']>): Promise<ResponseBody<operations['updateAllowlist'], 200>>;
|
|
156
|
+
/**
|
|
157
|
+
* Delete allowlist
|
|
158
|
+
*/
|
|
159
|
+
deleteAllowlist(params: PathParams<operations['deleteAllowlist']>): Promise<ResponseBody<operations['deleteAllowlist'], 204>>;
|
|
160
|
+
/**
|
|
161
|
+
* Get round allowlist snapshot
|
|
162
|
+
*/
|
|
163
|
+
getRoundAllowlistSnapshot(params: PathParams<operations['getRoundAllowlistSnapshot']>): Promise<ResponseBody<operations['getRoundAllowlistSnapshot'], 200>>;
|
|
164
|
+
/**
|
|
165
|
+
* Request certificate
|
|
166
|
+
*/
|
|
167
|
+
requestCertificate(data: RequestBody<paths['/v1/certificates/request']['post']>): Promise<ResponseBody<paths['/v1/certificates/request']['post'], 200>>;
|
|
168
|
+
/**
|
|
169
|
+
* Get pre-deactivate data by contract address
|
|
170
|
+
*/
|
|
171
|
+
getPreDeactivate(params: PathParams<paths['/v1/pre-deactivate/{contractAddress}']['get']>): Promise<ResponseBody<paths['/v1/pre-deactivate/{contractAddress}']['get'], 200>>;
|
|
172
|
+
}
|
|
173
|
+
export {};
|
package/dist/libs/const.d.ts
CHANGED
|
@@ -53,7 +53,14 @@ export declare const batchGenMessage: (stateIdx: number, keypair: Keypair, coord
|
|
|
53
53
|
encPubkeys: PubKey<bigint>;
|
|
54
54
|
}[];
|
|
55
55
|
export declare const privateKeyFromTxt: (txt: string) => Keypair | undefined;
|
|
56
|
-
export declare const
|
|
56
|
+
export declare const rerandomize: (pubKey: bigint[], ciphertext: {
|
|
57
|
+
c1: bigint[];
|
|
58
|
+
c2: bigint[];
|
|
59
|
+
}, randomVal?: bigint) => {
|
|
60
|
+
d1: bigint[];
|
|
61
|
+
d2: bigint[];
|
|
62
|
+
};
|
|
63
|
+
export declare const genAddKeyInput: (depth: number, { coordPubKey, oldKey, deactivates }: {
|
|
57
64
|
coordPubKey: PubKey;
|
|
58
65
|
oldKey: Keypair;
|
|
59
66
|
deactivates: bigint[][];
|
|
@@ -2,7 +2,7 @@ import { OfflineSigner } from '@cosmjs/proto-signing';
|
|
|
2
2
|
import { SignResult } from './types';
|
|
3
3
|
export declare function signMessage(signer: OfflineSigner, address: string, message: string, network: 'mainnet' | 'testnet'): Promise<SignResult>;
|
|
4
4
|
export declare function genKeypairFromSignature(signature: string): Promise<import("./types").Keypair>;
|
|
5
|
-
export declare function genKeypairFromSign({ signer, address, network
|
|
5
|
+
export declare function genKeypairFromSign({ signer, address, network }: {
|
|
6
6
|
signer: OfflineSigner;
|
|
7
7
|
address?: string;
|
|
8
8
|
network: 'mainnet' | 'testnet';
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Signature } from '@zk-kit/eddsa-poseidon';
|
|
2
|
+
import type { PublicKey } from './publickey';
|
|
3
|
+
import type { SignatureScheme } from './signature-scheme';
|
|
4
|
+
import { BigNumberish } from '@zk-kit/utils';
|
|
5
|
+
export interface SignatureWithBytes {
|
|
6
|
+
message: BigNumberish;
|
|
7
|
+
signature: Signature<bigint>;
|
|
8
|
+
}
|
|
9
|
+
export declare abstract class Signer {
|
|
10
|
+
abstract sign(message: BigNumberish): Signature<bigint>;
|
|
11
|
+
/**
|
|
12
|
+
* Sign messages with a specific payload. By combining the message bytes with the payload before hashing and signing,
|
|
13
|
+
* it ensures that a signed message is tied to a specific purpose and domain separator is provided
|
|
14
|
+
*/
|
|
15
|
+
signWithPayload(message: BigNumberish): Promise<SignatureWithBytes>;
|
|
16
|
+
/**
|
|
17
|
+
* Get the key scheme of the keypair: Secp256k1 or ED25519
|
|
18
|
+
*/
|
|
19
|
+
abstract getKeyScheme(): SignatureScheme;
|
|
20
|
+
/**
|
|
21
|
+
* The public key for this keypair
|
|
22
|
+
*/
|
|
23
|
+
abstract getPublicKey(): PublicKey;
|
|
24
|
+
}
|
|
25
|
+
export declare abstract class Keypair extends Signer {
|
|
26
|
+
/**
|
|
27
|
+
* This returns the Bech32 secret key string for this keypair.
|
|
28
|
+
*/
|
|
29
|
+
abstract getSecretKey(): string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse and validate a path that is compliant to SLIP-0010 in form m/44'/118'/{account_index}'/{change_index}'/{address_index}'.
|
|
3
|
+
*
|
|
4
|
+
* @param path path string (e.g. `m/44'/118'/0'/0'/0'`).
|
|
5
|
+
*/
|
|
6
|
+
export declare function isValidHardenedPath(path: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Parse and validate a path that is compliant to BIP-32 in form m/54'/784'/{account_index}'/{change_index}/{address_index}
|
|
9
|
+
* for Secp256k1 and m/74'/784'/{account_index}'/{change_index}/{address_index} for Secp256r1.
|
|
10
|
+
*
|
|
11
|
+
* Note that the purpose for Secp256k1 is registered as 54, to differentiate from Ed25519 with purpose 44.
|
|
12
|
+
*
|
|
13
|
+
* @param path path string (e.g. `m/54'/118'/0'/0/0`).
|
|
14
|
+
*/
|
|
15
|
+
export declare function isValidBIP32Path(path: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Uses KDF to derive 64 bytes of key data from mnemonic with empty password.
|
|
18
|
+
*
|
|
19
|
+
* @param mnemonics 12 words string split by spaces.
|
|
20
|
+
*/
|
|
21
|
+
export declare function mnemonicToSeed(mnemonics: string): Uint8Array;
|
|
22
|
+
/**
|
|
23
|
+
* Derive the seed in hex format from a 12-word mnemonic string.
|
|
24
|
+
*
|
|
25
|
+
* @param mnemonics 12 words string split by spaces.
|
|
26
|
+
*/
|
|
27
|
+
export declare function mnemonicToSeedHex(mnemonics: string): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Signature } from '@zk-kit/eddsa-poseidon';
|
|
2
|
+
/**
|
|
3
|
+
* Value to be converted into public key.
|
|
4
|
+
*/
|
|
5
|
+
export type PublicKeyInitData = string | Uint8Array | Iterable<number>;
|
|
6
|
+
export declare function bytesEqual(a: Uint8Array, b: Uint8Array): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* A public key
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class PublicKey {
|
|
11
|
+
/**
|
|
12
|
+
* Checks if two public keys are equal
|
|
13
|
+
*/
|
|
14
|
+
equals(publicKey: PublicKey): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Converts the public key to a packed data format
|
|
17
|
+
*/
|
|
18
|
+
abstract toPackedData(): bigint;
|
|
19
|
+
/**
|
|
20
|
+
* Verifies that the signature is valid for for the provided message
|
|
21
|
+
*/
|
|
22
|
+
abstract verify(data: Uint8Array, signature: Signature): boolean;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type SignatureScheme = 'EDDSA_POSEIDON';
|
package/dist/libs/index.d.ts
CHANGED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file EdDSA-Poseidon Keypair Implementation
|
|
3
|
+
* @description This module provides a complete implementation of EdDSA-Poseidon keypairs
|
|
4
|
+
* for use in zero-knowledge proof systems and MACI (Minimal Anti-Collusion Infrastructure).
|
|
5
|
+
*
|
|
6
|
+
* The EdDSA-Poseidon signature scheme combines:
|
|
7
|
+
* - Edwards-curve Digital Signature Algorithm (EdDSA) for efficient elliptic curve cryptography
|
|
8
|
+
* - Poseidon hash function for ZK-SNARK friendly hashing
|
|
9
|
+
* - BIP-32 hierarchical deterministic key derivation
|
|
10
|
+
* - Support for both JSON payload signing and ZK-optimized credential signing
|
|
11
|
+
*
|
|
12
|
+
* Key features:
|
|
13
|
+
* - Random keypair generation
|
|
14
|
+
* - Keypair derivation from mnemonic phrases
|
|
15
|
+
* - Keypair reconstruction from secret keys
|
|
16
|
+
* - Multiple signing methods for different use cases
|
|
17
|
+
* - Comprehensive input validation and error handling
|
|
18
|
+
*
|
|
19
|
+
* @author MACI Team
|
|
20
|
+
* @version 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
import { Keypair } from '../../cryptography/keypair';
|
|
23
|
+
import type { SignatureScheme } from '../../cryptography/signature-scheme';
|
|
24
|
+
import { EdDSAPoseidonPublicKey } from './publickey';
|
|
25
|
+
import { EcdhSharedKey, PubKey, Tree } from '../../crypto';
|
|
26
|
+
import { BigNumberish } from '@zk-kit/utils';
|
|
27
|
+
export declare const DEFAULT_EDDSA_POSEIDON_DERIVATION_PATH = "m/44'/118'/0'/0/0";
|
|
28
|
+
/**
|
|
29
|
+
* Interface defining the structure of EdDSA-Poseidon keypair data
|
|
30
|
+
* Contains both the public and secret key components as bigint values
|
|
31
|
+
*/
|
|
32
|
+
export interface EdDSAPoseidonKeypairData {
|
|
33
|
+
/** The public key component as a bigint */
|
|
34
|
+
publicKey: bigint;
|
|
35
|
+
/** The secret key component as a bigint */
|
|
36
|
+
secretKey: bigint;
|
|
37
|
+
formatedPrivKey: bigint;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* EdDSA-Poseidon Keypair implementation for zero-knowledge proof systems
|
|
41
|
+
*
|
|
42
|
+
* This class implements the Edwards-curve Digital Signature Algorithm (EdDSA)
|
|
43
|
+
* using the Poseidon hash function, which is optimized for zero-knowledge circuits.
|
|
44
|
+
* It extends the base Keypair class and provides methods for key generation,
|
|
45
|
+
* signing, and cryptographic operations suitable for MACI (Minimal Anti-Collusion Infrastructure).
|
|
46
|
+
*/
|
|
47
|
+
export declare class EdDSAPoseidonKeypair extends Keypair {
|
|
48
|
+
/** Private storage for the keypair data */
|
|
49
|
+
private keypair;
|
|
50
|
+
/**
|
|
51
|
+
* Create a new EdDSA-Poseidon keypair instance.
|
|
52
|
+
*
|
|
53
|
+
* If no keypair data is provided, a new random keypair will be generated
|
|
54
|
+
* using cryptographically secure random number generation.
|
|
55
|
+
*
|
|
56
|
+
* @param keypair Optional existing EdDSA-Poseidon keypair data to initialize with
|
|
57
|
+
*/
|
|
58
|
+
constructor(keypair?: EdDSAPoseidonKeypairData);
|
|
59
|
+
/**
|
|
60
|
+
* Get the signature scheme identifier for this keypair.
|
|
61
|
+
*
|
|
62
|
+
* @returns The signature scheme type 'EDDSA_POSEIDON'
|
|
63
|
+
*/
|
|
64
|
+
getKeyScheme(): SignatureScheme;
|
|
65
|
+
/**
|
|
66
|
+
* Generate a new random EdDSA-Poseidon keypair.
|
|
67
|
+
*
|
|
68
|
+
* This is a convenience static method that creates a new instance
|
|
69
|
+
* with randomly generated cryptographic keys.
|
|
70
|
+
*
|
|
71
|
+
* @returns A new EdDSAPoseidonKeypair instance with random keys
|
|
72
|
+
*/
|
|
73
|
+
static generate(): EdDSAPoseidonKeypair;
|
|
74
|
+
/**
|
|
75
|
+
* Create a keypair from an existing secret key.
|
|
76
|
+
*
|
|
77
|
+
* This method reconstructs a keypair from a previously generated secret key.
|
|
78
|
+
* The secret key can be provided as either a hex string (with or without '0x' prefix)
|
|
79
|
+
* or as a bigint value. For generating keypairs from mnemonic seeds, use the
|
|
80
|
+
* {@link EdDSAPoseidonKeypair.deriveKeypair} method instead.
|
|
81
|
+
*
|
|
82
|
+
* @param secretKey The secret key as a hex string or bigint
|
|
83
|
+
* @param options Configuration options for key reconstruction
|
|
84
|
+
* @param options.skipValidation If true, skips cryptographic validation of the secret key
|
|
85
|
+
*
|
|
86
|
+
* @returns A new EdDSAPoseidonKeypair instance created from the secret key
|
|
87
|
+
* @throws Error if the provided secret key is invalid and validation is not skipped
|
|
88
|
+
*/
|
|
89
|
+
static fromSecretKey(secretKey: string | bigint, options?: {
|
|
90
|
+
skipValidation?: boolean;
|
|
91
|
+
}): EdDSAPoseidonKeypair;
|
|
92
|
+
/**
|
|
93
|
+
* Get the public key associated with this keypair.
|
|
94
|
+
*
|
|
95
|
+
* @returns An EdDSAPoseidonPublicKey instance containing the public key data
|
|
96
|
+
*/
|
|
97
|
+
getPublicKey(): EdDSAPoseidonPublicKey;
|
|
98
|
+
/**
|
|
99
|
+
* Get the secret key as a hexadecimal string.
|
|
100
|
+
*
|
|
101
|
+
* Returns the secret key component of this keypair encoded as a hex string.
|
|
102
|
+
* This can be used to serialize the secret key for storage or transmission.
|
|
103
|
+
*
|
|
104
|
+
* @returns The secret key encoded as a hexadecimal string
|
|
105
|
+
*/
|
|
106
|
+
getSecretKey(): string;
|
|
107
|
+
getFormatedPrivKey(): bigint;
|
|
108
|
+
/**
|
|
109
|
+
* Sign a message using this keypair's secret key.
|
|
110
|
+
*
|
|
111
|
+
* Creates a cryptographic signature for the provided message using the
|
|
112
|
+
* EdDSA-Poseidon signature algorithm. The signature can later be verified
|
|
113
|
+
* using the corresponding public key.
|
|
114
|
+
*
|
|
115
|
+
* @param message The message to sign (as BigNumberish - can be bigint, string, or number)
|
|
116
|
+
* @returns The cryptographic signature of the message
|
|
117
|
+
*/
|
|
118
|
+
sign(message: BigNumberish): import("@zk-kit/eddsa-poseidon").Signature<bigint>;
|
|
119
|
+
/**
|
|
120
|
+
* Derive an EdDSA-Poseidon keypair from a mnemonic phrase and derivation path.
|
|
121
|
+
*
|
|
122
|
+
* This method implements hierarchical deterministic (HD) key derivation following
|
|
123
|
+
* the BIP-32 standard. The mnemonic phrase must be normalized and validated against
|
|
124
|
+
* the English wordlist before use.
|
|
125
|
+
*
|
|
126
|
+
* @param mnemonics The mnemonic phrase (12, 15, 18, 21, or 24 words)
|
|
127
|
+
* @param path Optional BIP-32 derivation path. If not provided, uses the default path.
|
|
128
|
+
* Must be in the form m/44'/118'/{account_index}'/{change_index}/{address_index}
|
|
129
|
+
*
|
|
130
|
+
* @returns A new EdDSAPoseidonKeypair derived from the mnemonic and path
|
|
131
|
+
* @throws Error if the derivation path is invalid or key derivation fails
|
|
132
|
+
*/
|
|
133
|
+
static deriveKeypair(mnemonics: string, path?: string): EdDSAPoseidonKeypair;
|
|
134
|
+
/**
|
|
135
|
+
* Sign a payload containing contract address and amount information.
|
|
136
|
+
*
|
|
137
|
+
* This method creates a structured payload with the contract address, amount,
|
|
138
|
+
* and public key coordinates, then generates a cryptographic signature over
|
|
139
|
+
* the JSON-serialized payload using SHA-256 hashing.
|
|
140
|
+
*
|
|
141
|
+
* The resulting signature can be used as a certificate or proof of authorization
|
|
142
|
+
* for the specified contract address and amount.
|
|
143
|
+
*
|
|
144
|
+
* @param params The payload parameters
|
|
145
|
+
* @param params.amount The amount value as a string
|
|
146
|
+
* @param params.contractAddress The contract address to include in the payload
|
|
147
|
+
*
|
|
148
|
+
* @returns Base64-encoded signature of the payload
|
|
149
|
+
*/
|
|
150
|
+
signPayload({ amount, contractAddress, }: {
|
|
151
|
+
amount: string;
|
|
152
|
+
contractAddress: string;
|
|
153
|
+
}): string;
|
|
154
|
+
/**
|
|
155
|
+
* Sign a credential containing contract address and amount using Poseidon hashing.
|
|
156
|
+
*
|
|
157
|
+
* This method creates a cryptographic credential by signing a structured message
|
|
158
|
+
* using the Poseidon hash function (hash5). The message includes the amount,
|
|
159
|
+
* contract address (converted to uint256), public key coordinates, and a zero nonce.
|
|
160
|
+
*
|
|
161
|
+
* This type of credential is optimized for zero-knowledge proof systems where
|
|
162
|
+
* Poseidon hashing is more efficient than traditional hash functions.
|
|
163
|
+
*
|
|
164
|
+
* @param params The credential parameters
|
|
165
|
+
* @param params.amount The amount value as a string (will be converted to BigInt)
|
|
166
|
+
* @param params.contractAddress The contract address (will be converted to uint256)
|
|
167
|
+
*
|
|
168
|
+
* @returns Base64-encoded signature of the credential
|
|
169
|
+
*/
|
|
170
|
+
signCredential({ amount, contractAddress, }: {
|
|
171
|
+
amount: string;
|
|
172
|
+
contractAddress: string;
|
|
173
|
+
}): string;
|
|
174
|
+
/**
|
|
175
|
+
* Generates an Elliptic-Curve Diffie–Hellman (ECDH) shared key given a private
|
|
176
|
+
* key and a public key.
|
|
177
|
+
* @param pubKey A public key generated using genPubKey()
|
|
178
|
+
* @returns The ECDH shared key.
|
|
179
|
+
*/
|
|
180
|
+
genEcdhSharedKey(pubKey: PubKey): EcdhSharedKey;
|
|
181
|
+
genDeactivateRoot(accounts: PubKey[] | bigint[], stateTreeDepth: number): {
|
|
182
|
+
deactivates: bigint[][];
|
|
183
|
+
root: bigint;
|
|
184
|
+
leaves: bigint[];
|
|
185
|
+
tree: Tree;
|
|
186
|
+
};
|
|
187
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { PublicKey } from '../../cryptography/publickey';
|
|
2
|
+
import type { PublicKeyInitData } from '../../cryptography/publickey';
|
|
3
|
+
import { Signature } from '@zk-kit/eddsa-poseidon';
|
|
4
|
+
import { PubKey } from '../../crypto';
|
|
5
|
+
/**
|
|
6
|
+
* An EdDSAPoseidon public key
|
|
7
|
+
*/
|
|
8
|
+
export declare class EdDSAPoseidonPublicKey extends PublicKey {
|
|
9
|
+
private packedData;
|
|
10
|
+
private rawPoint;
|
|
11
|
+
/**
|
|
12
|
+
* Create a new EdDSAPoseidonPublicKey object
|
|
13
|
+
* @param value public key as packed bigint, Point array, Uint8Array or string
|
|
14
|
+
*/
|
|
15
|
+
constructor(value: PublicKeyInitData | bigint | PubKey);
|
|
16
|
+
/**
|
|
17
|
+
* Checks if two EdDSAPoseidon public keys are equal
|
|
18
|
+
*/
|
|
19
|
+
equals(publicKey: EdDSAPoseidonPublicKey): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Return the Point representation of the EdDSAPoseidon public key
|
|
22
|
+
*/
|
|
23
|
+
toPoints(): PubKey;
|
|
24
|
+
/**
|
|
25
|
+
* Return the packed bigint representation of the EdDSAPoseidon public key
|
|
26
|
+
*/
|
|
27
|
+
toPackedData(): bigint;
|
|
28
|
+
/**
|
|
29
|
+
* Verifies that the signature is valid for for the provided message
|
|
30
|
+
*/
|
|
31
|
+
verify(message: Uint8Array | string, signature: Signature): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Verifies that the signature is valid for the provided address and amount
|
|
34
|
+
*/
|
|
35
|
+
verifyPayload({ amount, contractAddress, signature }: {
|
|
36
|
+
amount: string;
|
|
37
|
+
contractAddress: string;
|
|
38
|
+
signature: string;
|
|
39
|
+
}): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Verifies that the signature is valid for the provided address and amount
|
|
42
|
+
*/
|
|
43
|
+
verifyCredential({ amount, contractAddress, signature }: {
|
|
44
|
+
amount: string;
|
|
45
|
+
contractAddress: string;
|
|
46
|
+
signature: string;
|
|
47
|
+
}): boolean;
|
|
48
|
+
}
|