Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/update ethers #188

Merged
merged 11 commits into from
Sep 27, 2023
15 changes: 2 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
"@babel/core": "7.20.2",
"@babel/preset-env": "7.20.2",
"@babel/preset-typescript": "7.18.6",
"@ethersproject/strings": "5.7.0",
"@semantic-release/changelog": "6.0.1",
"@semantic-release/git": "10.0.1",
"@types/jest": "29.2.2",
Expand All @@ -102,17 +101,7 @@
"typescript": "4.8.4"
},
"dependencies": {
"@ethersproject/abi": "^5.6.3",
"@ethersproject/abstract-signer": "^5.6.2",
"@ethersproject/address": "^5.6.1",
"@ethersproject/basex": "^5.6.1",
"@ethersproject/bignumber": "^5.6.2",
"@ethersproject/bytes": "^5.6.1",
"@ethersproject/contracts": "^5.6.2",
"@ethersproject/keccak256": "^5.6.1",
"@ethersproject/providers": "^5.6.8",
"@ethersproject/signing-key": "^5.6.2",
"@ethersproject/transactions": "^5.6.2",
"did-resolver": "^4.0.1"
"did-resolver": "^4.0.1",
"ethers": "6.7.1"
}
}
8 changes: 4 additions & 4 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InfuraProvider, JsonRpcProvider } from '@ethersproject/providers'
import { InfuraProvider, JsonRpcProvider } from 'ethers'
import { configureResolverWithNetworks } from '../configuration'

describe('configuration', () => {
Expand Down Expand Up @@ -27,8 +27,8 @@ describe('configuration', () => {
infuraProjectId: 'blabla',
networks: [{ name: 'mainnet', rpcUrl: 'redefine me' }],
})
expect((<InfuraProvider>contracts['mainnet'].provider).projectId).not.toBeDefined()
expect((<JsonRpcProvider>contracts['mainnet'].provider).connection.url).toBe('redefine me')
expect((<InfuraProvider>contracts['mainnet'].runner!.provider).projectId).not.toBeDefined()
expect((<JsonRpcProvider>contracts['mainnet'].runner!.provider)._getConnection().url).toBe('redefine me')
})

