forked from Bitcoindefi/Open-Stellar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator-client.ts
More file actions
183 lines (166 loc) · 10.3 KB
/
Copy pathvalidator-client.ts
File metadata and controls
183 lines (166 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { Buffer } from "buffer";
import { Address } from "@stellar/stellar-sdk";
import {
AssembledTransaction,
Client as ContractClient,
ClientOptions as ContractClientOptions,
MethodOptions,
Result,
Spec as ContractSpec,
} from "@stellar/stellar-sdk/contract";
import type {
u32,
i32,
u64,
i64,
u128,
i128,
u256,
i256,
Option,
Timepoint,
Duration,
} from "@stellar/stellar-sdk/contract";
export * from "@stellar/stellar-sdk";
export * as contract from "@stellar/stellar-sdk/contract";
export * as rpc from "@stellar/stellar-sdk/rpc";
if (typeof window !== "undefined") {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore Buffer exists
window.Buffer = window.Buffer || Buffer;
}
export type StellarNetwork = "testnet" | "mainnet";
export const networks = {
testnet: {
networkPassphrase: "Test SDF Network ; September 2015",
rpcUrl: "https://soroban-testnet.stellar.org",
contractId: process.env.NEXT_PUBLIC_STELLAR_TESTNET_VALIDATOR_CONTRACT_ID ?? "CDNSZUNEWFCGSPWLPDSWTENR2WPHKC34RGZQG7RJA54OPGTZGVVRFYBA",
verifierContractId: process.env.NEXT_PUBLIC_STELLAR_TESTNET_VERIFIER_CONTRACT_ID ?? "CCMKLYSRUH2HMA4UU6WLXWQXEY6KAH5AWB5BEVMJGNGC5GLGTVROLG4A",
},
mainnet: {
networkPassphrase: "Public Global Stellar Network ; September 2015",
rpcUrl: "https://mainnet.sorobanrpc.com",
contractId: process.env.NEXT_PUBLIC_STELLAR_MAINNET_VALIDATOR_CONTRACT_ID ?? "",
verifierContractId: process.env.NEXT_PUBLIC_STELLAR_MAINNET_VERIFIER_CONTRACT_ID ?? "",
},
} as const
export const Errors = {
1: {message:"NotInitialized"},
2: {message:"AlreadyInitialized"},
/**
* Wrong number of public inputs for the agent_passport circuit.
*/
3: {message:"BadPublicInputs"},
/**
* This nullifier was already spent — replay / Sybil attempt.
*/
4: {message:"NullifierUsed"},
/**
* The Groth16 proof did not verify against the embedded key.
*/
5: {message:"InvalidProof"}
}
export interface Attestation {
agent_id: u256;
/**
* Ledger sequence at which the passport was minted.
*/
ledger: u32;
nullifier: u256;
registry_root: u256;
spend_cap: u256;
}
/**
* Groth16 proof over BN254, re-declared in *this* contract's spec (the
* imported one isn't exported) so SDKs/CLI can build the argument directly.
* Byte layout: G1 `a`/`c` = x||y (32B BE each); G2 `b` = x.c1||x.c0||y.c1||y.c0.
*/
export interface Groth16Proof {
a: Buffer;
b: Buffer;
c: Buffer;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export interface Client {
/**
* Construct and simulate a init transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
* One-time wiring: who can re-point the verifier, and the verifier's
* contract address. Panics on a second call.
*/
init: ({admin, verifier}: {admin: string, verifier: string}, options?: MethodOptions) => Promise<AssembledTransaction<null>>
/**
* Construct and simulate a verifier transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
* The verifier contract this validator delegates proof-checking to.
*/
verifier: (options?: MethodOptions) => Promise<AssembledTransaction<Result<string>>>
/**
* Construct and simulate a get_passport transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
* Fetch the stored attestation for an agent, if any.
*/
get_passport: ({agent_id}: {agent_id: u256}, options?: MethodOptions) => Promise<AssembledTransaction<Option<Attestation>>>
/**
* Construct and simulate a set_verifier transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
* Admin-only: re-point to a new verifier (e.g. after a circuit upgrade).
*/
set_verifier: ({verifier}: {verifier: string}, options?: MethodOptions) => Promise<AssembledTransaction<Result<void>>>
/**
* Construct and simulate a is_registered transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
* True iff `agent_id` holds a minted zk-passport.
*/
is_registered: ({agent_id}: {agent_id: u256}, options?: MethodOptions) => Promise<AssembledTransaction<boolean>>
/**
* Construct and simulate a is_nullifier_used transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
* True iff this nullifier has already been spent.
*/
is_nullifier_used: ({nullifier}: {nullifier: u256}, options?: MethodOptions) => Promise<AssembledTransaction<boolean>>
/**
* Construct and simulate a verify_and_register transaction. Returns an `AssembledTransaction` object which will have a `result` field containing the result of the simulation. If this transaction changes contract state, you will need to call `signAndSend()` on the returned object.
* Verify a passport proof and, if sound and unspent, mint the attestation.
*
* This is the load-bearing entry point: the proof *is* the authorization,
* so no `require_auth` is needed — anyone relaying a valid, fresh proof
* registers the agent. Returns the freshly stored [`Attestation`].
*/
verify_and_register: ({proof, public_inputs}: {proof: Groth16Proof, public_inputs: Array<u256>}, options?: MethodOptions) => Promise<AssembledTransaction<Result<Attestation>>>
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class Client extends ContractClient {
static async deploy<T = Client>(
/** Options for initializing a Client as well as for calling a method, with extras specific to deploying. */
options: MethodOptions &
Omit<ContractClientOptions, "contractId"> & {
/** The hash of the Wasm blob, which must already be installed on-chain. */
wasmHash: Buffer | string;
/** Salt used to generate the contract's ID. Passed through to {@link Operation.createCustomContract}. Default: random. */
salt?: Buffer | Uint8Array;
/** The format used to decode `wasmHash`, if it's provided as a string. */
format?: "hex" | "base64";
}
): Promise<AssembledTransaction<T>> {
return ContractClient.deploy(null, options)
}
constructor(public readonly options: ContractClientOptions) {
super(
new ContractSpec([ "AAAABAAAAAAAAAAAAAAABUVycm9yAAAAAAAABQAAAAAAAAAOTm90SW5pdGlhbGl6ZWQAAAAAAAEAAAAAAAAAEkFscmVhZHlJbml0aWFsaXplZAAAAAAAAgAAAD1Xcm9uZyBudW1iZXIgb2YgcHVibGljIGlucHV0cyBmb3IgdGhlIGFnZW50X3Bhc3Nwb3J0IGNpcmN1aXQuAAAAAAAAD0JhZFB1YmxpY0lucHV0cwAAAAADAAAAPFRoaXMgbnVsbGlmaWVyIHdhcyBhbHJlYWR5IHNwZW50IOKAlCByZXBsYXkgLyBTeWJpbCBhdHRlbXB0LgAAAA1OdWxsaWZpZXJVc2VkAAAAAAAABAAAADpUaGUgR3JvdGgxNiBwcm9vZiBkaWQgbm90IHZlcmlmeSBhZ2FpbnN0IHRoZSBlbWJlZGRlZCBrZXkuAAAAAAAMSW52YWxpZFByb29mAAAABQ==",
"AAAAAQAAAAAAAAAAAAAAC0F0dGVzdGF0aW9uAAAAAAUAAAAAAAAACGFnZW50X2lkAAAADAAAADFMZWRnZXIgc2VxdWVuY2UgYXQgd2hpY2ggdGhlIHBhc3Nwb3J0IHdhcyBtaW50ZWQuAAAAAAAABmxlZGdlcgAAAAAABAAAAAAAAAAJbnVsbGlmaWVyAAAAAAAADAAAAAAAAAANcmVnaXN0cnlfcm9vdAAAAAAAAAwAAAAAAAAACXNwZW5kX2NhcAAAAAAA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iBhZnRlciBhIGNpcmN1aXQgdXBncmFkZSkuAAAAAAAMc2V0X3ZlcmlmaWVyAAAAAQAAAAAAAAAIdmVyaWZpZXIAAAATAAAAAQAAA+kAAAACAAAAAw==",
"AAAAAAAAAC9UcnVlIGlmZiBgYWdlbnRfaWRgIGhvbGRzIGEgbWludGVkIHprLXBhc3Nwb3J0LgAAAAANaXNfcmVnaXN0ZXJlZAAAAAAAAAEAAAAAAAAACGFnZW50X2lkAAAADAAAAAEAAAAB",
"AAAAAAAAAC9UcnVlIGlmZiB0aGlzIG51bGxpZmllciBoYXMgYWxyZWFkeSBiZWVuIHNwZW50LgAAAAARaXNfbnVsbGlmaWVyX3VzZWQAAAAAAAABAAAAAAAAAAludWxsaWZpZXIAAAAAAAAMAAAAAQAAAAE=",
"AAAAAAAAARpWZXJpZnkgYSBwYXNzcG9ydCBwcm9vZiBhbmQsIGlmIHNvdW5kIGFuZCB1bnNwZW50LCBtaW50IHRoZSBhdHRlc3RhdGlvbi4KClRoaXMgaXMgdGhlIGxvYWQtYmVhcmluZyBlbnRyeSBwb2ludDogdGhlIHByb29mICppcyogdGhlIGF1dGhvcml6YXRpb24sCnNvIG5vIGByZXF1aXJlX2F1dGhgIGlzIG5lZWRlZCDigJQgYW55b25lIHJlbGF5aW5nIGEgdmFsaWQsIGZyZXNoIHByb29mCnJlZ2lzdGVycyB0aGUgYWdlbnQuIFJldHVybnMgdGhlIGZyZXNobHkgc3RvcmVkIFtgQXR0ZXN0YXRpb25gXS4AAAAAABN2ZXJpZnlfYW5kX3JlZ2lzdGVyAAAAAAIAAAAAAAAABXByb29mAAAAAAAH0AAAAAxHcm90aDE2UHJvb2YAAAAAAAAADXB1YmxpY19pbnB1dHMAAAAAAAPqAAAADAAAAAEAAAPpAAAH0AAAAAtBdHRlc3RhdGlvbgAAAAAD" ]),
options
)
}
public readonly fromJSON = {
init: this.txFromJSON<null>,
verifier: this.txFromJSON<Result<string>>,
get_passport: this.txFromJSON<Option<Attestation>>,
set_verifier: this.txFromJSON<Result<void>>,
is_registered: this.txFromJSON<boolean>,
is_nullifier_used: this.txFromJSON<boolean>,
verify_and_register: this.txFromJSON<Result<Attestation>>
}
}