Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from klaytn/dev
Browse files Browse the repository at this point in the history
Merge dev into main
  • Loading branch information
Lewis authored Mar 12, 2024
2 parents 11bacdd + fbe57d6 commit bbabe5c
Show file tree
Hide file tree
Showing 22 changed files with 2,752 additions and 2,978 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "Publish SDK"

on:
workflow_run:
workflows: ["testing"]
branches: [main]
types:
- completed

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 20
registry-url: https://registry.npmjs.org/

- name: generate zkauth-sdk
shell: bash
run: |
npm install && npm run build
- name: publish zkauth-sdk
shell: bash
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npx pnpm --filter "@klaytn/*" -r publish --publish-branch main --no-git-check --access=public
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Users can create their zkAuth wallet based on their OAuth2 idToken.
chainIdOrZero: 0,
subHash: subHash,
provider: JsonRpcProvider,
entryPointAddress: Addresses[chainId].entryPointAddr,
entryPointAddress: Addresses[chainId].EntryPointAddr,
};

const scw = new RecoveryAccountAPI(signer, params, Addresses[chainId].oidcRecoveryFactoryV02Addr);
const scw = new RecoveryAccountAPI(signer, params, Addresses[chainId].RecoveryFactoryAddr);
const cfAddress = await scw.getAccountAddress();
// Save wallet as your own
```
Expand All @@ -63,7 +63,7 @@ Users can create their zkAuth wallet based on their OAuth2 idToken.
```js
const signer = new ethers.Wallet(ownerKey, JsonRpcProvider);
const scw = new RecoveryAccountAPI(signer, params, Addresses.oidcRecoveryFactoryV02Addr);
const scw = new RecoveryAccountAPI(signer, params, Addresses.RecoveryFactoryAddr);
const target = cfAddress;
// You can use any UserOp, so use entryPoint() as an example
Expand Down Expand Up @@ -93,7 +93,7 @@ Users can send transaction called `UserOp` with their zkAuth wallet. It requires
provider: getProvider(network.chainId),
entryPointAddress: Addresses.entryPointAddr,
};
const scw = new RecoveryAccountAPI(signer, param, Addresses.oidcRecoveryFactoryV02Addr);
const scw = new RecoveryAccountAPI(signer, param, Addresses.RecoveryFactoryAddr);
```

2. Prepare transaction data
Expand Down Expand Up @@ -136,9 +136,9 @@ Users can add a new guardian to their zkAuth wallet. It allows same provider but
const param: RecoveryAccountApiParams = {
scaAddr: cfAddress,
provider: JsonRpcProvider,
entryPointAddress: Addresses[chainId].entryPointAddr,
entryPointAddress: Addresses[chainId].EntryPointAddr,
};
const scw = new RecoveryAccountAPI(signer, param, Addresses[chainId].oidcRecoveryFactoryV02Addr);
const scw = new RecoveryAccountAPI(signer, param, Addresses[chainId].RecoveryFactoryAddr);
const data = scw.encodeAddGuardian(newGuardian, newSubHash, newThreshold);
const tx: TransactionDetailsForUserOp = {
target: cfAddress,
Expand Down Expand Up @@ -173,9 +173,9 @@ Users can remove a guardian from their zkAuth wallet.
const param: RecoveryAccountApiParams = {
scaAddr: cfAddress,
provider: JsonRpcProvider,
entryPointAddress: Addresses[chainId].entryPointAddr,
entryPointAddress: Addresses[chainId].EntryPointAddr,
};
const scw = new RecoveryAccountAPI(signer, param, Addresses[chainId].oidcRecoveryFactoryV02Addr);
const scw = new RecoveryAccountAPI(signer, param, Addresses[chainId].RecoveryFactoryAddr);
const data = scw.encodeRemoveGuardian(targetGuardian, newSubHash, newThreshold);
const tx: TransactionDetailsForUserOp = {
target: cfAddress,
Expand Down Expand Up @@ -225,8 +225,6 @@ If user wallet is `ghost` wallet, user can't recover it, so delete it and create
const iss: string[] = [];
const sub: string[] = [];
const salts: string[] = [];
const confUrls: string[] = [];
const jwkUrls: string[] = [];
const jwks: RsaJsonWebKey[] = [];
const proofAndPubSigs: any[] = [];
Expand All @@ -239,8 +237,6 @@ If user wallet is `ghost` wallet, user can't recover it, so delete it and create
sub.push(subTemp);
salts.push(await calcSalt(subTemp));
const provider = getProviderNameFromIss(issTemp);
confUrls.push(OIDCProviders.find(p => p.name === provider.toLowerCase())?.confUrl as string);
jwkUrls.push(OIDCProviders.find(p => p.name === provider.toLowerCase())?.jwkUrl as string);
jwks.push((await getJWKs(provider, header.kid)) as RsaJsonWebKey);
// Prepare zk proof
Expand All @@ -257,9 +253,9 @@ If user wallet is `ghost` wallet, user can't recover it, so delete it and create
const params: RecoveryAccountApiParams = {
scaAddr: cfAddress,
provider: JsonRpcProvider,
entryPointAddress: Addresses[chainId].entryPointAddr,
entryPointAddress: Addresses[chainId].EntryPointAddr,
};
const scw = new RecoveryAccountAPI(signer, params, Addresses[chainId].oidcRecoveryFactoryV02Addr);
const scw = new RecoveryAccountAPI(signer, params, Addresses[chainId].RecoveryFactoryAddr);
for (const [idx] of recoverTokens.entries()) {
const proof = proofAndPubSigs[idx].proof;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@
"test": "test"
}
}

24 changes: 12 additions & 12 deletions src/account-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "ethers";

import { OIDCRecoveryAccountV02, OIDCGuardianV02 } from ".";
import { RecoveryAccount, Guardian } from ".";

export const isPhantom = async (cfAddress: string, rpcUrl: string) => {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
Expand All @@ -16,8 +16,8 @@ export const getThreshold = async (cfAddress: string, rpcUrl: string) => {
return 0;
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const oidcRecoveryAccount = new ethers.Contract(cfAddress, OIDCRecoveryAccountV02, provider);
return Number(await oidcRecoveryAccount.threshold());
const recoveryAccount = new ethers.Contract(cfAddress, RecoveryAccount, provider);
return Number(await recoveryAccount.threshold());
};

export const getIss = async (cfAddress: string, rpcUrl: string) => {
Expand All @@ -32,7 +32,7 @@ export const getIss = async (cfAddress: string, rpcUrl: string) => {
}

for (const address of guardianAddress) {
const guardian = new ethers.Contract(address, OIDCGuardianV02, provider);
const guardian = new ethers.Contract(address, Guardian, provider);
const iss = await guardian.iss();
oidcIss.push(iss);
}
Expand All @@ -44,8 +44,8 @@ export const getGuardians = async (cfAddress: string, rpcUrl: string) => {
return [];
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const oidcRecoveryAccount = new ethers.Contract(cfAddress, OIDCRecoveryAccountV02, provider);
const guardians = await oidcRecoveryAccount.getGuardiansInfo();
const recoveryAccount = new ethers.Contract(cfAddress, RecoveryAccount, provider);
const guardians = await recoveryAccount.getGuardiansInfo();
const guardianAddress = guardians.map((guardian: any) => guardian[1]);
return guardianAddress;
};
Expand All @@ -55,27 +55,27 @@ export const getInitialOwnerAddress = async (cfAddress: string, rpcUrl: string)
return undefined;
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const oidcRecoveryAccount = new ethers.Contract(cfAddress, OIDCRecoveryAccountV02, provider);
const recoveryAccount = new ethers.Contract(cfAddress, RecoveryAccount, provider);

return await oidcRecoveryAccount.initialOwner();
return await recoveryAccount.initialOwner();
};

export const getInitialGuardianAddress = async (cfAddress: string, rpcUrl: string) => {
if (await isPhantom(cfAddress, rpcUrl)) {
return undefined;
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const oidcRecoveryAccount = new ethers.Contract(cfAddress, OIDCRecoveryAccountV02, provider);
const recoveryAccount = new ethers.Contract(cfAddress, RecoveryAccount, provider);

return await oidcRecoveryAccount.initialGuardian();
return await recoveryAccount.initialGuardian();
};

export const getOwnerAddress = async (cfAddress: string, rpcUrl: string) => {
if (await isPhantom(cfAddress, rpcUrl)) {
return undefined;
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const oidcRecoveryAccount = new ethers.Contract(cfAddress, OIDCRecoveryAccountV02, provider);
const recoveryAccount = new ethers.Contract(cfAddress, RecoveryAccount, provider);

return await oidcRecoveryAccount.owner();
return await recoveryAccount.owner();
};
26 changes: 11 additions & 15 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from "ethers";
import { arrayify, hexConcat, Interface } from "ethers/lib/utils";

import { AuthData } from "./authBuilder";
import { OIDCRecoveryAccountFactoryV02, OIDCRecoveryAccountV02 } from "./constants/abi";
import { RecoveryAccountFactory, RecoveryAccount } from "./constants/abi";

/**
* Needed if user's account is not yet deployed
Expand Down Expand Up @@ -47,27 +47,23 @@ export class RecoveryAccountAPI extends BaseAccountAPI {
this.signer = signer;
}

async _getOIDCRecoveryAccountV02(): Promise<ethers.Contract> {
const accountContract = new ethers.Contract(
await this.getAccountAddress(),
OIDCRecoveryAccountV02,
this.signer
);
async _getRecoveryAccount(): Promise<ethers.Contract> {
const accountContract = new ethers.Contract(await this.getAccountAddress(), RecoveryAccount, this.signer);

return accountContract;
}

async _getOIDCRecoveryAccountFactoryV02(): Promise<ethers.Contract> {
async _getRecoveryAccountFactory(): Promise<ethers.Contract> {
if (this.factoryAddress == null) {
throw new Error("no factory address");
}
const accountContract = new ethers.Contract(this.factoryAddress, OIDCRecoveryAccountFactoryV02, this.signer);
const accountContract = new ethers.Contract(this.factoryAddress, RecoveryAccountFactory, this.signer);
return accountContract;
}

async _getAccountContract(): Promise<ethers.Contract> {
if (this.accountContract == null) {
this.accountContract = await this._getOIDCRecoveryAccountV02();
this.accountContract = await this._getRecoveryAccount();
}
return this.accountContract;
}
Expand All @@ -82,7 +78,7 @@ export class RecoveryAccountAPI extends BaseAccountAPI {
async _getFactoryContract(): Promise<ethers.Contract> {
if (this.factory == null) {
if (this.factoryAddress?.length !== 0) {
this.factory = await this._getOIDCRecoveryAccountFactoryV02();
this.factory = await this._getRecoveryAccountFactory();
} else {
throw new Error("no factory to get initCode");
}
Expand Down Expand Up @@ -150,7 +146,7 @@ export class RecoveryAccountAPI extends BaseAccountAPI {
async requestRecover(newOwner: string, auth: AuthData, subSigner?: ethers.Wallet) {
// TODO: Make separate API for subSigner.
const sca = subSigner
? new ethers.Contract(await this.getAccountAddress(), OIDCRecoveryAccountV02, subSigner)
? new ethers.Contract(await this.getAccountAddress(), RecoveryAccount, subSigner)
: await this._getAccountContract();
const tx = await sca.requestRecover(newOwner, auth);
await tx.wait();
Expand All @@ -169,19 +165,19 @@ export class RecoveryAccountAPI extends BaseAccountAPI {
}

encodeAddGuardian(guardian: string, subHash: string, newThreshold: number) {
const iface = new Interface(OIDCRecoveryAccountV02);
const iface = new Interface(RecoveryAccount);
const data = iface.encodeFunctionData("addGuardian", [guardian, subHash, newThreshold]);
return data;
}

encodeRemoveGuardian(guardian: string, subHash: string, newThreshold: number) {
const iface = new Interface(OIDCRecoveryAccountV02);
const iface = new Interface(RecoveryAccount);
const data = iface.encodeFunctionData("removeGuardian", [guardian, subHash, newThreshold]);
return data;
}

encodeUpdateThreshold(newThreshold: number) {
const iface = new Interface(OIDCRecoveryAccountV02);
const iface = new Interface(RecoveryAccount);
const data = iface.encodeFunctionData("updateThreshold", [newThreshold]);
return data;
}
Expand Down
4 changes: 2 additions & 2 deletions src/authBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IJwtProvider } from "./jwtProvider";
import { typeDataRecovery } from "./samples";
import { sampleProofA, sampleProofB, sampleProofC } from "./samples/constants";

import { ZkauthJwtV02, string2Uints } from ".";
import { ZkauthJwtV02, toUints } from ".";

export interface typeDataArgs {
verifyingContract: string;
Expand Down Expand Up @@ -280,7 +280,7 @@ export function calcGuardianId(subHash: string, guardian: string) {
}

export const generateModPubSig = (modBytes: string | Buffer) => {
return string2Uints(modBytes, ZkauthJwtV02.maxPubLen);
return toUints(Buffer.from(modBytes), ZkauthJwtV02.maxPubLen);
};

export const generateJwtPubSig = (jwt: string) => {
Expand Down
Loading

0 comments on commit bbabe5c

Please sign in to comment.