Skip to content

Commit

Permalink
fix: track legacy deployments, fix nonce calculation, export contract (
Browse files Browse the repository at this point in the history
…#167)

* update imports from ethr-did-registry and bump package

closes #165
closes #166

* track legacy deployments
* fix nonce calculation
* export contract build and deployments array

Co-authored-by: nickreynolds <[email protected]>
  • Loading branch information
mirceanis and nickreynolds authored Sep 6, 2022
1 parent 2737b6d commit c0d0366
Show file tree
Hide file tree
Showing 9 changed files with 839 additions and 77 deletions.
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@
"exports": {
".": {
"require": "./lib/index.cjs",
"import": "./lib/index.module.js"
"import": "./lib/index.modern.js"
}
},
"typesVersions": {
"*": {
"lib/index.d.ts": [
"lib/index.d.ts"
],
"*": [
"./lib/index.d.js"
]
}
},
"repository": {
Expand Down Expand Up @@ -102,7 +112,6 @@
"@ethersproject/providers": "^5.6.8",
"@ethersproject/signing-key": "^5.6.2",
"@ethersproject/transactions": "^5.6.2",
"did-resolver": "^4.0.0",
"ethr-did-registry": "^1.0.0"
"did-resolver": "^4.0.0"
}
}
188 changes: 157 additions & 31 deletions src/__tests__/nonce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import { EthrDidController } from '../controller'
import { createProvider } from './testUtils'
import { arrayify } from '@ethersproject/bytes'
import { SigningKey } from '@ethersproject/signing-key'
import { default as EthereumDIDRegistry } from './EthereumDIDRegistry-Legacy/LegacyEthereumDIDRegistry.json'
import { default as LegacyEthereumDIDRegistry } from './EthereumDIDRegistry-Legacy/LegacyEthereumDIDRegistry.json'
import { default as EthereumDIDRegistry } from '../config/EthereumDIDRegistry.json'

jest.setTimeout(30000)

