Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate From ContractKit to Viem #294

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/fair-knives-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@celo/phone-number-privacy-monitor": major
---

Replace @celo/contractkit with viem


### Breaking Changes

`queryOdisForQuota` and `queryOdisForSalt` for first param instead of a string url now take an object with rpcURL and chainID.

```diff
- queryOdisForQuota("https://forno.celo.org",...rest)
+ queryOdisForQuota({rpcURL: "https://forno.celo.org", chainID: 42220},...rest)


- queryOdisForSalt("https://forno.celo.org",...rest)
+ queryOdisForSalt({rpcURL: "https://forno.celo.org", chainID: 42220},...rest)
```
48 changes: 48 additions & 0 deletions .changeset/hot-buckets-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
"@celo/phone-number-privacy-combiner": major
---

Replace @celo/contractkit with viem


### Breaking Changes

`startCombiner` now takes an optional WalletClient instance from viem instead of a ContractKit instance

config passed to `startCombiner` has a sub config `blockchain` which has the following changes.

```diff
{
...restOfConfig,
blockchain: {
- provider: "https://forno.celo.org"
+ rpcURL: "https://forno.celo.org"
+ chainID: 42220
}
}

```
---

`lib/common/web3` => `lib/common`

`getDEK` moved from

it now takes a WalletClient as its first param instead of a ContractKit.

third param is now typed to require address starts with 0x

```diff
- export async function getDEK(kit: ContractKit, logger: Logger, account: string): Promise<string>
+ export async function getDEK(client: Client, logger: Logger, account: Address): Promise<string>
```

---

lib/pnp/services/account-services`

`ContractKitAccountServiceOptions` => `ViemAccountServiceOptions`

`ContractKitAccountService` => `ViemAccountService`

addressed passed to `getAccount` now MUST start with `0x` in type
13 changes: 13 additions & 0 deletions .changeset/nasty-forks-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@celo/identity": major
---

Contract kit has been replaced with viem as dependency.

from `lib/odis/query`; WalletKeySigner instead of a contractKit instance now takes a sign191 function

- This should use EIP191 to sign the message using the private key assosiated with the account

Most places that were previously typed as string are now 0x-string typed

ContractKit is now an optional peer dependency. it is only needed if using the offchain-data-wrapper
5 changes: 5 additions & 0 deletions .changeset/neat-tables-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@celo/encrypted-backup": patch
---

Remove @celo/connect as a depdendency
66 changes: 66 additions & 0 deletions .changeset/shiny-plants-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
"@celo/phone-number-privacy-common": major
---

Replace @celo/contractkit with viem


### BREAKING CHANGES
Config changes

`BlockchainConfig` gains a new required `chainID` property

`provider` which is an overloaded term is renamed to `rpcURL`

```diff
config: BlockchainConfig = {
- provider: FORNO_URL
+ rpcURL: FORNOURL
+ chainID: 42220
}
```

`getContractKit` => `getWalletClient`


functions replaced in utils/authentication

`newContractKitFetcher` => `newDEKFetcher`

```diff
- newContractKitFetcher(contractKit: ContractKit, ...)
+ newDEKFetcher(viemClient: Client, ...)
```

functions with with changed signatures

`getDataEncryptionKey`

```diff
export async function getDataEncryptionKey(
- address: string,
- contractKit: ContractKit,
+ address: Address,
+ viemClient: Client,
logger: Logger,
fullNodeTimeoutMs: number,
fullNodeRetryCount: number,
fullNodeRetryDelayMs: number,
- ): Promise<string>
+ ): Promise<Hex>
```

functions removed from test/utils

- `createMockToken`
- `createMockContractKit`
- `createMockConnection`
- `createMockWeb3`
- `replenishQuota`


### NEW FUNCTIONS

`import {getAccountsContract, getOdisPaymentsContract, getCUSDContract } from @celo/phone-number-privacy-common`

To replace contractKit wrappers for Accounts, OdisPayments, and StableToken, contracts.
22 changes: 22 additions & 0 deletions .changeset/stupid-suns-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@celo/phone-number-privacy-signer": major
---

Replace @celo/contractKit with viem


If you are just running the service no actual changes required except to use with same major version of combiner and monitor

### Breaking Changes



`ContractKitAccountService` => `ClientAccountService`

```diff
- new ContractKitAccountService(logger, contractKit)
+ new ClientAccountService(logger, walletClient)

