Skip to content

Commit

Permalink
Register canon addr only if not exists (#586)
Browse files Browse the repository at this point in the history
* only register if not exists

* fix isZero

* changseset

* Update sdk.ts

* maybeCanonaddr

* api docs
  • Loading branch information
panieldark authored Nov 10, 2023
1 parent 1ac7460 commit 94bb921
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-cars-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nocturne-xyz/frontend-sdk": patch
---

Only register canon addr if not already
2 changes: 1 addition & 1 deletion packages/frontend-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nocturne-xyz/frontend-sdk",
"version": "5.0.0",
"version": "5.0.1",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend-sdk/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export interface NocturneSdkApi {

/**
* Register the user's canonical address from the snap instance against the current signer EOA.
* If already registered, returns undefined, otherwise returns contract transaction of registration.
*/
registerCanonicalAddress(): Promise<ContractTransaction>;
registerCanonicalAddress(): Promise<ContractTransaction | undefined>;

/**
* @param values Asset amounts
Expand Down
36 changes: 23 additions & 13 deletions packages/frontend-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,14 @@ export class NocturneSdk implements NocturneSdkApi {
return await this.depositAdapter.fetchDepositRequestsBySpender(spender);
}

// TODO: use this method in interface
async registerCanonicalAddress(): Promise<ethers.ContractTransaction> {
async registerCanonicalAddress(): Promise<
ethers.ContractTransaction | undefined
> {
const ethSigner = await getSigner(this.provider);
const address = await ethSigner.getAddress();
const maybeExistingCanonAddr = await this.getCanonAddrFromRegistry(address);
if (maybeExistingCanonAddr) return;

const client = await this.clientThunk();
const registry = await this.canonAddrRegistryThunk();
const prover = await this.canonAddrSigCheckProverThunk();
Expand Down Expand Up @@ -860,17 +865,8 @@ export class NocturneSdk implements NocturneSdkApi {
}

// check it has corresponding canon addr in registry
const registry = await this.canonAddrRegistryThunk();
let maybeCompressedCanonAddr: BigNumber | undefined;
try {
maybeCompressedCanonAddr =
(await registry._ethAddressToCompressedCanonAddr(eoaAddr)) as
| BigNumber
| undefined;
} catch (err) {
console.warn("error when looking up canon addr in registry: ", err);
return undefined;
}
const maybeCompressedCanonAddr =
await this.getCanonAddrFromRegistry(eoaAddr);

if (!maybeCompressedCanonAddr) {
return undefined;
Expand Down Expand Up @@ -1104,4 +1100,18 @@ export class NocturneSdk implements NocturneSdkApi {
}),
);
}
private async getCanonAddrFromRegistry(
eoaAddr: string,
): Promise<BigNumber | undefined> {
// check it has corresponding canon addr in registry
const registry = await this.canonAddrRegistryThunk();
try {
const maybeCanonAddr =
await registry._ethAddressToCompressedCanonAddr(eoaAddr);
return maybeCanonAddr.isZero() ? undefined : maybeCanonAddr;
} catch (err) {
console.warn("error when looking up canon addr in registry: ", err);
return undefined;
}
}
}

0 comments on commit 94bb921

Please sign in to comment.