describe('nonce tracking compatability', () => {
describe('nonce tracking', () => {
// let registry, accounts, did, identity, controller, delegate1, delegate2, ethr, didResolver
let registryContract: Contract,
let legacyRegistryContract: Contract,
registryContract: Contract,
accounts: string[],
did: string,
legacyDid: string,
identity: string,
controller: string,
delegate1: string,
Expand All @@ -24,14 +27,16 @@ describe('nonce tracking compatability', () => {
const web3Provider = createProvider()

beforeAll(async () => {
const factory = ContractFactory.fromSolidity(EthereumDIDRegistry).connect(web3Provider.getSigner(0))

registryContract = await factory.deploy()
registryContract = await registryContract.deployed()
const legacyFactory = ContractFactory.fromSolidity(LegacyEthereumDIDRegistry).connect(web3Provider.getSigner(0))
legacyRegistryContract = await legacyFactory.deploy()
legacyRegistryContract = await legacyRegistryContract.deployed()
await legacyRegistryContract.deployTransaction.wait()
const legacyRegistryAddress = legacyRegistryContract.address

const factory = ContractFactory.fromSolidity(EthereumDIDRegistry).connect(web3Provider.getSigner(0))
registryContract = await (await factory.deploy()).deployed()
await registryContract.deployTransaction.wait()

const registry = registryContract.address
const registryAddress = registryContract.address

accounts = await web3Provider.listAccounts()

Expand All @@ -40,13 +45,20 @@ describe('nonce tracking compatability', () => {
delegate1 = accounts[3]
delegate2 = accounts[4]
keyAgreementController = accounts[5]
legacyDid = `did:ethr:legacy:${identity}`
did = `did:ethr:dev:${identity}`

didResolver = new Resolver(getResolver({ name: 'dev', provider: web3Provider, registry }))
didResolver = new Resolver(
getResolver({
networks: [
{ name: 'legacy', provider: web3Provider, registry: legacyRegistryAddress },
{ name: 'dev', provider: web3Provider, registry: registryAddress },
],
})
)
})

describe('nonce compatability', () => {
it('changing owner two times should result in original owner wallet nonce increase', async () => {
describe('new contract', () => {
it('changing owner two times should result in original owner wallet nonce increase only once', async () => {
// Wallet signing the transaction
const signer = accounts[1]
// Current Owner of the Identity
Expand All @@ -61,12 +73,21 @@ describe('nonce tracking compatability', () => {
const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000002')
const nextOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000003')

console.log(await registryContract.functions.nonce(originalOwner))
const ethrController = new EthrDidController(
identifier,
registryContract,
web3Provider.getSigner(signer),
undefined,
undefined,
undefined,
undefined,
false
)

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

await new EthrDidController(identifier, registryContract, web3Provider.getSigner(signer)).changeOwnerSigned(
await ethrController.changeOwnerSigned(
nextOwner,
{
sigV: signature.v,
Expand All @@ -75,12 +96,10 @@ describe('nonce tracking compatability', () => {
}
)

console.log(await registryContract.functions.nonce(originalOwner))

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

await new EthrDidController(identifier, registryContract, web3Provider.getSigner(signer)).changeOwnerSigned(
await ethrController.changeOwnerSigned(
finalOwner,
{
sigV: signature2.v,
Expand All @@ -89,12 +108,10 @@ describe('nonce tracking compatability', () => {
}
)

console.log(await registryContract.functions.nonce(originalOwner))

const nonce = await registryContract.functions.nonce(originalOwner)
// Expect the nonce of the original identity to equal 2 as the nonce tracking in the legacy contract is
// done on an identity basis
expect(nonce[0]._hex).toEqual('0x02')
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')
})

it('set attribute after owner change should result in original owner wallet nonce increase', async () => {
Expand All @@ -112,10 +129,21 @@ describe('nonce tracking compatability', () => {
const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000005')
const nextOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000006')

const hash = await new EthrDidController(identifier, registryContract).createChangeOwnerHash(nextOwner)
const ethrController = new EthrDidController(
identifier,
registryContract,
web3Provider.getSigner(signer),
undefined,
undefined,
undefined,
undefined,
false
)

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

await new EthrDidController(identifier, registryContract, web3Provider.getSigner(signer)).changeOwnerSigned(
await ethrController.changeOwnerSigned(
nextOwner,
{
sigV: signature.v,
Expand All @@ -124,14 +152,14 @@ describe('nonce tracking compatability', () => {
}
)

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

await new EthrDidController(identifier, registryContract, web3Provider.getSigner(signer)).setAttributeSigned(
await ethrController.setAttributeSigned(
attributeName,
attributeValue,
attributeExpiration,
Expand All @@ -142,7 +170,105 @@ describe('nonce tracking compatability', () => {
}
)

const nonce = await registryContract.functions.nonce(originalOwner)
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')
})
})
describe('legacy contract', () => {
it('changing owner two times should result in original owner wallet nonce increase', async () => {
// Wallet signing the transaction
const signer = accounts[1]
// Current Owner of the Identity
const originalOwner = accounts[2]
// New owner of the Identity after change
const nextOwner = accounts[3]
// Final owner of the Identity
const finalOwner = accounts[4]

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

const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000002')
const nextOwnerPrivateKey = arrayify('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 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,
}
)

// 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')
})

it('set attribute after owner change should result in original owner wallet nonce increase', async () => {
const signer = accounts[1]
const originalOwner = accounts[5]
const nextOwner = accounts[6]

const serviceEndpointParams = { uri: 'https://didcomm.example.com', transportType: 'http' }
const attributeName = 'did/svc/testService'
const attributeValue = JSON.stringify(serviceEndpointParams)
const attributeExpiration = 86400

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

const originalOwnerPrivateKey = arrayify('0x0000000000000000000000000000000000000000000000000000000000000005')
const nextOwnerPrivateKey = arrayify('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 hash2 = await new EthrDidController(identifier, legacyRegistryContract).createSetAttributeHash(
attributeName,
attributeValue,
attributeExpiration
)
const signature2 = new SigningKey(nextOwnerPrivateKey).signDigest(hash2)

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

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

expect(nonce[0]._hex).toEqual('0x02')
})
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Contract, ContractFactory } from '@ethersproject/contracts'
import { Resolvable, Resolver } from 'did-resolver'
import { getResolver } from '../resolver'
import { EthrDidController } from '../controller'
import { EthereumDIDRegistry } from 'ethr-did-registry'
import { default as EthereumDIDRegistry } from '../config/EthereumDIDRegistry.json'
import { interpretIdentifier, nullAddress, stringToBytes32 } from '../helpers'
import { createProvider, sleep, startMining, stopMining } from './testUtils'
import { arrayify } from '@ethersproject/bytes'
Expand Down
Loading

0 comments on commit c0d0366

Please sign in to comment.