-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.tsx
321 lines (287 loc) · 11.6 KB
/
index.tsx
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { ContractKit, newKit } from "@celo/contractkit";
import { BigNumber } from "bignumber.js";
import { useEffect, useState } from "react";
import { toast } from "../components";
import { OdisUtils } from "@celo/identity";
import WebBlsBlindingClient from "../components/bls-blinding-client";
import { useCelo } from "@celo/react-celo";
import "@celo/react-celo/lib/styles.css";
import { E164_REGEX } from "../services/twilio";
import { Account } from "web3-core";
import { AuthSigner } from "@celo/identity/lib/odis/query";
import { FederatedAttestationsWrapper } from "@celo/contractkit/lib/wrappers/FederatedAttestations";
import { OdisPaymentsWrapper } from "@celo/contractkit/lib/wrappers/OdisPayments";
import RegisterNumberModal from "./registerNumber";
import SendToNumberModal from "./sendToNumber";
import DeregisterNumberModal from "./deregisterNumber";
function App() {
const { kit, connect, address, destroy } = useCelo();
const ISSUER_PRIVATE_KEY = process.env.NEXT_PUBLIC_ISSUER_PRIVATE_KEY;
let issuerKit: ContractKit,
issuer: Account,
federatedAttestationsContract: FederatedAttestationsWrapper,
odisPaymentContract: OdisPaymentsWrapper;
const [isRegisterNumberModalOpen, setIsRegisterNumberModalOpen] =
useState(false);
const [isSendToNumberModalOpen, setIsSendToNumberModalOpen] = useState(false);
const [isDeregisterNumberModalOpen, setIsDeregisterNumberModalOpen] =
useState(false);
useEffect(() => {
const intializeIssuer = async () => {
issuerKit = newKit("https://alfajores-forno.celo-testnet.org");
issuer =
issuerKit.web3.eth.accounts.privateKeyToAccount(ISSUER_PRIVATE_KEY);
issuerKit.addAccount(ISSUER_PRIVATE_KEY);
issuerKit.defaultAccount = issuer.address;
federatedAttestationsContract =
await issuerKit.contracts.getFederatedAttestations();
odisPaymentContract = await issuerKit.contracts.getOdisPayments();
};
intializeIssuer();
});
async function getIdentifier(phoneNumber: string) {
try {
if (!E164_REGEX.test(phoneNumber)) {
throw "Attempting to hash a non-e164 number: " + phoneNumber;
}
const ONE_CENT_CUSD = issuerKit.web3.utils.toWei("0.01", "ether");
let authMethod: any = OdisUtils.Query.AuthenticationMethod.WALLET_KEY;
const authSigner: AuthSigner = {
authenticationMethod: authMethod,
//@ts-ignore typing issue
contractKit: issuerKit,
};
const serviceContext = OdisUtils.Query.ODIS_ALFAJORES_CONTEXT;
//check remaining quota
const { remainingQuota } = await OdisUtils.Quota.getPnpQuotaStatus(
issuer.address,
authSigner,
serviceContext
);
//increase quota if needed.
console.log("remaining ODIS quota", remainingQuota);
if (remainingQuota < 1) {
// give odis payment contract permission to use cUSD
const cusd = await issuerKit.contracts.getStableToken();
const currrentAllowance = await cusd.allowance(
issuer.address,
odisPaymentContract.address
);
console.log("current allowance:", currrentAllowance.toString());
let enoughAllowance: boolean = false;
if (currrentAllowance < BigNumber(ONE_CENT_CUSD)) {
const approvalTxReceipt = await cusd
.increaseAllowance(odisPaymentContract.address, ONE_CENT_CUSD)
.sendAndWaitForReceipt();
console.log("approval status", approvalTxReceipt.status);
enoughAllowance = approvalTxReceipt.status;
} else {
enoughAllowance = true;
}
// increase quota
if (enoughAllowance) {
const odisPayment = await odisPaymentContract
.payInCUSD(issuer.address, ONE_CENT_CUSD)
.sendAndWaitForReceipt();
console.log("odis payment tx status:", odisPayment.status);
console.log("odis payment tx hash:", odisPayment.transactionHash);
} else {
throw "cUSD approval failed";
}
}
const blindingClient = new WebBlsBlindingClient(
serviceContext.odisPubKey
);
await blindingClient.init();
console.log("fetching identifier for:", phoneNumber);
const response =
await OdisUtils.PhoneNumberIdentifier.getPhoneNumberIdentifier(
phoneNumber,
issuer.address,
authSigner,
serviceContext,
undefined,
undefined,
blindingClient
);
console.log(`Obfuscated phone number: ${response.phoneHash}`);
console.log(
`Obfuscated phone number is a result of: sha3('tel://${response.e164Number}__${response.pepper}') => ${response.phoneHash}`
);
return response.phoneHash;
} catch (error) {
throw `failed to get identifier: ${error}`;
}
}
// this function needs to be called once when using a new issuer address
async function registerIssuerAccountAndWallet() {
if (issuer.address == undefined) {
throw "issuer not found";
}
const accountsContract = await issuerKit.contracts.getAccounts();
// register account if needed
let registeredAccount = await accountsContract.isAccount(issuer.address);
if (!registeredAccount) {
console.log("Registering account");
const receipt = await accountsContract
.createAccount()
.sendAndWaitForReceipt({ from: issuer.address });
console.log("Receipt status: ", receipt.status);
} else {
console.log("Account already registered");
}
// register wallet if needed
let registeredWalletAddress = await accountsContract.getWalletAddress(
issuer.address
);
console.log("Wallet address: ", registeredWalletAddress);
if (
registeredWalletAddress == "0x0000000000000000000000000000000000000000"
) {
console.log(
`Setting account's wallet address in Accounts.sol to ${issuer.address}`
);
const setWalletTx = await accountsContract
.setWalletAddress(issuer.address)
.sendAndWaitForReceipt();
console.log("Receipt status: ", setWalletTx.status);
} else {
console.log("Account's wallet already registered");
}
}
async function registerNumber(number: string) {
try {
const verificationTime = Math.floor(new Date().getTime() / 1000);
const identifier = await getIdentifier(number);
console.log(identifier);
// TODO: lookup list of issuers per phone number.
// This could be a good example to have for potential issuers to learn about this feature.
const { accounts } =
await federatedAttestationsContract.lookupAttestations(identifier, [
issuer.address,
]);
console.log(accounts);
if (accounts.length == 0) {
const attestationReceipt = await federatedAttestationsContract
.registerAttestationAsIssuer(identifier, address, verificationTime)
.sendAndWaitForReceipt();
console.log("attestation Receipt status:", attestationReceipt.status);
console.log(
`Register Attestation as issuer TX hash: https://explorer.celo.org/alfajores/tx/${attestationReceipt.transactionHash}/internal-transactions`
);
} else {
console.log("phone number already registered with this issuer");
}
} catch (error) {
throw `Error registering phone number: ${error}`;
}
}
async function sendToNumber(number: string, amount: string) {
try {
const identifier = await getIdentifier(number);
const amountInWei = issuerKit.web3.utils.toWei(amount, "ether");
const attestations =
await federatedAttestationsContract.lookupAttestations(identifier, [
issuer.address,
]);
// TODO: handle when no accounts mapped to number
const CELO = await kit.contracts.getGoldToken();
await CELO.transfer(
attestations.accounts[0],
amountInWei
).sendAndWaitForReceipt({ gasPrice: 20000000000 });
} catch (error) {
throw `Failed to send funds to ${number}: ${error}`;
}
}
async function deregisterPhoneNumber(phoneNumber: string) {
try {
const identifier = await getIdentifier(phoneNumber);
const receipt = await federatedAttestationsContract
.revokeAttestation(identifier, issuer.address, address)
.sendAndWaitForReceipt();
console.log(
`revoke attestation transaction receipt status: ${receipt.status}`
);
} catch (error) {
throw `Failed to deregister phone number: ${error}`;
}
}
return (
<main>
<br />
{!address ? (
<button
onClick={() =>
connect().catch((e) => toast.error((e as Error).message))
}
className="text-celo-green font-medium py-2 mb-2"
>
Connect your wallet
</button>
) : (
<div className="flex flex-col mx-auto content-center">
<p className="">Connected Address: {address}</p>
<button
onClick={destroy}
className="text-celo-green font-medium py-2 mb-2 self-center"
>
Disconnect your wallet
</button>
<div className="my-5 sm:mt-0">
<div className="overflow-hidden shadow sm:rounded-md">
<div className="bg-gray-50 px-4 py-5 text-center sm:px-6">
<button
className="mr-3 inline-flex justify-center rounded-md border border-transparent bg-celo-green py-2 px-4 text-sm font-medium text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-celo-green focus:ring-offset-2"
onClick={() => setIsRegisterNumberModalOpen(true)}
>
Verify and register your phone number
</button>
</div>
</div>
</div>
<div className="my-5 sm:mt-0">
<div className="overflow-hidden shadow sm:rounded-md">
<div className="bg-gray-50 px-4 py-5 text-center sm:px-6">
<button
className="mr-3 inline-flex justify-center rounded-md border border-transparent bg-celo-green py-2 px-4 text-sm font-medium text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-celo-green focus:ring-offset-2"
onClick={() => setIsSendToNumberModalOpen(true)}
>
Send payment to a phone number
</button>
</div>
</div>
</div>
<div className="my-5 sm:mt-0">
<div className="overflow-hidden shadow sm:rounded-md">
<div className="bg-gray-50 px-4 py-5 text-center sm:px-6">
<button
className="mr-3 inline-flex justify-center rounded-md border border-transparent bg-celo-green py-2 px-4 text-sm font-medium text-white shadow-sm focus:outline-none focus:ring-2 focus:ring-celo-green focus:ring-offset-2"
onClick={() => setIsDeregisterNumberModalOpen(true)}
>
De-register your phone number
</button>
</div>
</div>
</div>
<RegisterNumberModal
isOpen={isRegisterNumberModalOpen}
onDismiss={() => setIsRegisterNumberModalOpen(false)}
registerNumber={registerNumber}
/>
<SendToNumberModal
isOpen={isSendToNumberModalOpen}
onDismiss={() => setIsSendToNumberModalOpen(false)}
sendToNumber={sendToNumber}
/>
<DeregisterNumberModal
isOpen={isDeregisterNumberModalOpen}
onDismiss={() => setIsDeregisterNumberModalOpen(false)}
deregisterNumber={deregisterPhoneNumber}
/>
</div>
)}
</main>
);
}
export default App;