```

`getAccount` now takes strongly typed 0x string
16 changes: 0 additions & 16 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
},
{
"name": "Debug ContractKit Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--rootDir",
"${workspaceFolder}/packages/contractkit",
"--runInBand",
"${workspaceFolder}/packages/contractkit/src/**/*.test.ts",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ We've made a mini-series to explain you:

## 🧑‍💻 Quickstart

The following steps use the Celo [ContractKit](https://docs.celo.org/developer/contractkit) to quickly set you up to play around with the protocol. If you would like to use a different library instead, please refer to the [example scripts](examples/).
The following steps use the [Viem](https://viem.sh) to quickly set you up to play around with the protocol. If you would like to use a different library instead, please refer to the [example scripts](examples/).

1. Add the [`@celo/identity`](https://www.npmjs.com/package/@celo/identity) package into your project.

Expand All @@ -50,17 +50,18 @@ The following steps use the Celo [ContractKit](https://docs.celo.org/developer/c
2. Set up your issuer (read "Authentication" section in [privacy.md](docs/docs/privacy.md#authentication)), which is the account registering attestations. When a user requests for the issuer to register an attestation, the issuer should [verify](docs/protocol.md#verification) somehow that the user owns their identifier (ex. SMS verification for phone number identifiers).

```ts
import { newKit } from "@celo/contractkit";

import { createClient } from "viem";
import { celoAlfajores } from "viem/chains"
import { privateKeyToAccount } from 'viem/accounts'
// the issuer is the account that is registering the attestation
let ISSUER_PRIVATE_KEY;

// create alfajores contractKit instance with the issuer private key
const kit = await newKit("https://alfajores-forno.celo-testnet.org");
kit.addAccount(ISSUER_PRIVATE_KEY);
const issuerAddress =
kit.web3.eth.accounts.privateKeyToAccount(ISSUER_PRIVATE_KEY).address;
kit.defaultAccount = issuerAddress;
// create alfajores viem client with the issuer private key
const viemClient = createClient({
account: privateKeyToAccount(ISSUER_PRIVATE_KEY)
transport: http(),
chain: celoAlfajores
});

