Skip to content

Commit

Permalink
add signer option to all write transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jul 12, 2022
1 parent 3202169 commit ec19cb7
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 50 deletions.
4 changes: 3 additions & 1 deletion packages/ensjs/src/functions/burnFuses.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Signer } from 'ethers'
import { ENSArgs } from '..'
import { FuseOptions } from '../@types/FuseOptions'
import generateFuseInput from '../utils/generateFuseInput'
Expand All @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions packages/ensjs/src/functions/createSubname.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 = {
Expand All @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion packages/ensjs/src/functions/deleteSubname.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Signer } from 'ethers'
import { ENSArgs } from '..'

export default async function (
Expand All @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions packages/ensjs/src/functions/setName.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Signer } from 'ethers'
import { ENSArgs } from '..'

export default async function (
{ contracts, provider }: ENSArgs<'contracts' | 'provider'>,
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()!,
)
Expand Down
23 changes: 17 additions & 6 deletions packages/ensjs/src/functions/setRecords.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Signer } from 'ethers'
import { ENSArgs } from '..'
import { namehash } from '../utils/normalise'
import { generateRecordCallArray, RecordOptions } from '../utils/recordHelpers'
Expand All @@ -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!)
Expand Down
9 changes: 4 additions & 5 deletions packages/ensjs/src/functions/setResolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Signer } from 'ethers'
import { ENSArgs } from '..'
import { namehash } from '../utils/normalise'

Expand All @@ -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')
}

Expand Down
22 changes: 9 additions & 13 deletions packages/ensjs/src/functions/transferName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers'
import { ethers, Signer } from 'ethers'
import { ENSArgs } from '..'
import { namehash } from '../utils/normalise'

Expand All @@ -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') {
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/ensjs/src/functions/transferSubname.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers'
import { ethers, Signer } from 'ethers'
import { ENSArgs } from '..'
import { namehash } from '../utils/normalise'

Expand All @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions packages/ensjs/src/functions/unwrapName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils } from 'ethers'
import { Signer, utils } from 'ethers'
import { ENSArgs } from '..'
import { namehash } from '../utils/normalise'

Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions packages/ensjs/src/functions/wrapName.test.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof setup>>['revert']
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 11 additions & 8 deletions packages/ensjs/src/functions/wrapName.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)'](
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ec19cb7

Please sign in to comment.