it('works with named network', async () => {
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('configuration', () => {
it('works with rpc and numbered chainId', async () => {
const contracts = configureResolverWithNetworks({
rpcUrl: 'some rinkeby JSONRPC URL',
chainId: 1,
chainId: BigInt(1),
})
expect(contracts['0x1']).toBeDefined()
})
Expand Down
153 changes: 78 additions & 75 deletions src/__tests__/nonce.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Contract, ContractFactory } from '@ethersproject/contracts'
import { BrowserProvider, Contract, ContractFactory, getBytes, SigningKey } from 'ethers'
import { Resolvable, Resolver } from 'did-resolver'
import { getResolver } from '../resolver'
import { EthrDidController } from '../controller'
import { createProvider } from './testUtils'
import { arrayify } from '@ethersproject/bytes'
import { SigningKey } from '@ethersproject/signing-key'
import { default as LegacyEthereumDIDRegistry } from './EthereumDIDRegistry-Legacy/LegacyEthereumDIDRegistry.json'
import { default as EthereumDIDRegistry } from '../config/EthereumDIDRegistry.json'

Expand All @@ -24,21 +22,23 @@ describe('nonce tracking', () => {
keyAgreementController: string,
didResolver: Resolvable

const web3Provider = createProvider()
let browserProvider: BrowserProvider

beforeAll(async () => {
const legacyFactory = ContractFactory.fromSolidity(LegacyEthereumDIDRegistry).connect(web3Provider.getSigner(0))
browserProvider = createProvider()
const legacyFactory = ContractFactory.fromSolidity(LegacyEthereumDIDRegistry).connect(
await browserProvider.getSigner(0)
)
legacyRegistryContract = await legacyFactory.deploy()
legacyRegistryContract = await legacyRegistryContract.deployed()
await legacyRegistryContract.deployTransaction.wait()
const legacyRegistryAddress = legacyRegistryContract.address
legacyRegistryContract = await legacyRegistryContract.waitForDeployment()
const legacyRegistryAddress = await legacyRegistryContract.getAddress()

const factory = ContractFactory.fromSolidity(EthereumDIDRegistry).connect(web3Provider.getSigner(0))
registryContract = await (await factory.deploy()).deployed()
await registryContract.deployTransaction.wait()
const registryAddress = registryContract.address
const factory = ContractFactory.fromSolidity(EthereumDIDRegistry).connect(await browserProvider.getSigner(0))
registryContract = await (await factory.deploy()).waitForDeployment()
const registryAddress = await registryContract.getAddress()

accounts = await web3Provider.listAccounts()
const accountSigners = await browserProvider.listAccounts()
accounts = accountSigners.map((signer) => signer.address)

identity = accounts[1]
controller = accounts[2]
Expand All @@ -51,8 +51,8 @@ describe('nonce tracking', () => {
didResolver = new Resolver(
getResolver({
networks: [
{ name: 'legacy', provider: web3Provider, registry: legacyRegistryAddress },
{ name: 'dev', provider: web3Provider, registry: registryAddress },
{ name: 'legacy', provider: browserProvider, registry: legacyRegistryAddress },
{ name: 'dev', provider: browserProvider, registry: registryAddress },
],
})
)
Expand All @@ -70,13 +70,13 @@ describe('nonce tracking', () => {

const identifier = `did:ethr:dev:${originalOwner}`

const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000002')
const nextOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000003')
const originalOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000002')
const nextOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000003')

const ethrController = new EthrDidController(
identifier,
registryContract,
web3Provider.getSigner(signer),
await browserProvider.getSigner(signer),
undefined,
undefined,
undefined,
Expand All @@ -85,7 +85,7 @@ describe('nonce tracking', () => {
)

const hash = await ethrController.createChangeOwnerHash(nextOwner)
const signature = new SigningKey(originalOwnerPrivateKey).signDigest(hash)
const signature = new SigningKey(originalOwnerPrivateKey).sign(hash)

await ethrController.changeOwnerSigned(nextOwner, {
sigV: signature.v,
Expand All @@ -94,18 +94,18 @@ describe('nonce tracking', () => {
})

const hash2 = await ethrController.createChangeOwnerHash(finalOwner)
const signature2 = new SigningKey(nextOwnerPrivateKey).signDigest(hash2)
const signature2 = new SigningKey(nextOwnerPrivateKey).sign(hash2)

await ethrController.changeOwnerSigned(finalOwner, {
sigV: signature2.v,
sigR: signature2.r,
sigS: signature2.s,
})

const originalNonce = await registryContract.functions.nonce(originalOwner)
const signerNonce = await registryContract.functions.nonce(nextOwner)
expect(originalNonce[0]._hex).toEqual('0x01')
expect(signerNonce[0]._hex).toEqual('0x01')
const originalNonce: bigint = await registryContract.nonce(originalOwner)
const signerNonce: bigint = await registryContract.nonce(nextOwner)
expect(originalNonce).toEqual(1n)
expect(signerNonce).toEqual(1n)
})

it('set attribute after owner change should result in original owner wallet nonce increase', async () => {
Expand All @@ -120,13 +120,13 @@ describe('nonce tracking', () => {

const identifier = `did:ethr:dev:${originalOwner}`

const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000005')
const nextOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000006')
const originalOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000005')
const nextOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000006')

const ethrController = new EthrDidController(
identifier,
registryContract,
web3Provider.getSigner(signer),
await browserProvider.getSigner(signer),
undefined,
undefined,
undefined,
Expand All @@ -135,7 +135,7 @@ describe('nonce tracking', () => {
)

const hash = await ethrController.createChangeOwnerHash(nextOwner)
const signature = new SigningKey(originalOwnerPrivateKey).signDigest(hash)
const signature = new SigningKey(originalOwnerPrivateKey).sign(hash)

await ethrController.changeOwnerSigned(nextOwner, {
sigV: signature.v,
Expand All @@ -144,18 +144,18 @@ describe('nonce tracking', () => {
})

const hash2 = await ethrController.createSetAttributeHash(attributeName, attributeValue, attributeExpiration)
const signature2 = new SigningKey(nextOwnerPrivateKey).signDigest(hash2)
const signature2 = new SigningKey(nextOwnerPrivateKey).sign(hash2)

await ethrController.setAttributeSigned(attributeName, attributeValue, attributeExpiration, {
sigV: signature2.v,
sigR: signature2.r,
sigS: signature2.s,
})

const originalNonce = await registryContract.functions.nonce(originalOwner)
const signerNonce = await registryContract.functions.nonce(nextOwner)
expect(originalNonce[0]._hex).toEqual('0x01')
expect(signerNonce[0]._hex).toEqual('0x01')
const originalNonce = await registryContract.nonce(originalOwner)
const signerNonce = await registryContract.nonce(nextOwner)
expect(originalNonce).toEqual(1n)
expect(signerNonce).toEqual(1n)
})
})
describe('legacy contract', () => {
Expand All @@ -171,39 +171,41 @@ describe('nonce tracking', () => {

const identifier = `did:ethr:legacy:${originalOwner}`

const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000002')
const nextOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000003')
const originalOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000002')
const nextOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000003')

const hash = await new EthrDidController(identifier, legacyRegistryContract).createChangeOwnerHash(nextOwner)
const signature = new SigningKey(originalOwnerPrivateKey).signDigest(hash)

await new EthrDidController(identifier, legacyRegistryContract, web3Provider.getSigner(signer)).changeOwnerSigned(
nextOwner,
{
sigV: signature.v,
sigR: signature.r,
sigS: signature.s,
}
)
const signature = new SigningKey(originalOwnerPrivateKey).sign(hash)

await new EthrDidController(
identifier,
legacyRegistryContract,
await browserProvider.getSigner(signer)
).changeOwnerSigned(nextOwner, {
sigV: signature.v,
sigR: signature.r,
sigS: signature.s,
})

const hash2 = await new EthrDidController(identifier, legacyRegistryContract).createChangeOwnerHash(finalOwner)
const signature2 = new SigningKey(nextOwnerPrivateKey).signDigest(hash2)

await new EthrDidController(identifier, legacyRegistryContract, web3Provider.getSigner(signer)).changeOwnerSigned(
finalOwner,
{
sigV: signature2.v,
sigR: signature2.r,
sigS: signature2.s,
}
)
const signature2 = new SigningKey(nextOwnerPrivateKey).sign(hash2)

await new EthrDidController(
identifier,
legacyRegistryContract,
await browserProvider.getSigner(signer)
).changeOwnerSigned(finalOwner, {
sigV: signature2.v,
sigR: signature2.r,
sigS: signature2.s,
})

// Expect the nonce of the original identity to equal 2 as the nonce tracking in the legacy contract is
// done on an identity basis
const originalNonce = await legacyRegistryContract.functions.nonce(originalOwner)
const signerNonce = await legacyRegistryContract.functions.nonce(nextOwner)
expect(originalNonce[0]._hex).toEqual('0x02')
expect(signerNonce[0]._hex).toEqual('0x00')
const originalNonce = await legacyRegistryContract.nonce(originalOwner)
const signerNonce = await legacyRegistryContract.nonce(nextOwner)
expect(originalNonce).toEqual(2n)
expect(signerNonce).toEqual(0n)
})

it('set attribute after owner change should result in original owner wallet nonce increase', async () => {
Expand All @@ -218,41 +220,42 @@ describe('nonce tracking', () => {

const identifier = `did:ethr:legacy:${originalOwner}`

const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000005')
const nextOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000006')
const originalOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000005')
const nextOwnerPrivateKey = getBytes('0x0000000000000000000000000000000000000000000000000000000000000006')

const hash = await new EthrDidController(identifier, legacyRegistryContract).createChangeOwnerHash(nextOwner)
const signature = new SigningKey(originalOwnerPrivateKey).signDigest(hash)

await new EthrDidController(identifier, legacyRegistryContract, web3Provider.getSigner(signer)).changeOwnerSigned(
nextOwner,
{
sigV: signature.v,
sigR: signature.r,
sigS: signature.s,
}
)
const signature = new SigningKey(originalOwnerPrivateKey).sign(hash)

await new EthrDidController(
identifier,
legacyRegistryContract,
await browserProvider.getSigner(signer)
).changeOwnerSigned(nextOwner, {
sigV: signature.v,
sigR: signature.r,
sigS: signature.s,
})

const hash2 = await new EthrDidController(identifier, legacyRegistryContract).createSetAttributeHash(
attributeName,
attributeValue,
attributeExpiration
)
const signature2 = new SigningKey(nextOwnerPrivateKey).signDigest(hash2)
const signature2 = new SigningKey(nextOwnerPrivateKey).sign(hash2)

await new EthrDidController(
identifier,
legacyRegistryContract,
web3Provider.getSigner(signer)
await browserProvider.getSigner(signer)
).setAttributeSigned(attributeName, attributeValue, attributeExpiration, {
sigV: signature2.v,
sigR: signature2.r,
sigS: signature2.s,
})

const nonce = await legacyRegistryContract.functions.nonce(originalOwner)
const nonce = await legacyRegistryContract.nonce(originalOwner)

expect(nonce[0]._hex).toEqual('0x02')
expect(nonce).toEqual(2n)
})
})
})
Loading
Loading