Skip to content

Commit

Permalink
first final draft
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmgdr committed Sep 24, 2024
1 parent d012ef3 commit d2091b6
Show file tree
Hide file tree
Showing 24 changed files with 332 additions and 264 deletions.
3 changes: 1 addition & 2 deletions apps/combiner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
},
"dependencies": {
"@celo/base": "^6.0.0",
"@celo/contractkit": "^7.1.0",
"@celo/encrypted-backup": "^5.0.6",
"@celo/identity": "^5.1.2",
"@celo/phone-number-privacy-common": "^3.1.2",
Expand Down Expand Up @@ -56,7 +55,7 @@
"pg": "^8.2.1",
"prom-client": "12.0.0",
"uuid": "^7.0.3",
"viem": "^2.19.3"
"viem": "2.21.14"
},
"devDependencies": {
"@celo/phone-number-privacy-signer": "workspace:^",
Expand Down
8 changes: 2 additions & 6 deletions apps/combiner/src/common/web3/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { ErrorMessage, getDataEncryptionKey } from '@celo/phone-number-privacy-common'
import Logger from 'bunyan'
import { Address, PublicClient } from 'viem'
import { Address, Client } from 'viem'
import config from '../../config'
import { Counters, Histograms, newMeter } from '../metrics'

export async function getDEK(
client: PublicClient,
logger: Logger,
account: Address,
): Promise<string> {
export async function getDEK(client: Client, logger: Logger, account: Address): Promise<string> {
const _meter = newMeter(Histograms.fullNodeLatency, 'getDataEncryptionKey')
return _meter(() =>
getDataEncryptionKey(
Expand Down
4 changes: 2 additions & 2 deletions apps/combiner/src/pnp/services/account-services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ErrorMessage } from '@celo/phone-number-privacy-common'
import Logger from 'bunyan'
import { LRUCache } from 'lru-cache'
import { Address, PublicClient } from 'viem'
import { Address, Client } from 'viem'
import { OdisError, wrapError } from '../../common/error'
import { Counters } from '../../common/metrics'
import { traceAsyncFunction } from '../../common/tracing-utils'
Expand Down Expand Up @@ -47,7 +47,7 @@ export class CachingAccountService implements AccountService {
export class ContractKitAccountService implements AccountService {
constructor(
private readonly logger: Logger,
private readonly client: PublicClient,
private readonly client: Client,
) {}

async getAccount(address: Address): Promise<string> {
Expand Down
6 changes: 2 additions & 4 deletions apps/monitor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"dependencies": {
"@celo/base": "^6.0.0",
"@celo/contractkit": "^7.1.0",
"@celo/cryptographic-utils": "^5.0.7",
"@celo/encrypted-backup": "^5.0.6",
"@celo/identity": "^5.1.2",
Expand All @@ -33,9 +32,8 @@
"@celo/wallet-local": "^5.1.2",
"firebase-admin": "^11.11.0",
"firebase-functions": "^4.5.0",
"yargs": "^14.0.0",
"viem": "^2.19.3"

"viem": "2.21.14",
"yargs": "^14.0.0"
},
"devDependencies": {
"firebase-functions-test": "^3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion apps/monitor/src/scripts/run-load-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OdisContextName } from '@celo/identity/lib/odis/query'
import { CombinerEndpointPNP, rootLogger } from '@celo/phone-number-privacy-common'
import { Hex } from 'viem'
import yargs from 'yargs'
import { concurrentRPSLoadTest } from '../test'

Expand Down Expand Up @@ -91,7 +92,7 @@ yargs
args.bypassQuota,
args.useDEK,
args.movingAvgRequests,
args.privateKey,
args.privateKey as Hex,
args.privateKeyPercentage,
)
},
Expand Down
5 changes: 3 additions & 2 deletions apps/monitor/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ErrorMessages, OdisContextName } from '@celo/identity/lib/odis/query'
import { PnpClientQuotaStatus } from '@celo/identity/lib/odis/quota'
import { CombinerEndpointPNP, rootLogger } from '@celo/phone-number-privacy-common'
import { performance } from 'perf_hooks'
import { Hex } from 'viem'
import { queryOdisDomain, queryOdisForQuota, queryOdisForSalt } from './query'

const logger = rootLogger('odis-monitor')
Expand Down Expand Up @@ -48,7 +49,7 @@ export async function testPNPQuotaQuery(
blockchainProvider: string,
contextName: OdisContextName,
timeoutMs?: number,
privateKey?: string,
privateKey?: Hex,
privateKeyPercentage: number = 100,
) {
logger.info(`Performing test PNP query for ${CombinerEndpointPNP.PNP_QUOTA}`)
Expand Down Expand Up @@ -97,7 +98,7 @@ export async function concurrentRPSLoadTest(
bypassQuota: boolean = false,
useDEK: boolean = false,
movingAverageRequests: number = 50,
privateKey?: string,
privateKey?: Hex,
privateKeyPercentage: number = 100,
) {
const latencyQueue: number[] = []
Expand Down
3 changes: 1 addition & 2 deletions apps/signer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
},
"dependencies": {
"@celo/base": "^6.0.0",
"@celo/contractkit": "^7.1.0",
"@celo/phone-number-privacy-common": "^3.1.2",
"@celo/poprf": "^0.1.9",
"@celo/utils": "^6.0.0",
Expand Down Expand Up @@ -72,7 +71,7 @@
"pg": "^8.2.1",
"prom-client": "12.0.0",
"promise.allsettled": "^1.0.2",
"viem": "^2.19.3"
"viem": "2.21.14"
},
"devDependencies": {
"@types/express": "^4.17.20",
Expand Down
6 changes: 3 additions & 3 deletions apps/signer/src/common/web3/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { retryAsyncWithBackOffAndTimeout } from '@celo/base'
import { getDataEncryptionKey, getOdisPaymentsContract } from '@celo/phone-number-privacy-common'
import { BigNumber } from 'bignumber.js'
import Logger from 'bunyan'
import { Address, Hex, PublicClient } from 'viem'
import { Address, Client, Hex } from 'viem'
import { config } from '../../config'
import { Counters, Histograms, newMeter } from '../metrics'

export async function getOnChainOdisPayments(
client: PublicClient,
client: Client,
logger: Logger,
account: Address,
): Promise<BigNumber> {
Expand All @@ -32,7 +32,7 @@ export async function getOnChainOdisPayments(
)
}

export async function getDEK(client: PublicClient, logger: Logger, account: Address): Promise<Hex> {
export async function getDEK(client: Client, logger: Logger, account: Address): Promise<Hex> {
const _meter = newMeter(Histograms.fullNodeLatency, 'getDataEncryptionKey')
return _meter(() =>
getDataEncryptionKey(
Expand Down
4 changes: 2 additions & 2 deletions apps/signer/src/pnp/services/account-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ErrorMessage } from '@celo/phone-number-privacy-common'
import BigNumber from 'bignumber.js'
import Logger from 'bunyan'
import { LRUCache } from 'lru-cache'
import { Address, PublicClient } from 'viem'
import { Address, WalletClient } from 'viem'
import { OdisError, wrapError } from '../../common/error'
import { traceAsyncFunction } from '../../common/tracing-utils'
import { getDEK, getOnChainOdisPayments } from '../../common/web3/contracts'
Expand Down Expand Up @@ -56,7 +56,7 @@ export class CachingAccountService implements AccountService {
export class ClientAccountService implements AccountService {
constructor(
private readonly logger: Logger,
private readonly client: PublicClient,
private readonly client: WalletClient,
) {}

async getAccount(address: Address): Promise<PnpAccount> {
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { OdisUtils } from '@celo/identity'
import { AuthenticationMethod, AuthSigner, OdisContextName } from '@celo/identity/lib/odis/query'
import { ethers, Wallet } from 'ethers'
import {
ACCOUNTS_CONTRACT,
ACCOUNTS_PROXY_ADDRESS,
Expand All @@ -8,9 +11,6 @@ import {
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'

const USER_ACCOUNT = '0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34'
const USER_PHONE_NUMBER = '+18009099991'
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^16.8.1",
"react-native": ">=0.60.0-rc.0 <1.0.x",
"react-native-blind-threshold-bls": "^1.0.1",
"viem": "^2.19.4",
"viem": "2.21.14",
"web3": "^1.8.0",
"web3-utils": "^1.8.0"
},
Expand Down
91 changes: 65 additions & 26 deletions docs/examples/viem.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { federatedAttestationsABI } from '@celo/abis'
import { federatedAttestationsABI, odisPaymentsABI, stableTokenABI } from '@celo/abis'
import { OdisUtils } from '@celo/identity'
import { AuthSigner, OdisContextName, ServiceContext } from '@celo/identity/lib/odis/query'
import {
Address,
createPublicClient,
createWalletClient,
getContract,
Hex,
http,
parseEther,
PrivateKeyAccount,
PublicClient,
Transport,
WalletClient,
} from 'viem'
import { readContract } from 'viem/_types/actions/public/readContract'
import { simulateContract } from 'viem/_types/actions/public/simulateContract'
import { writeContract } from 'viem/_types/actions/wallet/writeContract'
import { privateKeyToAccount } from 'viem/accounts'
import { celoAlfajores } from 'viem/chains'
import {
ALFAJORES_CUSD_ADDRESS,
FA_PROXY_ADDRESS as FEDERATED_ATTESTATIONS_ADDRESS,
ODIS_PAYMENTS_PROXY_ADDRESS,
} from './constants'

const FEDERATED_ATTESTATIONS_ADDRESS = '0x70F9314aF173c246669cFb0EEe79F9Cfd9C34ee3' as const
const ALFAJORES_RPC = 'https://alfajores-forno.celo-testnet.org'
const ISSUER_PRIVATE_KEY = '0x199abda8320f5af0bb51429d246a4e537d1c85fbfaa30d52f9b34df381bd3a95'
class ASv2 {
walletClient: WalletClient<Transport, typeof celoAlfajores, PrivateKeyAccount>
private walletClient: WalletClient
private publicClient: PublicClient<Transport, typeof celoAlfajores>
issuer: PrivateKeyAccount
authSigner: AuthSigner
serviceContext: ServiceContext
Expand All @@ -29,9 +36,12 @@ class ASv2 {
this.issuer = privateKeyToAccount(ISSUER_PRIVATE_KEY)

this.walletClient = createWalletClient({
chain: celoAlfajores,
transport: http(ALFAJORES_RPC),
account: this.issuer,
})
this.publicClient = createPublicClient({ chain: celoAlfajores, transport: http(ALFAJORES_RPC) })

this.serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES)
this.authSigner = {
authenticationMethod: OdisUtils.Query.AuthenticationMethod.WALLET_KEY,
Expand All @@ -51,15 +61,25 @@ class ASv2 {
this.serviceContext,
)

const { request } = await simulateContract(this.walletClient, {
const federatedAttestations = getContract({
abi: federatedAttestationsABI,
functionName: 'registerAttestationAsIssuer',
args: [obfuscatedIdentifier as Hex, account, attestationIssuedTime],
address: FEDERATED_ATTESTATIONS_ADDRESS,
chain: celoAlfajores,
client: this.walletClient,
})

await writeContract(this.walletClient, request)
await federatedAttestations.write.registerAttestationAsIssuer(
[obfuscatedIdentifier as Hex, account, attestationIssuedTime],
{ chain: this.walletClient.chain, account: this.issuer },
)

// const { request } = await simulateContract(this.walletClient, {
// abi: federatedAttestationsABI,
// functionName: 'registerAttestationAsIssuer',
// address: FEDERATED_ATTESTATIONS_ADDRESS,
// chain: celoAlfajores,
// })

// await this.walletClient.writeContract(request)
}

async lookupAddresses(phoneNumber: string) {
Expand All @@ -73,7 +93,7 @@ class ASv2 {
)

// query on-chain mappings
const [_countsPerIssuer, accounts, _signers] = await readContract(this.walletClient, {
const [_countsPerIssuer, accounts, _signers] = await readContract(this.publicClient, {
abi: federatedAttestationsABI,
functionName: 'lookupAttestations',
address: '0x',
Expand All @@ -93,36 +113,55 @@ class ASv2 {

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 = getContract({
abi: stableTokenABI,
address: ALFAJORES_CUSD_ADDRESS,
client: { public: this.publicClient, wallet: this.walletClient },
})
const odisPaymentsContract = getContract({
abi: odisPaymentsABI,
address: ODIS_PAYMENTS_PROXY_ADDRESS,
client: { public: this.publicClient, wallet: this.walletClient },
})

// give odis payment contract permission to use cUSD
const currentAllowance = await stableTokenContract.allowance(
const currentAllowance = await stableTokenContract.read.allowance([
this.issuer.address,
odisPaymentsContract.address,
)
])
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 = parseEther('0.01')

if (currentAllowance <= ONE_CENT_CUSD_WEI) {
const approvalTxHash = await stableTokenContract.write.increaseAllowance(
[odisPaymentsContract.address, ONE_CENT_CUSD_WEI],
{ account: this.issuer, chain: celoAlfajores },
)

const approvalTxReceipt = await this.publicClient.waitForTransactionReceipt({
hash: approvalTxHash,
})

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
enoughAllowance = approvalTxReceipt.status === 'success'
} else {
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)
const odisPaymentHash = await odisPaymentsContract.write.payInCUSD(
[this.issuer.address, ONE_CENT_CUSD_WEI],
{ account: this.issuer, chain: celoAlfajores },
)

const odisPaymentReceipt = await this.publicClient.waitForTransactionReceipt({
hash: odisPaymentHash,
})
console.log('odis payment tx status:', odisPaymentReceipt.status)
console.log('odis payment tx hash:', odisPaymentHash)
} else {
throw 'cUSD approval failed'
}
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/web3.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { OdisUtils } from '@celo/identity'
import { AuthSigner, OdisContextName } from '@celo/identity/lib/odis/query'
import Web3 from 'web3'
import {
ACCOUNTS_CONTRACT,
ACCOUNTS_PROXY_ADDRESS,
Expand All @@ -8,9 +11,6 @@ import {
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'

const ISSUER_PRIVATE_KEY = '0x726e53db4f0a79dfd63f58b19874896fce3748fcb80874665e0c147369c04a37'
const DEK_PUBLIC_KEY = '0x026063780c81991c032fb4fa7485c6607b7542e048ef85d08516fe5c4482360e4b'
Expand Down
3 changes: 1 addition & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"dependencies": {
"@celo/abis": "^11.0.0",
"@celo/base": "^6.0.0",
"@celo/contractkit": "^7.1.0",
"@celo/utils": "^6.0.0",
"@types/bunyan": "1.8.10",
"bignumber.js": "^9.1.2",
Expand All @@ -35,7 +34,7 @@
"io-ts": "2.0.1",
"is-base64": "^1.1.0",
"node-fetch": "^2.6.9",
"viem": "^2.19.3"
"viem": "2.21.14"
},
"devDependencies": {
"@celo/poprf": "^0.1.9",
Expand Down
Loading

0 comments on commit d2091b6

Please sign in to comment.