Skip to content

Commit

Permalink
adding opengsn test
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsmkl committed Oct 13, 2022
1 parent 0ac11fc commit e1d140b
Show file tree
Hide file tree
Showing 6 changed files with 3,455 additions and 62 deletions.
5 changes: 5 additions & 0 deletions contracts/governance/NeverminedConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ INVMConfig
return trustedForwarder;
}

function setTrustedForwarder(address forwarder)
external onlyGovernor(msg.sender) {
trustedForwarder = forwarder;
}

modifier onlyGovernor(address _address)
{
require(
Expand Down
4 changes: 4 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ module.exports = {
url: 'http://localhost:18545',
timeout: 200000
},
local: {
url: 'http://localhost:8545',
timeout: 200000
},
'polygon-localnet': {
url: url || 'http://localhost:8545',
accounts,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
"@nomiclabs/hardhat-truffle5": "^2.0.6",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@nomiclabs/hardhat-etherscan": "^3.1.0",
"@opengsn/dev": "^2.2.6",
"@opengsn/provider": "^2.2.6",
"@openzeppelin/hardhat-upgrades": "^1.19.0",
"hardhat": "^2.10.1",
"hardhat-dependency-compiler": "^1.1.3",
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ const constants = {
url: 'https://example.com/did/nevermined/test-attr-example.txt'
},
activities: {
GENERATED: '0x1',
USED: '0x2'
GENERATED: '0x0000000000000000000000000000000000000000000000000000000000000001',
USED: '0x02'
}
}

Expand Down
116 changes: 116 additions & 0 deletions test/unit/registry/OpenGNS.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const { RelayProvider } = require('@opengsn/provider')
const { GsnTestEnvironment } = require('@opengsn/dev' )
const { ethers } = require('hardhat')
const { it, describe, before } = require('mocha')
const { assert } = require('chai')
const constants = require('../../helpers/constants.js')
const testUtils = require('../../helpers/utils.js')
const Web3HttpProvider = require('web3-providers-http')

async function deployLibrary(name, signer) {
const factory = await ethers.getContractFactory(name, signer)
const library = await factory.deploy()
const h1 = library.deployTransaction.hash
await library.deployed()
const address = (await web3.eth.getTransactionReceipt(h1)).contractAddress
console.log(`Library ${name} deployed into address ${address}`)
return address
}

async function deployContract(contract, deployer, libraries, args) {
const C = await ethers.getContractFactory(contract, { libraries })
const signer = C.connect(deployer)
const c = await signer.deploy()
await c.deployed()
const tx = await c.initialize(...args)
await tx.wait()
return c
}

describe('using ethers with OpenGSN', () => {
let didRegistry, nft
let accounts
// let web3provider
const value = 'https://nevermined.io/did/nevermined/test-attr-example.txt'
const nftMetadataURL = 'https://nevermined.io/metadata.json'
let account
before(async () => {
let env = await GsnTestEnvironment.startGsn('localhost')

const { paymasterAddress, forwarderAddress } = env.contractsDeployment

const web3provider = new Web3HttpProvider('http://localhost:8545')

const deploymentProvider = new ethers.providers.Web3Provider(web3provider)
const deployer = await deploymentProvider.getSigner(8)

accounts = await web3.eth.getAccounts()
const owner = accounts[0]
const governor = accounts[1]
// const owner = accounts[0]

const didlib = await deployLibrary('DIDRegistryLibrary', deployer)

const nvmConfig = await deployContract('NeverminedConfig', deployer, {}, [owner, governor, false])

nft = await deployContract('NFTUpgradeable', deployer, {}, [''])

didRegistry = await deployContract(
'DIDRegistry',
deployer,
{DIDRegistryLibrary: didlib},
[owner, nft.address, constants.address.zero, nvmConfig.address, constants.address.zero]
)

await nft.connect(deployer).addMinter(didRegistry.address)
await nvmConfig.connect(await deploymentProvider.getSigner(governor)).setTrustedForwarder(forwarderAddress)


const config = await {
// loggerConfiguration: { logLevel: 'error'},
paymasterAddress: paymasterAddress,
auditorsCount: 0
}
// const hdweb3provider = new HDWallet('0x123456', 'http://localhost:8545')
let gsnProvider = RelayProvider.newProvider({provider: web3provider, config})
await gsnProvider.init()
// The above is the full provider configuration. can use the provider returned by startGsn:
// const gsnProvider = env.relayProvider

const wallet = new ethers.Wallet(Buffer.from('1'.repeat(64),'hex'))
gsnProvider.addAccount(wallet.privateKey)
account = wallet.address

// gsnProvider is now an rpc provider with GSN support. make it an ethers provider:
const etherProvider = new ethers.providers.Web3Provider(gsnProvider)

didRegistry = didRegistry.connect(etherProvider.getSigner(account))
})

describe('Register an Asset with a DID', () => {

it('Should mint and burn NFTs after initialization', async () => {
const didSeed = testUtils.generateId()
const did = await didRegistry.hashDID(didSeed, account)
const checksum = testUtils.generateId()

await didRegistry['registerMintableDID(bytes32,bytes32,address[],string,uint256,uint256,bytes32,string)'](
didSeed, checksum, [], value, 20, 0, constants.activities.GENERATED, nftMetadataURL, { from: account })
await didRegistry['mint(bytes32,uint256)'](did, 20, { from: account })

let balance = await nft.balanceOf(account, did)
assert.strictEqual(20, balance.toNumber())

await didRegistry.burn(did, 5, { from: account })

balance = await nft.balanceOf(account, did)
assert.strictEqual(15, balance.toNumber())

const _nftURI = await nft.uri(did)
assert.strictEqual(nftMetadataURL, _nftURI)
})

})

})

0 comments on commit e1d140b

Please sign in to comment.