|
| 1 | +import { assert } from 'chai' |
| 2 | +import { Account, ContractHandler, Nevermined, didZeroX, generateId, zeroX } from '../../src' |
| 3 | +import { config } from '../config' |
| 4 | +import { Signer, Transaction, ethers } from 'ethers' |
| 5 | +import { sleep } from '../utils/utils' |
| 6 | + |
| 7 | +describe('Lazy registration of assets', () => { |
| 8 | + let publisher: Account |
| 9 | + let relayer: Account |
| 10 | + let publisherSigner: Signer |
| 11 | + let relayerSigner: Signer |
| 12 | + let nevermined: Nevermined |
| 13 | + let didRegistryAbi |
| 14 | + let registryContract: ethers.Contract |
| 15 | + let functionArgs |
| 16 | + |
| 17 | + const functionName = 'registerDID' |
| 18 | + |
| 19 | + const providers = [] |
| 20 | + const metadataUrl = 'http://localhost:5000' |
| 21 | + const activityId = zeroX(generateId()) |
| 22 | + const immutableUrl = '' |
| 23 | + |
| 24 | + before(async () => { |
| 25 | + nevermined = await Nevermined.getInstance(config) |
| 26 | + ;[publisher, relayer] = await nevermined.accounts.list() |
| 27 | + |
| 28 | + publisherSigner = await nevermined.accounts.findSigner(publisher.getId()) |
| 29 | + relayerSigner = await nevermined.accounts.findSigner(relayer.getId()) |
| 30 | + |
| 31 | + const networkName = await nevermined.keeper.getNetworkName() |
| 32 | + didRegistryAbi = await ContractHandler.getABI( |
| 33 | + 'DIDRegistry', |
| 34 | + config.artifactsFolder, |
| 35 | + networkName, |
| 36 | + ) |
| 37 | + registryContract = new ethers.Contract( |
| 38 | + didRegistryAbi.address, |
| 39 | + didRegistryAbi.abi, |
| 40 | + nevermined.web3, |
| 41 | + ) |
| 42 | + registryContract[functionName](didRegistryAbi.address) |
| 43 | + }) |
| 44 | + |
| 45 | + describe('Ethers :: Lazy registration of assets', () => { |
| 46 | + const didSeed = zeroX(generateId()) |
| 47 | + const checksum = zeroX(generateId()) |
| 48 | + let fragment |
| 49 | + let unsignedTx |
| 50 | + let signedTx |
| 51 | + |
| 52 | + it('should be able to generate a tx hash and the signature', async () => { |
| 53 | + registryContract.connect(publisherSigner) |
| 54 | + |
| 55 | + functionArgs = [ |
| 56 | + didSeed, |
| 57 | + checksum, |
| 58 | + providers.map(zeroX), |
| 59 | + metadataUrl, |
| 60 | + activityId, |
| 61 | + immutableUrl, |
| 62 | + ] |
| 63 | + |
| 64 | + fragment = await registryContract[functionName].getFragment(...functionArgs) |
| 65 | + assert.isDefined(fragment) |
| 66 | + |
| 67 | + unsignedTx = await registryContract[functionName].populateTransaction(...functionArgs) |
| 68 | + assert.isDefined(unsignedTx) |
| 69 | + |
| 70 | + console.log(`unsignedTx: `, unsignedTx) |
| 71 | + |
| 72 | + signedTx = await publisherSigner.signTransaction(unsignedTx) |
| 73 | + |
| 74 | + assert.isDefined(signedTx) |
| 75 | + console.log(`signedTx: `, JSON.stringify(signedTx)) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should be able to rely the tx and register on-chain', async () => { |
| 79 | + const tx = Transaction.from(signedTx) |
| 80 | + |
| 81 | + const gasLimit = await registryContract[functionName].estimateGas(...functionArgs, { |
| 82 | + from: relayer.getId(), |
| 83 | + }) |
| 84 | + const feeData = await nevermined.utils.contractHandler.getFeeData() |
| 85 | + |
| 86 | + // const feeData = await nevermined.web3.getFeeData() |
| 87 | + |
| 88 | + tx.chainId = await nevermined.keeper.getNetworkId() |
| 89 | + tx.type = 2 |
| 90 | + tx.nonce = await relayerSigner.getNonce() |
| 91 | + tx.gasLimit = gasLimit |
| 92 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 93 | + if (Object.keys(feeData).includes('gasPrice')) |
| 94 | + tx.gasPrice = Object.values(feeData)['gasPrice']! |
| 95 | + else { |
| 96 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 97 | + tx.maxFeePerGas = Object.values(feeData)['maxFeePerGas']! |
| 98 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 99 | + tx.maxPriorityFeePerGas = Object.values(feeData)['maxPriorityFeePerGas']! |
| 100 | + } |
| 101 | + |
| 102 | + console.log(`Deserialized tx: `, JSON.stringify(tx)) |
| 103 | + |
| 104 | + const txResponse = await nevermined.web3.broadcastTransaction(tx.serialized) |
| 105 | + |
| 106 | + // const txResponse = await provider.sendTransaction(Transaction.from(signedTx)) |
| 107 | + |
| 108 | + assert.isDefined(txResponse) |
| 109 | + |
| 110 | + console.log(`TX Response: `, txResponse) |
| 111 | + await sleep(3000) |
| 112 | + |
| 113 | + // const txResult = await nevermined.web3.getTransactionReceipt(txResponse.hash) |
| 114 | + // console.log(`TX Result: `, JSON.stringify(txResult)) |
| 115 | + }) |
| 116 | + |
| 117 | + it('the asset should be there created correctly', async () => { |
| 118 | + const did = await nevermined.keeper.didRegistry.hashDID(zeroX(didSeed), publisher.getId()) |
| 119 | + |
| 120 | + console.log(`DID: `, did) |
| 121 | + console.log(`DID Seed: `, didSeed) |
| 122 | + |
| 123 | + const assetRegistry = await nevermined.keeper.didRegistry.getAttributesByDid(did) |
| 124 | + |
| 125 | + assert.equal(assetRegistry.did, didZeroX(did)) |
| 126 | + assert.equal(assetRegistry.owner, publisher.getId()) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + describe.skip('Contracts :: Lazy registration of assets', () => { |
| 131 | + const didSeed = `did:nv:${generateId()}` |
| 132 | + const checksum = generateId() |
| 133 | + |
| 134 | + it('should be able to generate a tx hash and the signature', async () => { |
| 135 | + const _did = await nevermined.keeper.didRegistry.hashDID(didSeed, publisher.getId()) |
| 136 | + |
| 137 | + await nevermined.keeper.didRegistry.registerDID( |
| 138 | + didSeed, |
| 139 | + checksum, |
| 140 | + providers, |
| 141 | + publisher.getId(), |
| 142 | + metadataUrl, |
| 143 | + ) |
| 144 | + }) |
| 145 | + |
| 146 | + // it('should be able to rely the tx and register on-chain', async () => { |
| 147 | + |
| 148 | + // }) |
| 149 | + |
| 150 | + // it('the asset should be there created correctly', async () => { |
| 151 | + |
| 152 | + // }) |
| 153 | + }) |
| 154 | +}) |
0 commit comments