|
| 1 | +import { ethers } from 'hardhat'; |
| 2 | + |
| 3 | +import { IoIDRegistry, VerifyingProxy } from '../typechain-types'; |
| 4 | +import { getBytes, keccak256, solidityPacked } from 'ethers'; |
| 5 | + |
| 6 | +async function main() { |
| 7 | + const [verifier] = await ethers.getSigners(); |
| 8 | + const owner = ethers.Wallet.createRandom(); |
| 9 | + |
| 10 | + const network = await ethers.provider.getNetwork(); |
| 11 | + const chainId = Number(network.chainId.toString()); |
| 12 | + const ioIDRegistryFactory = await ethers.getContractFactory('ioIDRegistry'); |
| 13 | + const ioIDRegistry = ioIDRegistryFactory.attach('0x04e4655Cf258EC802D17c23ec6112Ef7d97Fa2aF') as IoIDRegistry; |
| 14 | + const verifyingProxyFactory = await ethers.getContractFactory('VerifyingProxy'); |
| 15 | + const verifyingProxy = verifyingProxyFactory.attach('0xa5c293471ef44625d9ef079296ff4f223405714d') as VerifyingProxy; |
| 16 | + |
| 17 | + const device = ethers.Wallet.createRandom(); |
| 18 | + const domain = { |
| 19 | + name: 'ioIDRegistry', |
| 20 | + version: '1', |
| 21 | + chainId: chainId, |
| 22 | + verifyingContract: ioIDRegistry.target, |
| 23 | + }; |
| 24 | + const types = { |
| 25 | + Permit: [ |
| 26 | + { name: 'owner', type: 'address' }, |
| 27 | + { name: 'nonce', type: 'uint256' }, |
| 28 | + ], |
| 29 | + }; |
| 30 | + |
| 31 | + const nonce = await ioIDRegistry.nonces(device.address); |
| 32 | + |
| 33 | + // @ts-ignore |
| 34 | + const signature = await device.signTypedData(domain, types, { owner: verifyingProxy.target, nonce: nonce }); |
| 35 | + const r = signature.substring(0, 66); |
| 36 | + const s = '0x' + signature.substring(66, 130); |
| 37 | + const v = '0x' + signature.substring(130); |
| 38 | + |
| 39 | + const projectId = await verifyingProxy.projectId(); |
| 40 | + const deviceNFT = await verifyingProxy.deviceNFT(); |
| 41 | + |
| 42 | + const verifyMessage = solidityPacked(['uint256', 'address', 'address'], [chainId, owner.address, device.address]); |
| 43 | + const verifySignature = await verifier.signMessage(getBytes(verifyMessage)); |
| 44 | + |
| 45 | + const tx = await verifyingProxy.register( |
| 46 | + verifySignature, |
| 47 | + keccak256('0x'), // did hash |
| 48 | + 'http://resolver.did', // did document uri |
| 49 | + owner.address, // owner |
| 50 | + device.address, // device |
| 51 | + v, |
| 52 | + r, |
| 53 | + s, |
| 54 | + ); |
| 55 | + |
| 56 | + console.log(`divice nft: ${projectId}`); |
| 57 | + console.log(`divice nft: ${deviceNFT}`); |
| 58 | + console.log(`divice address: ${device.address}`); |
| 59 | + console.log(`owner private key: ${owner.privateKey}`); |
| 60 | + console.log(`register device txHash: ${tx.hash}`); |
| 61 | +} |
| 62 | + |
| 63 | +main().catch(err => { |
| 64 | + console.error(err); |
| 65 | + process.exitCode = 1; |
| 66 | +}); |
0 commit comments