// information provided by user, issuer should confirm they do own the identifier
const userPlaintextIdentifier = "+12345678910";
Expand All @@ -79,7 +80,7 @@ The following steps use the Celo [ContractKit](https://docs.celo.org/developer/c
// authSigner provides information needed to authenticate with ODIS
const authSigner: AuthSigner = {
authenticationMethod: OdisUtils.Query.AuthenticationMethod.WALLET_KEY,
contractKit: kit,
sign191: ({message, account}) => viemClient.signMessage({message, account}),
};
// serviceContext provides the ODIS endpoint and public key
const serviceContext = OdisUtils.Query.getServiceContext(
Expand Down Expand Up @@ -152,7 +153,7 @@ The following steps use the Celo [ContractKit](https://docs.celo.org/developer/c

| Type |
| :-------------------------------------------------------------------------------------------: |
| [ContractKit](docs/examples/contractKit.ts) |
| [Viem](docs/examples/viem.ts) |
| [EthersJS (v5)](docs/examples/ethers.ts) |
| [web3.js](docs/examples/web3.ts) |
| [NextJS based web app (Phone Number)](https://github.com/celo-org/emisianto) |
Expand All @@ -161,7 +162,7 @@ The following steps use the Celo [ContractKit](https://docs.celo.org/developer/c
| [NextJS based web app (Twitter)](https://github.com/celo-org/SocialConnect-Twitter) |
| [Server side NextJS (Twitter)](https://github.com/celo-org/SocialConnect-Twitter-Server-Side) |

<!-- - [@celo/contractkit](https://docs.celo.org/developer/contractkit) (see [`examples/contractKit.ts`](examples/contractKit.ts)),
<!-- - [viem](https://viem.sh) (see [`examples/viem.ts`](examples/viem.ts)),
- [ethers.js](https://ethers.org/) (see [`examples/ethers.ts`](examples/ethers.ts)), and
- [web3.js](https://web3js.readthedocs.io/en/v1.8.1/) (see [`examples/web3.ts`](examples/web3.ts)). -->

Expand Down
8 changes: 4 additions & 4 deletions apps/combiner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"test:e2e:mainnet": "CONTEXT_NAME=mainnet yarn test:e2e"
},
"dependencies": {
"@celo/base": "^6.0.0",
"@celo/contractkit": "^7.1.0",
"@celo/base": "^7.0.0",
"@celo/encrypted-backup": "^5.0.6",
"@celo/identity": "^5.1.2",
"@celo/phone-number-privacy-common": "^3.1.2",
Expand Down Expand Up @@ -55,11 +54,12 @@
"node-fetch": "^2.6.9",
"pg": "^8.2.1",
"prom-client": "12.0.0",
"uuid": "^7.0.3"
"uuid": "^7.0.3",
"viem": "2.21.14"
},
"devDependencies": {
"@celo/phone-number-privacy-signer": "workspace:^",
"@celo/utils": "^6.0.0",
"@celo/utils": "^8.0.0",
"jest": "^29.7.0",
"knex": "^2.1.0",
"typescript": "^5.2.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ContractKit } from '@celo/contractkit'
import { ErrorMessage, getDataEncryptionKey } from '@celo/phone-number-privacy-common'
import Logger from 'bunyan'
import config from '../../config'
import { Counters, Histograms, newMeter } from '../metrics'
import { Address, Client } from 'viem'
import config from '../config'
import { Counters, Histograms, newMeter } from './metrics'

export async function getDEK(kit: ContractKit, logger: Logger, account: string): Promise<string> {
export async function getDEK(client: Client, logger: Logger, account: Address): Promise<string> {
const _meter = newMeter(Histograms.fullNodeLatency, 'getDataEncryptionKey')
return _meter(() =>
getDataEncryptionKey(
account,
kit,
client,
logger,
config.phoneNumberPrivacy.fullNodeTimeoutMs,
config.phoneNumberPrivacy.fullNodeRetryCount,
Expand Down
7 changes: 5 additions & 2 deletions apps/combiner/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TestUtils,
toBool,
} from '@celo/phone-number-privacy-common'
import { celoAlfajores } from 'viem/chains'

export function getCombinerVersion(): string {
return process.env.npm_package_version ?? require('../package.json').version ?? '0.0.0'
Expand Down Expand Up @@ -80,7 +81,8 @@ if (DEV_MODE) {
port: 8081,
},
blockchain: {
provider: FORNO_ALFAJORES,
rpcURL: FORNO_ALFAJORES,
chainID: celoAlfajores.id,
},
phoneNumberPrivacy: {
serviceName: defaultServiceName,
Expand Down Expand Up @@ -166,7 +168,8 @@ if (DEV_MODE) {
sslCertPath: env.SERVER_SSL_CERT_PATH,
},
blockchain: {
provider: env.BLOCKCHAIN_PROVIDER,
rpcURL: env.BLOCKCHAIN_PROVIDER,
chainID: env.CHAIN_ID,
apiKey: env.BLOCKCHAIN_API_KEY,
},
phoneNumberPrivacy: {
Expand Down
16 changes: 8 additions & 8 deletions apps/combiner/src/pnp/services/account-services.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ContractKit } from '@celo/contractkit'
import { ErrorMessage } from '@celo/phone-number-privacy-common'
import Logger from 'bunyan'
import { LRUCache } from 'lru-cache'
import { Address, Client } from 'viem'
import { getDEK } from '../../common/contracts'
import { OdisError, wrapError } from '../../common/error'
import { Counters } from '../../common/metrics'
import { traceAsyncFunction } from '../../common/tracing-utils'
import { getDEK } from '../../common/web3/contracts'

export interface AccountService {
getAccount(address: string): Promise<string>
}

export interface ContractKitAccountServiceOptions {
export interface ViemAccountServiceOptions {
fullNodeTimeoutMs: number
fullNodeRetryCount: number
fullNodeRetryDelayMs: number
Expand Down Expand Up @@ -44,15 +44,15 @@ export class CachingAccountService implements AccountService {
}

// tslint:disable-next-line:max-classes-per-file
export class ContractKitAccountService implements AccountService {
export class ViemAccountService implements AccountService {
constructor(
private readonly logger: Logger,
private readonly kit: ContractKit,
private readonly client: Client,
) {}

async getAccount(address: string): Promise<string> {
return traceAsyncFunction('ContractKitAccountService - getAccount', async () => {
return wrapError(getDEK(this.kit, this.logger, address), ErrorMessage.FAILURE_TO_GET_DEK)
async getAccount(address: Address): Promise<string> {
return traceAsyncFunction('ViemAccountService - getAccount', async () => {
return wrapError(getDEK(this.client, this.logger, address), ErrorMessage.FAILURE_TO_GET_DEK)
})
}
}
Expand Down
Loading
Loading