From ec19cb7010dd315ddefa56263835d7df4f72ecca Mon Sep 17 00:00:00 2001 From: tate Date: Tue, 12 Jul 2022 15:06:50 +1000 Subject: [PATCH] add signer option to all write transactions --- packages/ensjs/src/functions/burnFuses.ts | 4 +++- packages/ensjs/src/functions/createSubname.ts | 6 ++--- packages/ensjs/src/functions/deleteSubname.ts | 3 ++- packages/ensjs/src/functions/setName.ts | 11 +++++---- packages/ensjs/src/functions/setRecords.ts | 23 ++++++++++++++----- packages/ensjs/src/functions/setResolver.ts | 9 ++++---- packages/ensjs/src/functions/transferName.ts | 22 ++++++++---------- .../ensjs/src/functions/transferSubname.ts | 6 ++--- packages/ensjs/src/functions/unwrapName.ts | 6 ++--- packages/ensjs/src/functions/wrapName.test.ts | 4 ++-- packages/ensjs/src/functions/wrapName.ts | 19 ++++++++------- 11 files changed, 63 insertions(+), 50 deletions(-) diff --git a/packages/ensjs/src/functions/burnFuses.ts b/packages/ensjs/src/functions/burnFuses.ts index ae2ca3a5..7c2eb610 100644 --- a/packages/ensjs/src/functions/burnFuses.ts +++ b/packages/ensjs/src/functions/burnFuses.ts @@ -1,3 +1,4 @@ +import { Signer } from 'ethers' import { ENSArgs } from '..' import { FuseOptions } from '../@types/FuseOptions' import generateFuseInput from '../utils/generateFuseInput' @@ -7,8 +8,9 @@ export default async function ( { contracts, provider }: ENSArgs<'contracts' | 'provider'>, name: string, fusesToBurn: FuseOptions, + options?: { addressOrIndex?: string | number; signer?: Signer }, ) { - const signer = provider?.getSigner() + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) if (!signer) { throw new Error('No signer found') diff --git a/packages/ensjs/src/functions/createSubname.ts b/packages/ensjs/src/functions/createSubname.ts index fbf5f9fc..2cf1bfe8 100644 --- a/packages/ensjs/src/functions/createSubname.ts +++ b/packages/ensjs/src/functions/createSubname.ts @@ -1,4 +1,4 @@ -import { ethers } from 'ethers' +import { ethers, Signer } from 'ethers' import { ENSArgs } from '..' import { FuseOptions } from '../@types/FuseOptions' import generateFuseInput from '../utils/generateFuseInput' @@ -9,7 +9,7 @@ type BaseArgs = { owner: string resolverAddress?: string contract: 'registry' | 'nameWrapper' - options?: { addressOrIndex?: string | number } + options?: { addressOrIndex?: string | number; signer?: Signer } } type NameWrapperArgs = { @@ -23,7 +23,7 @@ export default async function ( { contracts, provider }: ENSArgs<'contracts' | 'provider'>, { name, owner, resolverAddress, contract, options, ...wrapperArgs }: Args, ) { - const signer = provider?.getSigner(options?.addressOrIndex) + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) if (!signer) { throw new Error('No signer found') diff --git a/packages/ensjs/src/functions/deleteSubname.ts b/packages/ensjs/src/functions/deleteSubname.ts index dc4c82d8..78527db7 100644 --- a/packages/ensjs/src/functions/deleteSubname.ts +++ b/packages/ensjs/src/functions/deleteSubname.ts @@ -1,3 +1,4 @@ +import { Signer } from 'ethers' import { ENSArgs } from '..' export default async function ( @@ -8,7 +9,7 @@ export default async function ( }: ENSArgs<'contracts' | 'provider' | 'transferSubname'>, name: string, contract: 'registry' | 'nameWrapper', - options?: { addressOrIndex?: string | number }, + options?: { addressOrIndex?: string | number; signer?: Signer }, ) { return transferSubname( name, diff --git a/packages/ensjs/src/functions/setName.ts b/packages/ensjs/src/functions/setName.ts index ebb286b6..48a45ff4 100644 --- a/packages/ensjs/src/functions/setName.ts +++ b/packages/ensjs/src/functions/setName.ts @@ -1,3 +1,4 @@ +import { Signer } from 'ethers' import { ENSArgs } from '..' export default async function ( @@ -5,16 +6,16 @@ export default async function ( name: string, address?: string, resolver?: string, - options?: { addressOrIndex?: string | number }, + options?: { addressOrIndex?: string | number; signer?: Signer }, ) { - const signerAddress = await provider - ?.getSigner(options?.addressOrIndex) - .getAddress() + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) - if (!signerAddress) { + if (!signer) { throw new Error('No signer found') } + const signerAddress = await signer.getAddress() + const reverseRegistrar = (await contracts?.getReverseRegistrar())?.connect( provider?.getSigner()!, ) diff --git a/packages/ensjs/src/functions/setRecords.ts b/packages/ensjs/src/functions/setRecords.ts index a02ae6cf..f57e83d2 100644 --- a/packages/ensjs/src/functions/setRecords.ts +++ b/packages/ensjs/src/functions/setRecords.ts @@ -1,3 +1,4 @@ +import type { Signer } from 'ethers' import { ENSArgs } from '..' import { namehash } from '../utils/normalise' import { generateRecordCallArray, RecordOptions } from '../utils/recordHelpers' @@ -10,26 +11,36 @@ export default async function ( }: ENSArgs<'contracts' | 'provider' | 'getResolver'>, name: string, records: RecordOptions, + resolverAddress?: string, + options?: { + signer?: Signer + addressOrIndex?: string | number + }, ) { if (!name.includes('.')) { throw new Error('Input is not an ENS name') } - const resolverAddress = await getResolver(name) + let resolverToUse: string + if (resolverAddress) { + resolverToUse = resolverAddress + } else { + resolverToUse = await getResolver(name) + } - if (!resolverAddress) { + if (!resolverToUse) { throw new Error('No resolver found for input address') } - const address = await provider?.getSigner().getAddress() + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) - if (!address) { + if (!signer) { throw new Error('No signer found') } const resolver = ( - await contracts?.getPublicResolver(provider, resolverAddress) - )?.connect(provider?.getSigner()!) + await contracts?.getPublicResolver(provider, resolverToUse) + )?.connect(signer) const hash = namehash(name) const calls: string[] = generateRecordCallArray(hash, records, resolver!) diff --git a/packages/ensjs/src/functions/setResolver.ts b/packages/ensjs/src/functions/setResolver.ts index 79be9666..8c4a2348 100644 --- a/packages/ensjs/src/functions/setResolver.ts +++ b/packages/ensjs/src/functions/setResolver.ts @@ -1,3 +1,4 @@ +import { Signer } from 'ethers' import { ENSArgs } from '..' import { namehash } from '../utils/normalise' @@ -6,13 +7,11 @@ export default async function ( name: string, contract: 'registry' | 'nameWrapper', resolver?: string, - options?: { addressOrIndex?: string | number }, + options?: { addressOrIndex?: string | number; signer?: Signer }, ) { - const address = await provider - ?.getSigner(options?.addressOrIndex) - .getAddress() + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) - if (!address) { + if (!signer) { throw new Error('No signer found') } diff --git a/packages/ensjs/src/functions/transferName.ts b/packages/ensjs/src/functions/transferName.ts index 28f63f02..63d52acc 100644 --- a/packages/ensjs/src/functions/transferName.ts +++ b/packages/ensjs/src/functions/transferName.ts @@ -1,4 +1,4 @@ -import { ethers } from 'ethers' +import { ethers, Signer } from 'ethers' import { ENSArgs } from '..' import { namehash } from '../utils/normalise' @@ -7,26 +7,24 @@ export default async function ( name: string, newOwner: string, contract: 'registry' | 'nameWrapper' | 'baseRegistrar', - options?: { addressOrIndex?: string | number }, + options?: { addressOrIndex?: string | number; signer?: Signer }, ) { - const address = await provider - ?.getSigner(options?.addressOrIndex) - .getAddress() + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) - if (!address) { + if (!signer) { throw new Error('No signer found') } + const address = await signer.getAddress() + switch (contract) { case 'registry': { - const registry = (await contracts?.getRegistry())!.connect( - provider?.getSigner(options?.addressOrIndex)!, - ) + const registry = (await contracts?.getRegistry())!.connect(signer) return registry.setOwner(namehash(name), newOwner) } case 'baseRegistrar': { const baseRegistrar = (await contracts?.getBaseRegistrar())!.connect( - provider?.getSigner(options?.addressOrIndex)!, + signer, ) const labels = name.split('.') if (labels.length > 2 || labels[labels.length - 1] !== 'eth') { @@ -39,9 +37,7 @@ export default async function ( ) } case 'nameWrapper': { - const nameWrapper = (await contracts?.getNameWrapper())!.connect( - provider?.getSigner(options?.addressOrIndex)!, - ) + const nameWrapper = (await contracts?.getNameWrapper())!.connect(signer) return nameWrapper.safeTransferFrom( address, newOwner, diff --git a/packages/ensjs/src/functions/transferSubname.ts b/packages/ensjs/src/functions/transferSubname.ts index 94c27c23..dc4f1b4e 100644 --- a/packages/ensjs/src/functions/transferSubname.ts +++ b/packages/ensjs/src/functions/transferSubname.ts @@ -1,4 +1,4 @@ -import { ethers } from 'ethers' +import { ethers, Signer } from 'ethers' import { ENSArgs } from '..' import { namehash } from '../utils/normalise' @@ -7,9 +7,9 @@ export default async function ( name: string, contract: 'registry' | 'nameWrapper', address: string, - options?: { addressOrIndex?: string | number }, + options?: { addressOrIndex?: string | number; signer?: Signer }, ) { - const signer = provider?.getSigner(options?.addressOrIndex) + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) if (!signer) { throw new Error('No signer found') diff --git a/packages/ensjs/src/functions/unwrapName.ts b/packages/ensjs/src/functions/unwrapName.ts index 37ad5b46..6e544626 100644 --- a/packages/ensjs/src/functions/unwrapName.ts +++ b/packages/ensjs/src/functions/unwrapName.ts @@ -1,4 +1,4 @@ -import { utils } from 'ethers' +import { Signer, utils } from 'ethers' import { ENSArgs } from '..' import { namehash } from '../utils/normalise' @@ -7,9 +7,9 @@ export default async function ( name: string, newController: string, newRegistrant?: string, - options?: { addressOrIndex?: string | number }, + options?: { addressOrIndex?: string | number; signer?: Signer }, ) { - const signer = provider?.getSigner(options?.addressOrIndex) + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) const address = await signer?.getAddress() diff --git a/packages/ensjs/src/functions/wrapName.test.ts b/packages/ensjs/src/functions/wrapName.test.ts index 2cecd81a..69184ae3 100644 --- a/packages/ensjs/src/functions/wrapName.test.ts +++ b/packages/ensjs/src/functions/wrapName.test.ts @@ -1,8 +1,8 @@ import { BigNumber, ethers } from 'ethers' import { ENS } from '..' +import setup from '../tests/setup' import { hexEncodeName } from '../utils/hexEncodedName' import { namehash } from '../utils/normalise' -import setup from '../tests/setup' let ENSInstance: ENS let revert: Awaited>['revert'] @@ -42,7 +42,7 @@ describe('wrapName', () => { const nameWrapper = await ENSInstance.contracts!.getNameWrapper()! const [result] = await nameWrapper.getFuses(namehash('parthtejpal.eth')) - expect((result as BigNumber).toHexString()).toBe('0x51') + expect((result as BigNumber).toHexString()).toBe('0x40') }) it('should allow an initial resolver address', async () => { const tx = await ENSInstance.wrapName( diff --git a/packages/ensjs/src/functions/wrapName.ts b/packages/ensjs/src/functions/wrapName.ts index 58e8f76b..15b5f7fa 100644 --- a/packages/ensjs/src/functions/wrapName.ts +++ b/packages/ensjs/src/functions/wrapName.ts @@ -1,4 +1,4 @@ -import { ethers } from 'ethers' +import { ethers, Signer } from 'ethers' import { ENSArgs } from '..' import type { FuseOptions } from '../@types/FuseOptions' import generateFuseInput from '../utils/generateFuseInput' @@ -19,8 +19,8 @@ async function wrapETH( const labelhash = ethers.utils.solidityKeccak256(['string'], [labels[0]]) const data = ethers.utils.defaultAbiCoder.encode( - ['string', 'address', 'uint96', 'address'], - [labels[0], wrappedOwner, decodedFuses, resolverAddress], + ['string', 'address', 'uint32', 'uint64', 'address'], + [labels[0], wrappedOwner, '0x0', decodedFuses, resolverAddress], ) return baseRegistrar['safeTransferFrom(address,address,uint256,bytes)']( @@ -67,16 +67,19 @@ export default async function ( wrappedOwner: string, fuseOptions?: FuseOptions | string | number, resolverAddress?: string, - options?: { addressOrIndex?: string | number }, + options?: { + signer?: Signer + addressOrIndex?: string | number + }, ) { - const signer = provider?.getSigner(options?.addressOrIndex) + const signer = options?.signer || provider?.getSigner(options?.addressOrIndex) - const address = await signer?.getAddress() - - if (!signer || !address) { + if (!signer) { throw new Error('No signer found') } + const address = await signer.getAddress() + let decodedFuses: string switch (typeof fuseOptions) {