From 999e228046261ed8a230902d73b66b48e373c4b8 Mon Sep 17 00:00:00 2001 From: Aaron DeRuvo Date: Fri, 29 Sep 2023 11:47:36 +0200 Subject: [PATCH] prettify the docs for consistency --- .../blinding/reactNativeBlindingClient.ts | 2 +- docs/examples/blinding/webBlindingClient.ts | 4 +- docs/examples/constants.ts | 35 +-- docs/examples/contractKit.ts | 114 ++++---- docs/examples/ethers.ts | 114 ++++---- docs/examples/web3.ts | 275 +++++++++--------- 6 files changed, 255 insertions(+), 289 deletions(-) diff --git a/docs/examples/blinding/reactNativeBlindingClient.ts b/docs/examples/blinding/reactNativeBlindingClient.ts index 95672102a..5c822427d 100644 --- a/docs/examples/blinding/reactNativeBlindingClient.ts +++ b/docs/examples/blinding/reactNativeBlindingClient.ts @@ -40,4 +40,4 @@ export class ReactNativeBlsBlindingClient implements BlsBlindingClient { } return Buffer.from(byteBuffer).toString('base64') } -} \ No newline at end of file +} diff --git a/docs/examples/blinding/webBlindingClient.ts b/docs/examples/blinding/webBlindingClient.ts index ffdc6f876..5a39df67e 100644 --- a/docs/examples/blinding/webBlindingClient.ts +++ b/docs/examples/blinding/webBlindingClient.ts @@ -17,8 +17,8 @@ export class WebBlsBlindingClient implements BlsBlindingClient { this.init() } - async init(){ - await thresholdBls.init("/blind_threshold_bls_bg.wasm") + async init() { + await thresholdBls.init('/blind_threshold_bls_bg.wasm') } async blindMessage(base64PhoneNumber: string, seed?: Buffer): Promise { diff --git a/docs/examples/constants.ts b/docs/examples/constants.ts index bb19ddcd1..0fa0600f8 100644 --- a/docs/examples/constants.ts +++ b/docs/examples/constants.ts @@ -1,21 +1,18 @@ -export const FA_CONTRACT = require("./abis/FederatedAttestations.json"); -export const FA_PROXY_CONTRACT = require("./abis/FederatedAttestationsProxy.json"); -export const REGISTRY_CONTRACT = require("./abis/Registry.json"); -export const ESCROW_PROXY_CONTRACT = require("./abis/EscrowProxy.json"); -export const ESCROW_CONTRACT = require("./abis/Escrow.json"); -export const ODIS_PAYMENTS_CONTRACT = require("./abis/OdisPayments.json"); -export const STABLE_TOKEN_CONTRACT = require("./abis/StableToken.json"); -export const ACCOUNTS_CONTRACT = require("./abis/Accounts.json") +export const FA_CONTRACT = require('./abis/FederatedAttestations.json') +export const FA_PROXY_CONTRACT = require('./abis/FederatedAttestationsProxy.json') +export const REGISTRY_CONTRACT = require('./abis/Registry.json') +export const ESCROW_PROXY_CONTRACT = require('./abis/EscrowProxy.json') +export const ESCROW_CONTRACT = require('./abis/Escrow.json') +export const ODIS_PAYMENTS_CONTRACT = require('./abis/OdisPayments.json') +export const STABLE_TOKEN_CONTRACT = require('./abis/StableToken.json') +export const ACCOUNTS_CONTRACT = require('./abis/Accounts.json') -export const ALFAJORES_RPC = "https://alfajores-forno.celo-testnet.org"; -export const ALFAJORES_ACCOUNT = "0x48522303E536B4299B57e2cC2F0f5fe85f8C316F"; +export const ALFAJORES_RPC = 'https://alfajores-forno.celo-testnet.org' +export const ALFAJORES_ACCOUNT = '0x48522303E536B4299B57e2cC2F0f5fe85f8C316F' export const ALFAJORES_ACCOUNT_PK = - "726e53db4f0a79dfd63f58b19874896fce3748fcb80874665e0c147369c04a37"; -export const FA_PROXY_ADDRESS = "0x70F9314aF173c246669cFb0EEe79F9Cfd9C34ee3"; -export const ESCROW_PROXY_ADDRESS = - "0xb07E10c5837c282209c6B9B3DE0eDBeF16319a37"; -export const ODIS_PAYMENTS_PROXY_ADDRESS = - "0x645170cdB6B5c1bc80847bb728dBa56C50a20a49"; -export const ALFAJORES_CUSD_ADDRESS = - "0x765DE816845861e75A25fCA122bb6898B8B1282a"; -export const ACCOUNTS_PROXY_ADDRESS = "0xed7f51A34B4e71fbE69B3091FcF879cD14bD73A9"; + '726e53db4f0a79dfd63f58b19874896fce3748fcb80874665e0c147369c04a37' +export const FA_PROXY_ADDRESS = '0x70F9314aF173c246669cFb0EEe79F9Cfd9C34ee3' +export const ESCROW_PROXY_ADDRESS = '0xb07E10c5837c282209c6B9B3DE0eDBeF16319a37' +export const ODIS_PAYMENTS_PROXY_ADDRESS = '0x645170cdB6B5c1bc80847bb728dBa56C50a20a49' +export const ALFAJORES_CUSD_ADDRESS = '0x765DE816845861e75A25fCA122bb6898B8B1282a' +export const ACCOUNTS_PROXY_ADDRESS = '0xed7f51A34B4e71fbE69B3091FcF879cD14bD73A9' diff --git a/docs/examples/contractKit.ts b/docs/examples/contractKit.ts index 1339df7a9..4cb870d3a 100644 --- a/docs/examples/contractKit.ts +++ b/docs/examples/contractKit.ts @@ -1,38 +1,33 @@ -import { OdisUtils } from "@celo/identity"; -import { - AuthSigner, - OdisContextName, - ServiceContext, -} from "@celo/identity/lib/odis/query"; -import { ContractKit, newKit } from "@celo/contractkit"; -import { Account } from "web3-core"; - -const ALFAJORES_RPC = "https://alfajores-forno.celo-testnet.org"; -const ISSUER_PRIVATE_KEY = - "0x199abda8320f5af0bb51429d246a4e537d1c85fbfaa30d52f9b34df381bd3a95"; +import { OdisUtils } from '@celo/identity' +import { AuthSigner, OdisContextName, ServiceContext } from '@celo/identity/lib/odis/query' +import { ContractKit, newKit } from '@celo/contractkit' +import { Account } from 'web3-core' + +const ALFAJORES_RPC = 'https://alfajores-forno.celo-testnet.org' +const ISSUER_PRIVATE_KEY = '0x199abda8320f5af0bb51429d246a4e537d1c85fbfaa30d52f9b34df381bd3a95' class ASv2 { - kit: ContractKit; - issuer: Account; - authSigner: AuthSigner; + kit: ContractKit + issuer: Account + authSigner: AuthSigner serviceContext: ServiceContext constructor(kit: ContractKit) { - this.kit = kit; - this.issuer = kit.web3.eth.accounts.privateKeyToAccount(ISSUER_PRIVATE_KEY); - this.kit.addAccount(ISSUER_PRIVATE_KEY); - this.kit.defaultAccount = this.issuer.address; - this.serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES); + this.kit = kit + this.issuer = kit.web3.eth.accounts.privateKeyToAccount(ISSUER_PRIVATE_KEY) + this.kit.addAccount(ISSUER_PRIVATE_KEY) + this.kit.defaultAccount = this.issuer.address + this.serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES) this.authSigner = { authenticationMethod: OdisUtils.Query.AuthenticationMethod.WALLET_KEY, contractKit: this.kit, - }; + } } async registerAttestation(phoneNumber: string, account: string, attestationIssuedTime: number) { - await this.checkAndTopUpODISQuota(); + await this.checkAndTopUpODISQuota() // get identifier from phone number using ODIS - const {obfuscatedIdentifier} = await OdisUtils.Identifier.getObfuscatedIdentifier( + const { obfuscatedIdentifier } = await OdisUtils.Identifier.getObfuscatedIdentifier( phoneNumber, OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER, this.issuer.address, @@ -40,18 +35,17 @@ class ASv2 { this.serviceContext ) - const federatedAttestationsContract = - await this.kit.contracts.getFederatedAttestations(); + const federatedAttestationsContract = await this.kit.contracts.getFederatedAttestations() // upload identifier <-> address mapping to onchain registry await federatedAttestationsContract .registerAttestationAsIssuer(obfuscatedIdentifier, account, attestationIssuedTime) - .send(); + .send() } async lookupAddresses(phoneNumber: string) { // get identifier from phone number using ODIS - const {obfuscatedIdentifier} = await OdisUtils.Identifier.getObfuscatedIdentifier( + const { obfuscatedIdentifier } = await OdisUtils.Identifier.getObfuscatedIdentifier( phoneNumber, OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER, this.issuer.address, @@ -59,16 +53,15 @@ class ASv2 { this.serviceContext ) - const federatedAttestationsContract = - await this.kit.contracts.getFederatedAttestations(); + const federatedAttestationsContract = await this.kit.contracts.getFederatedAttestations() // query on-chain mappings const attestations = await federatedAttestationsContract.lookupAttestations( obfuscatedIdentifier, [this.issuer.address] - ); + ) - return attestations.accounts; + return attestations.accounts } private async checkAndTopUpODISQuota() { @@ -77,62 +70,59 @@ class ASv2 { this.issuer.address, this.authSigner, this.serviceContext - ); + ) - console.log("remaining ODIS quota", remainingQuota); + console.log('remaining ODIS quota', remainingQuota) if (remainingQuota < 1) { - const stableTokenContract = await this.kit.contracts.getStableToken(); - const odisPaymentsContract = await this.kit.contracts.getOdisPayments(); + const stableTokenContract = await this.kit.contracts.getStableToken() + const odisPaymentsContract = await this.kit.contracts.getOdisPayments() // give odis payment contract permission to use cUSD const currentAllowance = await stableTokenContract.allowance( this.issuer.address, odisPaymentsContract.address - ); - console.log("current allowance:", currentAllowance.toString()); - let enoughAllowance: boolean = false; + ) + console.log('current allowance:', currentAllowance.toString()) + let enoughAllowance: boolean = false - const ONE_CENT_CUSD_WEI = this.kit.web3.utils.toWei("0.01", "ether"); + const ONE_CENT_CUSD_WEI = this.kit.web3.utils.toWei('0.01', 'ether') if (currentAllowance.lt(ONE_CENT_CUSD_WEI)) { const approvalTxReceipt = await stableTokenContract - .increaseAllowance( - odisPaymentsContract.address, - ONE_CENT_CUSD_WEI - ) - .sendAndWaitForReceipt(); - console.log("approval status", approvalTxReceipt.status); - enoughAllowance = approvalTxReceipt.status; + .increaseAllowance(odisPaymentsContract.address, ONE_CENT_CUSD_WEI) + .sendAndWaitForReceipt() + console.log('approval status', approvalTxReceipt.status) + enoughAllowance = approvalTxReceipt.status } else { - enoughAllowance = true; + enoughAllowance = true } // increase quota if (enoughAllowance) { const odisPayment = await odisPaymentsContract .payInCUSD(this.issuer.address, ONE_CENT_CUSD_WEI) - .sendAndWaitForReceipt(); - console.log("odis payment tx status:", odisPayment.status); - console.log("odis payment tx hash:", odisPayment.transactionHash); + .sendAndWaitForReceipt() + console.log('odis payment tx status:', odisPayment.status) + console.log('odis payment tx hash:', odisPayment.transactionHash) } else { - throw "cUSD approval failed"; + throw 'cUSD approval failed' } } } } -(async () => { - const kit = await newKit(ALFAJORES_RPC); - const asv2 = new ASv2(kit); - const userAccount = "0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34"; - const userPhoneNumber = "+18009099999"; - const timeAttestationWasVerified = Math.floor(new Date().getTime() / 1000); - try{ - await asv2.registerAttestation(userPhoneNumber, userAccount, timeAttestationWasVerified) - console.log("attestation registered") +;(async () => { + const kit = await newKit(ALFAJORES_RPC) + const asv2 = new ASv2(kit) + const userAccount = '0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34' + const userPhoneNumber = '+18009099999' + const timeAttestationWasVerified = Math.floor(new Date().getTime() / 1000) + try { + await asv2.registerAttestation(userPhoneNumber, userAccount, timeAttestationWasVerified) + console.log('attestation registered') } catch (err) { // mostly likely reason registering would fail is if this issuer has already // registered a mapping between this number and account } - console.log(await asv2.lookupAddresses(userPhoneNumber)); -})(); + console.log(await asv2.lookupAddresses(userPhoneNumber)) +})() diff --git a/docs/examples/ethers.ts b/docs/examples/ethers.ts index e4b34df1d..515bf1d72 100644 --- a/docs/examples/ethers.ts +++ b/docs/examples/ethers.ts @@ -7,67 +7,56 @@ import { ODIS_PAYMENTS_CONTRACT, ODIS_PAYMENTS_PROXY_ADDRESS, STABLE_TOKEN_CONTRACT, -} from "./constants"; -import { OdisUtils } from "@celo/identity"; -import { - AuthenticationMethod, - AuthSigner, - OdisContextName, -} from "@celo/identity/lib/odis/query"; -import { ethers, Wallet } from "ethers"; +} from './constants' +import { OdisUtils } from '@celo/identity' +import { AuthenticationMethod, AuthSigner, OdisContextName } from '@celo/identity/lib/odis/query' +import { ethers, Wallet } from 'ethers' -const USER_ACCOUNT = "0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34"; -const USER_PHONE_NUMBER = "+18009099991"; +const USER_ACCOUNT = '0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34' +const USER_PHONE_NUMBER = '+18009099991' -const ISSUER_PRIVATE_KEY = - "0x726e53db4f0a79dfd63f58b19874896fce3748fcb80874665e0c147369c04a37"; -const DEK_PUBLIC_KEY = - "0x026063780c81991c032fb4fa7485c6607b7542e048ef85d08516fe5c4482360e4b"; -const DEK_PRIVATE_KEY = - "0xc2bbdabb440141efed205497a41d5fb6114e0435fd541e368dc628a8e086bfee"; +const ISSUER_PRIVATE_KEY = '0x726e53db4f0a79dfd63f58b19874896fce3748fcb80874665e0c147369c04a37' +const DEK_PUBLIC_KEY = '0x026063780c81991c032fb4fa7485c6607b7542e048ef85d08516fe5c4482360e4b' +const DEK_PRIVATE_KEY = '0xc2bbdabb440141efed205497a41d5fb6114e0435fd541e368dc628a8e086bfee' -const ALFAJORES_RPC = "https://alfajores-forno.celo-testnet.org"; +const ALFAJORES_RPC = 'https://alfajores-forno.celo-testnet.org' -const NOW_TIMESTAMP = Math.floor(new Date().getTime() / 1000); +const NOW_TIMESTAMP = Math.floor(new Date().getTime() / 1000) class ASv2 { - provider = new ethers.providers.JsonRpcProvider(ALFAJORES_RPC); - issuer = new Wallet(ISSUER_PRIVATE_KEY, this.provider); - serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES); + provider = new ethers.providers.JsonRpcProvider(ALFAJORES_RPC) + issuer = new Wallet(ISSUER_PRIVATE_KEY, this.provider) + serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES) authSigner: AuthSigner = { authenticationMethod: AuthenticationMethod.ENCRYPTION_KEY, rawKey: DEK_PRIVATE_KEY, - }; + } /** Contracts **/ - accountsContract = new ethers.Contract( - ACCOUNTS_PROXY_ADDRESS, - ACCOUNTS_CONTRACT.abi, - this.issuer - ); + accountsContract = new ethers.Contract(ACCOUNTS_PROXY_ADDRESS, ACCOUNTS_CONTRACT.abi, this.issuer) federatedAttestationsContract = new ethers.Contract( FA_PROXY_ADDRESS, FA_CONTRACT.abi, this.issuer - ); + ) odisPaymentsContract = new ethers.Contract( ODIS_PAYMENTS_PROXY_ADDRESS, ODIS_PAYMENTS_CONTRACT.abi, this.issuer - ); + ) stableTokenContract = new ethers.Contract( ALFAJORES_CUSD_ADDRESS, STABLE_TOKEN_CONTRACT.abi, this.issuer - ); + ) constructor() { - this.accountsContract.setAccountDataEncryptionKey(DEK_PUBLIC_KEY); + this.accountsContract.setAccountDataEncryptionKey(DEK_PUBLIC_KEY) } async registerAttestation(phoneNumber: string, account: string) { - await this.checkAndTopUpODISQuota(); + await this.checkAndTopUpODISQuota() // get identifier from phone number using ODIS const obfuscatedIdentifier = ( @@ -78,14 +67,14 @@ class ASv2 { this.authSigner, this.serviceContext ) - ).obfuscatedIdentifier; + ).obfuscatedIdentifier // upload identifier <-> address mapping to onchain registry await this.federatedAttestationsContract.registerAttestationAsIssuer( obfuscatedIdentifier, account, NOW_TIMESTAMP - ); + ) } async lookupAddresses(phoneNumber: string) { @@ -98,15 +87,15 @@ class ASv2 { this.authSigner, this.serviceContext ) - ).obfuscatedIdentifier; + ).obfuscatedIdentifier // query onchain mappings - const attestations = - await this.federatedAttestationsContract.lookupAttestations(obfuscatedIdentifier, [ - this.issuer.address, - ]); + const attestations = await this.federatedAttestationsContract.lookupAttestations( + obfuscatedIdentifier, + [this.issuer.address] + ) - return attestations.accounts; + return attestations.accounts } private async checkAndTopUpODISQuota() { @@ -115,55 +104,52 @@ class ASv2 { this.issuer.address, this.authSigner, this.serviceContext - ); + ) - console.log("remaining ODIS quota", remainingQuota); + console.log('remaining ODIS quota', remainingQuota) if (remainingQuota < 1) { // give odis payment contract permission to use cUSD const currentAllowance = await this.stableTokenContract.allowance( this.issuer.address, this.odisPaymentsContract.address - ); - console.log("current allowance:", currentAllowance.toString()); - let enoughAllowance: boolean = false; + ) + console.log('current allowance:', currentAllowance.toString()) + let enoughAllowance: boolean = false - const ONE_CENT_CUSD_WEI = ethers.utils.parseEther("0.01"); + const ONE_CENT_CUSD_WEI = ethers.utils.parseEther('0.01') if (ONE_CENT_CUSD_WEI.gt(currentAllowance)) { const approvalTxReceipt = await this.stableTokenContract - .increaseAllowance( - this.odisPaymentsContract.address, - ONE_CENT_CUSD_WEI - ) - .sendAndWaitForReceipt(); - console.log("approval status", approvalTxReceipt.status); - enoughAllowance = approvalTxReceipt.status; + .increaseAllowance(this.odisPaymentsContract.address, ONE_CENT_CUSD_WEI) + .sendAndWaitForReceipt() + console.log('approval status', approvalTxReceipt.status) + enoughAllowance = approvalTxReceipt.status } else { - enoughAllowance = true; + enoughAllowance = true } // increase quota if (enoughAllowance) { const odisPayment = await this.odisPaymentsContract .payInCUSD(this.issuer.address, ONE_CENT_CUSD_WEI) - .sendAndWaitForReceipt(); - console.log("odis payment tx status:", odisPayment.status); - console.log("odis payment tx hash:", odisPayment.transactionHash); + .sendAndWaitForReceipt() + console.log('odis payment tx status:', odisPayment.status) + console.log('odis payment tx hash:', odisPayment.transactionHash) } else { - throw "cUSD approval failed"; + throw 'cUSD approval failed' } } } } -(async () => { - const asv2 = new ASv2(); +;(async () => { + const asv2 = new ASv2() try { - await asv2.registerAttestation(USER_PHONE_NUMBER, USER_ACCOUNT); - console.log("attestation registered") + await asv2.registerAttestation(USER_PHONE_NUMBER, USER_ACCOUNT) + console.log('attestation registered') } catch (err) { // mostly likely reason registering would fail is if this issuer has already // registered a mapping between this number and account } - console.log(await asv2.lookupAddresses(USER_PHONE_NUMBER)); -})(); + console.log(await asv2.lookupAddresses(USER_PHONE_NUMBER)) +})() diff --git a/docs/examples/web3.ts b/docs/examples/web3.ts index e6b1adb93..ef282dbe2 100644 --- a/docs/examples/web3.ts +++ b/docs/examples/web3.ts @@ -1,154 +1,147 @@ import { - ACCOUNTS_CONTRACT, - ACCOUNTS_PROXY_ADDRESS, - ALFAJORES_CUSD_ADDRESS, - FA_CONTRACT, - FA_PROXY_ADDRESS, - ODIS_PAYMENTS_CONTRACT, - ODIS_PAYMENTS_PROXY_ADDRESS, - STABLE_TOKEN_CONTRACT, -} from "./constants"; -import Web3 from "web3"; + ACCOUNTS_CONTRACT, + ACCOUNTS_PROXY_ADDRESS, + ALFAJORES_CUSD_ADDRESS, + FA_CONTRACT, + FA_PROXY_ADDRESS, + ODIS_PAYMENTS_CONTRACT, + ODIS_PAYMENTS_PROXY_ADDRESS, + STABLE_TOKEN_CONTRACT, +} from './constants' +import Web3 from 'web3' import { OdisUtils } from '@celo/identity' -import { AuthSigner, OdisContextName } from "@celo/identity/lib/odis/query"; +import { AuthSigner, OdisContextName } from '@celo/identity/lib/odis/query' -const ISSUER_PRIVATE_KEY = "0x726e53db4f0a79dfd63f58b19874896fce3748fcb80874665e0c147369c04a37"; -const DEK_PUBLIC_KEY = "0x026063780c81991c032fb4fa7485c6607b7542e048ef85d08516fe5c4482360e4b"; -const DEK_PRIVATE_KEY = "0xc2bbdabb440141efed205497a41d5fb6114e0435fd541e368dc628a8e086bfee"; +const ISSUER_PRIVATE_KEY = '0x726e53db4f0a79dfd63f58b19874896fce3748fcb80874665e0c147369c04a37' +const DEK_PUBLIC_KEY = '0x026063780c81991c032fb4fa7485c6607b7542e048ef85d08516fe5c4482360e4b' +const DEK_PRIVATE_KEY = '0xc2bbdabb440141efed205497a41d5fb6114e0435fd541e368dc628a8e086bfee' -const ALFAJORES_RPC = "https://alfajores-forno.celo-testnet.org"; +const ALFAJORES_RPC = 'https://alfajores-forno.celo-testnet.org' class ASv2 { - web3 = new Web3(ALFAJORES_RPC); - issuer = this.web3.eth.accounts.privateKeyToAccount(ISSUER_PRIVATE_KEY); - authSigner: AuthSigner = { - authenticationMethod: OdisUtils.Query.AuthenticationMethod.ENCRYPTION_KEY, - rawKey: DEK_PRIVATE_KEY - } - serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES); - - /** Contracts **/ - accountsContract = new this.web3.eth.Contract( - ACCOUNTS_CONTRACT.abi, - ACCOUNTS_PROXY_ADDRESS - ); - federatedAttestationsContract = new this.web3.eth.Contract( - FA_CONTRACT.abi, - FA_PROXY_ADDRESS - ); - odisPaymentsContract = new this.web3.eth.Contract( - ODIS_PAYMENTS_CONTRACT.abi, - ODIS_PAYMENTS_PROXY_ADDRESS - ); - stableTokenContract = new this.web3.eth.Contract( - STABLE_TOKEN_CONTRACT.abi, - ALFAJORES_CUSD_ADDRESS - ); - - constructor(){ - this.web3.eth.accounts.wallet.add(this.issuer); - this.web3.eth.defaultAccount = this.issuer.address - this.accountsContract.methods.setAccountDataEncryptionKey(DEK_PUBLIC_KEY).send({from: this.issuer.address, gas: 50000}) - } + web3 = new Web3(ALFAJORES_RPC) + issuer = this.web3.eth.accounts.privateKeyToAccount(ISSUER_PRIVATE_KEY) + authSigner: AuthSigner = { + authenticationMethod: OdisUtils.Query.AuthenticationMethod.ENCRYPTION_KEY, + rawKey: DEK_PRIVATE_KEY, + } + serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES) - async registerAttestation(phoneNumber: string, account: string, attestationIssuedTime: number) { - await this.checkAndTopUpODISQuota(); - - // get identifier from phone number using ODIS - const identifier = (await OdisUtils.Identifier.getObfuscatedIdentifier( - phoneNumber, - OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER, - this.issuer.address, - this.authSigner, - this.serviceContext - )).obfuscatedIdentifier - - // upload identifier <-> address mapping to onchain registry - await this.federatedAttestationsContract.methods - .registerAttestationAsIssuer( - identifier, - account, - attestationIssuedTime - ) - .send({from: this.issuer.address, gas: 50000}); - } + /** Contracts **/ + accountsContract = new this.web3.eth.Contract(ACCOUNTS_CONTRACT.abi, ACCOUNTS_PROXY_ADDRESS) + federatedAttestationsContract = new this.web3.eth.Contract(FA_CONTRACT.abi, FA_PROXY_ADDRESS) + odisPaymentsContract = new this.web3.eth.Contract( + ODIS_PAYMENTS_CONTRACT.abi, + ODIS_PAYMENTS_PROXY_ADDRESS + ) + stableTokenContract = new this.web3.eth.Contract( + STABLE_TOKEN_CONTRACT.abi, + ALFAJORES_CUSD_ADDRESS + ) - async lookupAddresses(phoneNumber: string){ - // get identifier from phone number using ODIS - const identifier = (await OdisUtils.Identifier.getObfuscatedIdentifier( - phoneNumber, - OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER, - this.issuer.address, - this.authSigner, - this.serviceContext - )).obfuscatedIdentifier - - // query onchain mappings - const attestations = await this.federatedAttestationsContract.methods - .lookupAttestations(identifier, [this.issuer.address]) - .call(); - - return attestations.accounts - } + constructor() { + this.web3.eth.accounts.wallet.add(this.issuer) + this.web3.eth.defaultAccount = this.issuer.address + this.accountsContract.methods + .setAccountDataEncryptionKey(DEK_PUBLIC_KEY) + .send({ from: this.issuer.address, gas: 50000 }) + } + + async registerAttestation(phoneNumber: string, account: string, attestationIssuedTime: number) { + await this.checkAndTopUpODISQuota() + + // get identifier from phone number using ODIS + const identifier = ( + await OdisUtils.Identifier.getObfuscatedIdentifier( + phoneNumber, + OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER, + this.issuer.address, + this.authSigner, + this.serviceContext + ) + ).obfuscatedIdentifier + + // upload identifier <-> address mapping to onchain registry + await this.federatedAttestationsContract.methods + .registerAttestationAsIssuer(identifier, account, attestationIssuedTime) + .send({ from: this.issuer.address, gas: 50000 }) + } + + async lookupAddresses(phoneNumber: string) { + // get identifier from phone number using ODIS + const identifier = ( + await OdisUtils.Identifier.getObfuscatedIdentifier( + phoneNumber, + OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER, + this.issuer.address, + this.authSigner, + this.serviceContext + ) + ).obfuscatedIdentifier - private async checkAndTopUpODISQuota() { - //check remaining quota - const { remainingQuota } = await OdisUtils.Quota.getPnpQuotaStatus( - this.issuer.address, - this.authSigner, - this.serviceContext - ); - - console.log("remaining ODIS quota", remainingQuota); - if (remainingQuota < 1) { - // give odis payment contract permission to use cUSD - const currentAllowance = await this.stableTokenContract.methods.allowance( - this.issuer.address, - this.odisPaymentsContract.options.address - ); - console.log("current allowance:", currentAllowance.toString()); - let enoughAllowance: boolean = false; - - const ONE_CENT_CUSD_WEI = this.web3.utils.toWei("0.01", "ether"); - - if (currentAllowance < ONE_CENT_CUSD_WEI) { - const approvalTxReceipt = await this.stableTokenContract.methods - .increaseAllowance( - this.odisPaymentsContract.options.address, - ONE_CENT_CUSD_WEI - ) - .sendAndWaitForReceipt(); - console.log("approval status", approvalTxReceipt.status); - enoughAllowance = approvalTxReceipt.status; - } else { - enoughAllowance = true; - } - - // increase quota - if (enoughAllowance) { - const odisPayment = await this.odisPaymentsContract.methods - .payInCUSD(this.issuer.address, ONE_CENT_CUSD_WEI) - .sendAndWaitForReceipt(); - console.log("odis payment tx status:", odisPayment.status); - console.log("odis payment tx hash:", odisPayment.transactionHash); - } else { - throw "cUSD approval failed"; - } - } + // query onchain mappings + const attestations = await this.federatedAttestationsContract.methods + .lookupAttestations(identifier, [this.issuer.address]) + .call() + + return attestations.accounts + } + + private async checkAndTopUpODISQuota() { + //check remaining quota + const { remainingQuota } = await OdisUtils.Quota.getPnpQuotaStatus( + this.issuer.address, + this.authSigner, + this.serviceContext + ) + + console.log('remaining ODIS quota', remainingQuota) + if (remainingQuota < 1) { + // give odis payment contract permission to use cUSD + const currentAllowance = await this.stableTokenContract.methods.allowance( + this.issuer.address, + this.odisPaymentsContract.options.address + ) + console.log('current allowance:', currentAllowance.toString()) + let enoughAllowance: boolean = false + + const ONE_CENT_CUSD_WEI = this.web3.utils.toWei('0.01', 'ether') + + if (currentAllowance < ONE_CENT_CUSD_WEI) { + const approvalTxReceipt = await this.stableTokenContract.methods + .increaseAllowance(this.odisPaymentsContract.options.address, ONE_CENT_CUSD_WEI) + .sendAndWaitForReceipt() + console.log('approval status', approvalTxReceipt.status) + enoughAllowance = approvalTxReceipt.status + } else { + enoughAllowance = true + } + + // increase quota + if (enoughAllowance) { + const odisPayment = await this.odisPaymentsContract.methods + .payInCUSD(this.issuer.address, ONE_CENT_CUSD_WEI) + .sendAndWaitForReceipt() + console.log('odis payment tx status:', odisPayment.status) + console.log('odis payment tx hash:', odisPayment.transactionHash) + } else { + throw 'cUSD approval failed' + } } + } } -(async () => { - const asv2 = new ASv2() - const userAccount = "0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34"; - const userPhoneNumber = "+18009099999"; - const timeAttestationWasVerified = Math.floor(new Date().getTime() / 1000); - try{ - await asv2.registerAttestation(userPhoneNumber, userAccount, timeAttestationWasVerified) - console.log("attestation registered") - } catch(err){ - // mostly likely reason registering would fail is if this issuer has already - // registered a mapping between this number and account - } - console.log(await asv2.lookupAddresses(userPhoneNumber)) +;(async () => { + const asv2 = new ASv2() + const userAccount = '0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34' + const userPhoneNumber = '+18009099999' + const timeAttestationWasVerified = Math.floor(new Date().getTime() / 1000) + try { + await asv2.registerAttestation(userPhoneNumber, userAccount, timeAttestationWasVerified) + console.log('attestation registered') + } catch (err) { + // mostly likely reason registering would fail is if this issuer has already + // registered a mapping between this number and account + } + console.log(await asv2.lookupAddresses(userPhoneNumber)) })()