Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Expanders followup #556

Merged
merged 2 commits into from
Mar 28, 2023
Merged
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
39 changes: 33 additions & 6 deletions contracts/clients/src/AddressRegistryWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
/* eslint-disable camelcase */

import { BigNumber, BigNumberish, ethers, Signer } from "ethers";
import { AddressRegistry } from "../typechain-types/contracts/AddressRegistry";
import { AddressRegistry__factory } from "../typechain-types/factories/contracts/AddressRegistry__factory";
import { AddressRegistry__factory as AddressRegistryFactory } from "../typechain-types/factories/contracts/AddressRegistry__factory";
import SignerOrProvider from "./helpers/SignerOrProvider";
import SafeSingletonFactory, {
SafeSingletonFactoryViewer,
} from "./SafeSingletonFactory";

/**
* A wrapper around the `AddressRegistry` contract to provide a more ergonomic
* interface, especially for `reverseLookup`.
*/
export default class AddressRegistryWrapper {
constructor(public registry: AddressRegistry) {}

/**
* Deploys a new `AddressRegistry` contract the traditional way.
*/
static async deployNew(signer: Signer): Promise<AddressRegistryWrapper> {
const factory = new AddressRegistry__factory(signer);
const factory = new AddressRegistryFactory(signer);

return new AddressRegistryWrapper(await factory.deploy());
}

/**
* Uses Gnosis Safe's factory to get an `AddressRegistry` contract at a
* predetermined address. Deploys if it doesn't already exist.
*/
static async connectOrDeploy(
signerOrFactory: Signer | SafeSingletonFactory,
salt: ethers.utils.BytesLike = ethers.utils.solidityPack(["uint256"], [0]),
): Promise<AddressRegistryWrapper> {
const factory = await SafeSingletonFactory.from(signerOrFactory);

const registry = await factory.connectOrDeploy(
AddressRegistry__factory,
AddressRegistryFactory,
[],
salt,
);

return new AddressRegistryWrapper(registry);
}

/**
* Uses Gnosis Safe's factory to get an `AddressRegistry` contract at a
* predetermined address. Returns undefined if it doesn't exist.
*/
static async connectIfDeployed(
signerOrProvider: SignerOrProvider,
salt: ethers.utils.BytesLike = ethers.utils.solidityPack(["uint256"], [0]),
Expand All @@ -41,20 +54,27 @@ export default class AddressRegistryWrapper {
);

const registry = await factoryViewer.connectIfDeployed(
AddressRegistry__factory,
AddressRegistryFactory,
[],
salt,
);

return registry ? new AddressRegistryWrapper(registry) : undefined;
}

/**
* Uses an id to lookup an address, the same way that happens on chain.
*/
async lookup(id: BigNumberish): Promise<string | undefined> {
const address = await this.registry.addresses(id);

return address === ethers.constants.AddressZero ? undefined : address;
}

/**
* Uses an address to lookup an id - the reverse of what happens on chain, by
* making use of the indexed `AddressRegistered` event.
*/
async reverseLookup(address: string): Promise<BigNumber | undefined> {
const events = await this.registry.queryFilter(
this.registry.filters.AddressRegistered(null, address),
Expand All @@ -65,6 +85,9 @@ export default class AddressRegistryWrapper {
return id;
}

/**
* Registers an address and returns the id that was assigned to it.
*/
async register(address: string): Promise<BigNumber> {
await (await this.registry.register(address)).wait();

Expand All @@ -77,6 +100,10 @@ export default class AddressRegistryWrapper {
return id;
}

/**
* Registers an address if it hasn't already been registered, and returns the
* id that was assigned to it.
*/
async registerIfNeeded(address: string): Promise<BigNumber> {
let id = await this.reverseLookup(address);

Expand Down
39 changes: 33 additions & 6 deletions contracts/clients/src/BlsPublicKeyRegistryWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
/* eslint-disable camelcase */

import { BigNumber, BigNumberish, ethers, Signer } from "ethers";
import { solidityKeccak256 } from "ethers/lib/utils";
import {
BLSPublicKeyRegistry,
BLSPublicKeyRegistry__factory,
BLSPublicKeyRegistry__factory as BLSPublicKeyRegistryFactory,
} from "../typechain-types";
import SignerOrProvider from "./helpers/SignerOrProvider";
import SafeSingletonFactory, {
SafeSingletonFactoryViewer,
} from "./SafeSingletonFactory";
import { PublicKey } from "./signer";

/**
* A wrapper around the `BLSPublicKeyRegistry` contract to provide a more
* ergonomic interface, especially for `reverseLookup`.
*/
export default class BlsPublicKeyRegistryWrapper {
constructor(public registry: BLSPublicKeyRegistry) {}

/**
* Deploys a new `BLSPublicKeyRegistry` contract the traditional way.
*/
static async deployNew(signer: Signer): Promise<BlsPublicKeyRegistryWrapper> {
const factory = new BLSPublicKeyRegistry__factory(signer);
const factory = new BLSPublicKeyRegistryFactory(signer);

return new BlsPublicKeyRegistryWrapper(await factory.deploy());
}

/**
* Uses Gnosis Safe's factory to get an `BLSPublicKeyRegistry` contract at a
* predetermined address. Deploys if it doesn't already exist.
*/
static async connectOrDeploy(
signerOrFactory: Signer | SafeSingletonFactory,
salt: ethers.utils.BytesLike = ethers.utils.solidityPack(["uint256"], [0]),
): Promise<BlsPublicKeyRegistryWrapper> {
const factory = await SafeSingletonFactory.from(signerOrFactory);

const registry = await factory.connectOrDeploy(
BLSPublicKeyRegistry__factory,
BLSPublicKeyRegistryFactory,
[],
salt,
);

return new BlsPublicKeyRegistryWrapper(registry);
}

/**
* Uses Gnosis Safe's factory to get an `BLSPublicKeyRegistry` contract at a
* predetermined address. Returns undefined if it doesn't exist.
*/
static async connectIfDeployed(
signerOrProvider: SignerOrProvider,
salt: ethers.utils.BytesLike = ethers.utils.solidityPack(["uint256"], [0]),
Expand All @@ -45,14 +58,17 @@ export default class BlsPublicKeyRegistryWrapper {
);

const registry = await factoryViewer.connectIfDeployed(
BLSPublicKeyRegistry__factory,
BLSPublicKeyRegistryFactory,
[],
salt,
);

return registry ? new BlsPublicKeyRegistryWrapper(registry) : undefined;
}

/**
* Uses an id to lookup a public key, the same way that happens on chain.
*/
async lookup(id: BigNumberish): Promise<PublicKey | undefined> {
const blsPublicKey = await Promise.all([
this.registry.blsPublicKeys(id, 0),
Expand All @@ -68,6 +84,10 @@ export default class BlsPublicKeyRegistryWrapper {
return blsPublicKey;
}

/**
* Uses a public key to lookup an id - the reverse of what happens on chain,
* by making use of the indexed `BLSPublicKeyRegistered` event.
*/
async reverseLookup(blsPublicKey: PublicKey): Promise<BigNumber | undefined> {
const blsPublicKeyHash = solidityKeccak256(["uint256[4]"], [blsPublicKey]);

Expand All @@ -80,6 +100,9 @@ export default class BlsPublicKeyRegistryWrapper {
return id;
}

/**
* Registers a public key and returns the id.
*/
async register(blsPublicKey: PublicKey): Promise<BigNumber> {
await (await this.registry.register(blsPublicKey)).wait();

Expand All @@ -92,6 +115,10 @@ export default class BlsPublicKeyRegistryWrapper {
return id;
}

/**
* Registers a public key if it hasn't already been registered, and returns
* the id that was assigned to it.
*/
async registerIfNeeded(blsPublicKey: PublicKey): Promise<BigNumber> {
let id = await this.reverseLookup(blsPublicKey);

Expand Down
38 changes: 17 additions & 21 deletions contracts/clients/src/BlsRegistrationCompressor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable camelcase */

import { BigNumber, ethers, Signer } from "ethers";
import {
AddressRegistry__factory,
AddressRegistry__factory as AddressRegistryFactory,
AggregatorUtilities,
AggregatorUtilities__factory,
BLSPublicKeyRegistry__factory,
AggregatorUtilities__factory as AggregatorUtilitiesFactory,
BLSPublicKeyRegistry__factory as BLSPublicKeyRegistryFactory,
BLSRegistration,
BLSRegistration__factory,
BLSRegistration__factory as BLSRegistrationFactory,
} from "../typechain-types";
import AddressRegistryWrapper from "./AddressRegistryWrapper";
import BlsPublicKeyRegistryWrapper from "./BlsPublicKeyRegistryWrapper";
Expand Down Expand Up @@ -43,31 +41,29 @@ export default class BlsRegistrationCompressor implements IOperationCompressor {
return new BlsRegistrationCompressor(
blsRegistration,
new BlsPublicKeyRegistryWrapper(
BLSPublicKeyRegistry__factory.connect(
BLSPublicKeyRegistryFactory.connect(
blsPublicKeyRegistryAddress,
blsRegistration.signer,
),
),
new AddressRegistryWrapper(
AddressRegistry__factory.connect(
AddressRegistryFactory.connect(
addressRegistryAddress,
blsRegistration.signer,
),
),
AggregatorUtilities__factory.connect(
AggregatorUtilitiesFactory.connect(
aggregatorUtilitiesAddress,
blsRegistration.signer,
),
);
}

static async deployNew(signer: Signer): Promise<BlsRegistrationCompressor> {
const blsPublicKeyRegistryFactory = new BLSPublicKeyRegistry__factory(
signer,
);
const blsPublicKeyRegistryFactory = new BLSPublicKeyRegistryFactory(signer);

const addressRegistryFactory = new AddressRegistry__factory(signer);
const aggregatorUtilitiesFactory = new AggregatorUtilities__factory(signer);
const addressRegistryFactory = new AddressRegistryFactory(signer);
const aggregatorUtilitiesFactory = new AggregatorUtilitiesFactory(signer);

const [
blsPublicKeyRegistryContract,
Expand All @@ -79,7 +75,7 @@ export default class BlsRegistrationCompressor implements IOperationCompressor {
aggregatorUtilitiesFactory.deploy(),
]);

const blsRegistrationFactory = new BLSRegistration__factory(signer);
const blsRegistrationFactory = new BLSRegistrationFactory(signer);

const blsRegistrationContract = await blsRegistrationFactory.deploy(
blsPublicKeyRegistryContract.address,
Expand All @@ -105,11 +101,11 @@ export default class BlsRegistrationCompressor implements IOperationCompressor {
await Promise.all([
BlsPublicKeyRegistryWrapper.connectOrDeploy(factory, salt),
AddressRegistryWrapper.connectOrDeploy(factory, salt),
factory.connectOrDeploy(AggregatorUtilities__factory, [], salt),
factory.connectOrDeploy(AggregatorUtilitiesFactory, [], salt),
]);

const blsRegistrationContract = await factory.connectOrDeploy(
BLSRegistration__factory,
BLSRegistrationFactory,
[
blsPublicKeyRegistry.registry.address,
addressRegistry.registry.address,
Expand All @@ -135,25 +131,25 @@ export default class BlsRegistrationCompressor implements IOperationCompressor {
);

const blsPublicKeyRegistryAddress = factoryViewer.calculateAddress(
BLSPublicKeyRegistry__factory,
BLSPublicKeyRegistryFactory,
[],
salt,
);

const addressRegistryAddress = factoryViewer.calculateAddress(
AddressRegistry__factory,
AddressRegistryFactory,
[],
salt,
);

const aggregatorUtilitiesAddress = factoryViewer.calculateAddress(
AggregatorUtilities__factory,
AggregatorUtilitiesFactory,
[],
salt,
);

const blsRegistration = await factoryViewer.connectIfDeployed(
BLSRegistration__factory,
BLSRegistrationFactory,
[
blsPublicKeyRegistryAddress,
addressRegistryAddress,
Expand Down
Loading