@arcblock/erc721did-contract 0.2.6 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,155 +0,0 @@
1
- {
2
- "_format": "hh-sol-artifact-1",
3
- "contractName": "ERC721DIDFactory",
4
- "sourceName": "src/ERC721DIDFactory.sol",
5
- "abi": [
6
- {
7
- "inputs": [
8
- {
9
- "internalType": "address",
10
- "name": "_implementation",
11
- "type": "address"
12
- }
13
- ],
14
- "stateMutability": "nonpayable",
15
- "type": "constructor"
16
- },
17
- {
18
- "anonymous": false,
19
- "inputs": [
20
- {
21
- "indexed": false,
22
- "internalType": "address",
23
- "name": "contractAddress",
24
- "type": "address"
25
- }
26
- ],
27
- "name": "Created",
28
- "type": "event"
29
- },
30
- {
31
- "anonymous": false,
32
- "inputs": [
33
- {
34
- "indexed": true,
35
- "internalType": "address",
36
- "name": "previousOwner",
37
- "type": "address"
38
- },
39
- {
40
- "indexed": true,
41
- "internalType": "address",
42
- "name": "newOwner",
43
- "type": "address"
44
- }
45
- ],
46
- "name": "OwnershipTransferred",
47
- "type": "event"
48
- },
49
- {
50
- "stateMutability": "nonpayable",
51
- "type": "fallback"
52
- },
53
- {
54
- "inputs": [
55
- {
56
- "internalType": "address",
57
- "name": "_proxyRegistryAddress",
58
- "type": "address"
59
- },
60
- {
61
- "internalType": "string",
62
- "name": "_baseImageURI",
63
- "type": "string"
64
- },
65
- {
66
- "internalType": "string",
67
- "name": "_contractName",
68
- "type": "string"
69
- },
70
- {
71
- "internalType": "string",
72
- "name": "_contractSymbol",
73
- "type": "string"
74
- },
75
- {
76
- "internalType": "string",
77
- "name": "_contractDescription",
78
- "type": "string"
79
- },
80
- {
81
- "internalType": "uint256",
82
- "name": "_totalLimit",
83
- "type": "uint256"
84
- }
85
- ],
86
- "name": "create",
87
- "outputs": [],
88
- "stateMutability": "nonpayable",
89
- "type": "function"
90
- },
91
- {
92
- "inputs": [],
93
- "name": "implementation",
94
- "outputs": [
95
- {
96
- "internalType": "address",
97
- "name": "",
98
- "type": "address"
99
- }
100
- ],
101
- "stateMutability": "view",
102
- "type": "function"
103
- },
104
- {
105
- "inputs": [],
106
- "name": "owner",
107
- "outputs": [
108
- {
109
- "internalType": "address",
110
- "name": "",
111
- "type": "address"
112
- }
113
- ],
114
- "stateMutability": "view",
115
- "type": "function"
116
- },
117
- {
118
- "inputs": [],
119
- "name": "renounceOwnership",
120
- "outputs": [],
121
- "stateMutability": "nonpayable",
122
- "type": "function"
123
- },
124
- {
125
- "inputs": [
126
- {
127
- "internalType": "address",
128
- "name": "_implementation",
129
- "type": "address"
130
- }
131
- ],
132
- "name": "setImplementation",
133
- "outputs": [],
134
- "stateMutability": "nonpayable",
135
- "type": "function"
136
- },
137
- {
138
- "inputs": [
139
- {
140
- "internalType": "address",
141
- "name": "newOwner",
142
- "type": "address"
143
- }
144
- ],
145
- "name": "transferOwnership",
146
- "outputs": [],
147
- "stateMutability": "nonpayable",
148
- "type": "function"
149
- }
150
- ],
151
- "bytecode": "0x608060405234801561001057600080fd5b50604051610d7d380380610d7d83398181016040528101906100329190610176565b61004e61004361009560201b60201c565b61009d60201b60201c565b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550506101e8565b600033905090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b600081519050610170816101d1565b92915050565b60006020828403121561018857600080fd5b600061019684828501610161565b91505092915050565b60006101aa826101b1565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6101da8161019f565b81146101e557600080fd5b50565b610b86806101f76000396000f3fe608060405234801561001057600080fd5b50600436106100665760003560e01c80633040090b146100a25780635c60da1b146100be578063715018a6146100dc5780638da5cb5b146100e6578063d784d42614610104578063f2fde38b1461012057610067565b5b6040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610099906108d7565b60405180910390fd5b6100bc60048036038101906100b79190610613565b61013c565b005b6100c6610220565b6040516100d391906107df565b60405180910390f35b6100e4610246565b005b6100ee61025a565b6040516100fb91906107df565b60405180910390f35b61011e600480360381019061011991906105ea565b610283565b005b61013a600480360381019061013591906105ea565b6102cf565b005b6000610169600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16610353565b90508073ffffffffffffffffffffffffffffffffffffffff1663fe9c9ff18888888888886040518763ffffffff1660e01b81526004016101ae969594939291906107fa565b600060405180830381600087803b1580156101c857600080fd5b505af11580156101dc573d6000803e3d6000fd5b505050507f1449abf21e49fd025f33495e77f7b1461caefdd3d4bb646424a3f445c4576a5b8160405161020f91906107df565b60405180910390a150505050505050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b61024e61040e565b610258600061048c565b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b61028b61040e565b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6102d761040e565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415610347576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161033e90610877565b60405180910390fd5b6103508161048c565b50565b6000763d602d80600a3d3981f3363d3d373d3d3d363d730000008260601b60e81c176000526e5af43d82803e903d91602b57fd5bf38260781b17602052603760096000f09050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415610409576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161040090610897565b60405180910390fd5b919050565b610416610550565b73ffffffffffffffffffffffffffffffffffffffff1661043461025a565b73ffffffffffffffffffffffffffffffffffffffff161461048a576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610481906108b7565b60405180910390fd5b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b600033905090565b600061056b6105668461091c565b6108f7565b90508281526020810184848401111561058357600080fd5b61058e8482856109a5565b509392505050565b6000813590506105a581610b22565b92915050565b600082601f8301126105bc57600080fd5b81356105cc848260208601610558565b91505092915050565b6000813590506105e481610b39565b92915050565b6000602082840312156105fc57600080fd5b600061060a84828501610596565b91505092915050565b60008060008060008060c0878903121561062c57600080fd5b600061063a89828a01610596565b965050602087013567ffffffffffffffff81111561065757600080fd5b61066389828a016105ab565b955050604087013567ffffffffffffffff81111561068057600080fd5b61068c89828a016105ab565b945050606087013567ffffffffffffffff8111156106a957600080fd5b6106b589828a016105ab565b935050608087013567ffffffffffffffff8111156106d257600080fd5b6106de89828a016105ab565b92505060a06106ef89828a016105d5565b9150509295509295509295565b61070581610969565b82525050565b60006107168261094d565b6107208185610958565b93506107308185602086016109b4565b61073981610a47565b840191505092915050565b6000610751602683610958565b915061075c82610a58565b604082019050919050565b6000610774601683610958565b915061077f82610aa7565b602082019050919050565b6000610797602083610958565b91506107a282610ad0565b602082019050919050565b60006107ba600f83610958565b91506107c582610af9565b602082019050919050565b6107d98161099b565b82525050565b60006020820190506107f460008301846106fc565b92915050565b600060c08201905061080f60008301896106fc565b8181036020830152610821818861070b565b90508181036040830152610835818761070b565b90508181036060830152610849818661070b565b9050818103608083015261085d818561070b565b905061086c60a08301846107d0565b979650505050505050565b6000602082019050818103600083015261089081610744565b9050919050565b600060208201905081810360008301526108b081610767565b9050919050565b600060208201905081810360008301526108d08161078a565b9050919050565b600060208201905081810360008301526108f0816107ad565b9050919050565b6000610901610912565b905061090d82826109e7565b919050565b6000604051905090565b600067ffffffffffffffff82111561093757610936610a18565b5b61094082610a47565b9050602081019050919050565b600081519050919050565b600082825260208201905092915050565b60006109748261097b565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b82818337600083830152505050565b60005b838110156109d25780820151818401526020810190506109b7565b838111156109e1576000848401525b50505050565b6109f082610a47565b810181811067ffffffffffffffff82111715610a0f57610a0e610a18565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f8301169050919050565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160008201527f6464726573730000000000000000000000000000000000000000000000000000602082015250565b7f455243313136373a20637265617465206661696c656400000000000000000000600082015250565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572600082015250565b7f556e6b6e6f772066756e6374696f6e0000000000000000000000000000000000600082015250565b610b2b81610969565b8114610b3657600080fd5b50565b610b428161099b565b8114610b4d57600080fd5b5056fea2646970667358221220b5fa8967ae0dd37c26ae927cac9ffccafe881cbd1273bb80536ba6e37309d4cf64736f6c63430008040033",
152
- "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100665760003560e01c80633040090b146100a25780635c60da1b146100be578063715018a6146100dc5780638da5cb5b146100e6578063d784d42614610104578063f2fde38b1461012057610067565b5b6040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610099906108d7565b60405180910390fd5b6100bc60048036038101906100b79190610613565b61013c565b005b6100c6610220565b6040516100d391906107df565b60405180910390f35b6100e4610246565b005b6100ee61025a565b6040516100fb91906107df565b60405180910390f35b61011e600480360381019061011991906105ea565b610283565b005b61013a600480360381019061013591906105ea565b6102cf565b005b6000610169600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16610353565b90508073ffffffffffffffffffffffffffffffffffffffff1663fe9c9ff18888888888886040518763ffffffff1660e01b81526004016101ae969594939291906107fa565b600060405180830381600087803b1580156101c857600080fd5b505af11580156101dc573d6000803e3d6000fd5b505050507f1449abf21e49fd025f33495e77f7b1461caefdd3d4bb646424a3f445c4576a5b8160405161020f91906107df565b60405180910390a150505050505050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b61024e61040e565b610258600061048c565b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b61028b61040e565b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6102d761040e565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415610347576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161033e90610877565b60405180910390fd5b6103508161048c565b50565b6000763d602d80600a3d3981f3363d3d373d3d3d363d730000008260601b60e81c176000526e5af43d82803e903d91602b57fd5bf38260781b17602052603760096000f09050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415610409576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161040090610897565b60405180910390fd5b919050565b610416610550565b73ffffffffffffffffffffffffffffffffffffffff1661043461025a565b73ffffffffffffffffffffffffffffffffffffffff161461048a576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610481906108b7565b60405180910390fd5b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b600033905090565b600061056b6105668461091c565b6108f7565b90508281526020810184848401111561058357600080fd5b61058e8482856109a5565b509392505050565b6000813590506105a581610b22565b92915050565b600082601f8301126105bc57600080fd5b81356105cc848260208601610558565b91505092915050565b6000813590506105e481610b39565b92915050565b6000602082840312156105fc57600080fd5b600061060a84828501610596565b91505092915050565b60008060008060008060c0878903121561062c57600080fd5b600061063a89828a01610596565b965050602087013567ffffffffffffffff81111561065757600080fd5b61066389828a016105ab565b955050604087013567ffffffffffffffff81111561068057600080fd5b61068c89828a016105ab565b945050606087013567ffffffffffffffff8111156106a957600080fd5b6106b589828a016105ab565b935050608087013567ffffffffffffffff8111156106d257600080fd5b6106de89828a016105ab565b92505060a06106ef89828a016105d5565b9150509295509295509295565b61070581610969565b82525050565b60006107168261094d565b6107208185610958565b93506107308185602086016109b4565b61073981610a47565b840191505092915050565b6000610751602683610958565b915061075c82610a58565b604082019050919050565b6000610774601683610958565b915061077f82610aa7565b602082019050919050565b6000610797602083610958565b91506107a282610ad0565b602082019050919050565b60006107ba600f83610958565b91506107c582610af9565b602082019050919050565b6107d98161099b565b82525050565b60006020820190506107f460008301846106fc565b92915050565b600060c08201905061080f60008301896106fc565b8181036020830152610821818861070b565b90508181036040830152610835818761070b565b90508181036060830152610849818661070b565b9050818103608083015261085d818561070b565b905061086c60a08301846107d0565b979650505050505050565b6000602082019050818103600083015261089081610744565b9050919050565b600060208201905081810360008301526108b081610767565b9050919050565b600060208201905081810360008301526108d08161078a565b9050919050565b600060208201905081810360008301526108f0816107ad565b9050919050565b6000610901610912565b905061090d82826109e7565b919050565b6000604051905090565b600067ffffffffffffffff82111561093757610936610a18565b5b61094082610a47565b9050602081019050919050565b600081519050919050565b600082825260208201905092915050565b60006109748261097b565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b82818337600083830152505050565b60005b838110156109d25780820151818401526020810190506109b7565b838111156109e1576000848401525b50505050565b6109f082610a47565b810181811067ffffffffffffffff82111715610a0f57610a0e610a18565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f8301169050919050565b7f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160008201527f6464726573730000000000000000000000000000000000000000000000000000602082015250565b7f455243313136373a20637265617465206661696c656400000000000000000000600082015250565b7f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572600082015250565b7f556e6b6e6f772066756e6374696f6e0000000000000000000000000000000000600082015250565b610b2b81610969565b8114610b3657600080fd5b50565b610b428161099b565b8114610b4d57600080fd5b5056fea2646970667358221220b5fa8967ae0dd37c26ae927cac9ffccafe881cbd1273bb80536ba6e37309d4cf64736f6c63430008040033",
153
- "linkReferences": {},
154
- "deployedLinkReferences": {}
155
- }
package/lib/contract.js DELETED
@@ -1,206 +0,0 @@
1
- /* eslint-disable no-unused-vars */
2
- /* eslint-disable no-undef */
3
- require('dotenv-flow').config();
4
- const ethers = require('ethers');
5
- const axios = require('axios');
6
- const waitFor = require('p-wait-for');
7
- const { toBase58, toBN } = require('@ocap/util');
8
- const upperFirst = require('lodash/upperFirst');
9
- const ERC721DID = require('./ERC721DID.json');
10
- const ERC721DIDFactory = require('./ERC721DIDFactory.json');
11
-
12
- const { INFURA_PROJECT_ID } = process.env;
13
-
14
- function getDefaultProvider(isLocal = false) {
15
- return isLocal
16
- ? new ethers.providers.JsonRpcProvider('http://localhost:8545')
17
- : new ethers.providers.InfuraProvider('goerli', INFURA_PROJECT_ID);
18
- }
19
-
20
- async function getContract({ contractAddress, isLocal = false, signer } = {}) {
21
- const provider = getDefaultProvider(isLocal);
22
-
23
- const contract = new ethers.Contract(contractAddress, ERC721DID.abi, signer || provider);
24
-
25
- return contract;
26
- }
27
-
28
- async function getContractFactory({ contractAddress, isLocal = false, signer } = {}) {
29
- const provider = getDefaultProvider(isLocal);
30
-
31
- const contract = new ethers.Contract(contractAddress, ERC721DIDFactory.abi, signer || provider);
32
-
33
- return contract;
34
- }
35
-
36
- async function getGasPrice({ provider }) {
37
- const gwei = 1000000000;
38
-
39
- const networkId = await getNetworkId({ contract });
40
-
41
- // ethereum
42
- if ([1, '1', 'mainnet', 'eth-main'].includes(networkId)) {
43
- const { data } = await axios.get(`https://token-data.arcblock.io/api/gas-prices?chainId=${networkId}`);
44
- return (data.fast / 10) * gwei;
45
- }
46
-
47
- if ([5, '5', 'goerli', 'eth-goerli'].includes(networkId)) {
48
- const { gasPrice } = await provider.getFeeData();
49
- return gasPrice.toString();
50
- }
51
-
52
- // hardhat
53
- if ([31337, '31337', 'hardhat'].includes(networkId)) {
54
- return 1 * gwei;
55
- }
56
-
57
- // TODO: bsc
58
- // TODO: matic
59
- // TODO: fantom
60
-
61
- return 1 * gwei;
62
- }
63
-
64
- async function getTxData(params = {}) {
65
- const { contract, fn, args } = params;
66
- const txData = contract.interface.encodeFunctionData(fn, args);
67
-
68
- const networkId = await getNetworkId({ contract });
69
-
70
- // const gasPrice = await getGasPrice({ provider: contract.provider });
71
- // const gasPriceAsGwei = Math.ceil(ethers.utils.formatUnits(gasPrice, 'gwei').toString());
72
-
73
- const gasLimit = await contract.provider
74
- .estimateGas({
75
- to: contract.address,
76
- data: txData,
77
- })
78
- .then((res) => res.toString());
79
-
80
- return toBase58(
81
- Buffer.from(
82
- JSON.stringify({
83
- network: networkId,
84
- tx: {
85
- to: contract.address,
86
- value: '0',
87
- gasLimit,
88
- data: txData,
89
- },
90
- }),
91
- 'utf-8'
92
- )
93
- );
94
- }
95
-
96
- const waitForTxReceipt = async ({ contract, txHash }) => {
97
- let tx = {};
98
- let receipt = {};
99
- const txHashTemp = txHash;
100
-
101
- await waitFor(
102
- async () => {
103
- // ethers getTransaction
104
- tx = await contract.provider.getTransaction(txHashTemp);
105
- return !!tx?.blockNumber;
106
- },
107
- { interval: 3000, timeout: 30 * 60 * 1000 }
108
- );
109
-
110
- await waitFor(
111
- async () => {
112
- // ethers getTransactionReceipt
113
- const originReceipt = await contract.provider.getTransactionReceipt(txHashTemp);
114
- receipt = {
115
- ...originReceipt,
116
- parseLog: originReceipt?.logs?.map((log) => {
117
- try {
118
- return contract.interface.parseLog(log);
119
- } catch (_error) {
120
- return log;
121
- }
122
- }),
123
- };
124
- return !!receipt?.blockNumber;
125
- },
126
- { interval: 3000, timeout: 30 * 60 * 1000 }
127
- );
128
-
129
- return receipt;
130
- };
131
-
132
- const onContractFilter = async ({ contract, fn, onSuccess }) => {
133
- contract.provider.on(
134
- {
135
- address: contract.address,
136
- topics: [contract.interface.getEventTopic(upperFirst(fn))],
137
- },
138
- async (res) => {
139
- await onSuccess(res);
140
- }
141
- );
142
- };
143
-
144
- const getNetworkId = async ({ contract }) => {
145
- const networkId = await contract.provider.getNetwork().then((res) => res.chainId);
146
- return networkId;
147
- };
148
-
149
- const getNetworkName = (networkId) => {
150
- const networkIdString = `${networkId}`;
151
- switch (networkIdString) {
152
- case '1':
153
- return 'mainnet';
154
- case '3':
155
- return 'ropsten';
156
- case '4':
157
- return 'rinkeby';
158
- case '5':
159
- return 'goerli';
160
- case '42':
161
- return 'kovan';
162
- case '31337':
163
- return 'hardhat';
164
- default:
165
- return 'unknown';
166
- }
167
- };
168
-
169
- const getAddressExplorerUrl = ({ networkId, hash }) => {
170
- return `https://${getNetworkName(networkId)}.etherscan.io/address/${hash}`;
171
- };
172
-
173
- const getBlockExplorerUrl = ({ networkId, blockletNumber }) => {
174
- return `https://${getNetworkName(networkId)}.etherscan.io/block/${blockletNumber}`;
175
- };
176
-
177
- const getTxExplorerUrl = ({ networkId, hash }) => {
178
- return `https://${getNetworkName(networkId)}.etherscan.io/tx/${hash}`;
179
- };
180
-
181
- const getCloneContractArgs = ({
182
- ownerAddress,
183
- baseImageURI,
184
- contractName,
185
- contractSymbol = '',
186
- contractDescription,
187
- totalLimit = 0,
188
- } = {}) => {
189
- const params = [ownerAddress, baseImageURI, contractName, contractSymbol, contractDescription, totalLimit];
190
- return params;
191
- };
192
-
193
- module.exports = {
194
- getContract,
195
- getContractFactory,
196
- getTxData,
197
- getGasPrice,
198
- waitForTxReceipt,
199
- onContractFilter,
200
- getNetworkId,
201
- getNetworkName,
202
- getBlockExplorerUrl,
203
- getTxExplorerUrl,
204
- getAddressExplorerUrl,
205
- getCloneContractArgs,
206
- };
package/lib/helper.js DELETED
@@ -1,216 +0,0 @@
1
- /* eslint-disable no-await-in-loop */
2
- const fs = require('fs');
3
- const assert = require('assert');
4
- const path = require('path');
5
- const pick = require('lodash/pick');
6
- const Web3 = require('web3');
7
- const crypto = require('crypto');
8
- const random = require('lodash/random');
9
- const chunk = require('lodash/chunk');
10
- const bip39 = require('ethereum-cryptography/bip39');
11
- const wallet = require('ethereumjs-wallet');
12
- const MerkleTree = require('@ocap/merkle-tree');
13
- const Mcrypto = require('@ocap/mcrypto');
14
- const { hexToBytes } = require('web3-utils');
15
- const { ethers } = require('hardhat');
16
- const { parse, stringify } = require('envfile');
17
-
18
- const BLACK_HOLE = '0x0000000000000000000000000000000000000000';
19
-
20
- const keccak256 = Mcrypto.Hasher.Keccak.hash256;
21
-
22
- const web3 = new Web3();
23
-
24
- // prepare accounts
25
- const HD_MNEMONIC = 'test test test test test test test test test test test junk';
26
- const HD_DERIVE_PATH = "m/44'/60'/0'/0/";
27
- const hdwallet = wallet.hdkey.fromMasterSeed(bip39.mnemonicToSeedSync(HD_MNEMONIC));
28
- const accounts = {};
29
- const names = [
30
- 'deployer',
31
- 'seed1',
32
- 'seed2',
33
- 'validator1',
34
- 'validator2',
35
- 'user1',
36
- 'user2',
37
- 'user3',
38
- 'user4',
39
- 'vault',
40
- 'vault2',
41
- ];
42
- for (let i = 0; i < names.length; i++) {
43
- const { privateKey } = hdwallet.derivePath(HD_DERIVE_PATH + i).getWallet();
44
- const account = web3.eth.accounts.privateKeyToAccount(web3.utils.toHex(privateKey));
45
- account.index = i;
46
- account.name = names[i];
47
- accounts[names[i]] = account;
48
- accounts[account.address] = account;
49
- }
50
- const seedValidators = [accounts.seed1.address, accounts.seed2.address];
51
- const seedSigners = [accounts.seed1, accounts.seed2];
52
-
53
- // prepare singers
54
- const getSingers = async () => {
55
- const signers = await ethers.getSigners();
56
-
57
- const getSigner = (name) => signers.find((x) => x.address === accounts[name].address) || signers[0];
58
-
59
- return { signers, getSigner };
60
- };
61
-
62
- // prepare contracts
63
- const getContracts = async ({ deployArgsMap = {} } = {}) => {
64
- const ERC721DIDParentContract = await ethers.getContractFactory('ERC721DID');
65
- const ERC721DIDParent = await ERC721DIDParentContract.deploy(...(deployArgsMap?.ERC721DIDParent || []));
66
-
67
- const ERC721DIDFactoryContract = await ethers.getContractFactory('ERC721DIDFactory');
68
- const ERC721DIDFactory = await ERC721DIDFactoryContract.deploy(
69
- ...(deployArgsMap?.ERC721DIDFactory || [ERC721DIDParent.address])
70
- );
71
-
72
- return { ERC721DIDParent, ERC721DIDFactory };
73
- };
74
-
75
- const getSignatures = (message, signers = seedSigners, nonces = {}, forceInvalid = false) =>
76
- signers.map((x) => {
77
- const nonce = nonces[x.address] || 0;
78
- const encoded = MerkleTree.encodePacked({ type: 'uint256', value: nonce }, { type: 'bytes32', value: message });
79
- return {
80
- signer: x.address,
81
- signature: hexToBytes(
82
- web3.eth.accounts.sign(forceInvalid ? encoded : keccak256(encoded), x.privateKey).signature
83
- ),
84
- nonce,
85
- };
86
- });
87
-
88
- const getTxHash = (size = 32) => `0x${crypto.randomBytes(size).toString('hex').toUpperCase()}`;
89
-
90
- const getUser = () => {
91
- const users = ['user1', 'user2', 'user3', 'user4'];
92
- return accounts[users[random(0, users.length - 1)]];
93
- };
94
-
95
- const getTx = (account, type, amount) => ({
96
- type,
97
- hash: getTxHash(),
98
- to: account.address,
99
- amount: web3.utils.toWei(String(amount), 'ether'),
100
- });
101
-
102
- const getTxs = (count = 10) => {
103
- const txs = [];
104
- for (let i = 0; i < count; i++) {
105
- txs.push(getTx(getUser(), 'withdraw', random(100, 200)));
106
- }
107
- return txs;
108
- };
109
-
110
- const getBlocks = (count = 9, size = 4) => {
111
- const groups = chunk(getTxs(count * size), size);
112
- const blocks = [];
113
- groups.forEach((txs, index) => {
114
- const height = index + 1;
115
-
116
- let previousHash;
117
- if (height > 1) {
118
- previousHash = blocks[index - 1].blockHash;
119
- } else {
120
- previousHash = '';
121
- }
122
-
123
- const txsHash = MerkleTree.getListHash(txs.map((x) => x.hash));
124
- const merkleTree = MerkleTree.getBlockMerkleTree(txs);
125
- const merkleRoot = merkleTree.getHexRoot();
126
- const blockHash = MerkleTree.getBlockHash({ height, previousHash, merkleRoot, txsHash });
127
-
128
- blocks.push({ height, blockHash, merkleRoot, txsHash, txs });
129
- });
130
-
131
- return blocks;
132
- };
133
-
134
- const insertBlock = (blocks, txs, height) => {
135
- assert(height >= 1, 'height must be >= 1');
136
- const previousHash = height >= 2 ? blocks[height - 2].blockHash : '';
137
- const txsHash = MerkleTree.getListHash(txs.map((x) => x.hash));
138
- const merkleTree = MerkleTree.getBlockMerkleTree(txs);
139
- const merkleRoot = merkleTree.getHexRoot();
140
- const blockHash = MerkleTree.getBlockHash({ height, previousHash, merkleRoot, txsHash });
141
- const block = { height, blockHash, merkleRoot, txsHash, txs };
142
-
143
- blocks.splice(height - 1, 0, block);
144
-
145
- for (let i = height; i < blocks.length; i++) {
146
- const previous = blocks[i - 1];
147
- const current = blocks[i];
148
- current.height = i + 1;
149
- current.previousHash = previous.blockHash;
150
- current.blockHash = MerkleTree.getBlockHash(pick(current, ['height', 'previousHash', 'merkleRoot', 'txsHash']));
151
- blocks[i] = current;
152
- }
153
- };
154
-
155
- const getProofs = (tx, height, blocks) => {
156
- const { txs } = blocks[height - 1];
157
- const tree = MerkleTree.getBlockMerkleTree(txs);
158
-
159
- const element = MerkleTree.encodePacked(
160
- { type: 'bytes32', value: tx.hash },
161
- { type: 'address', value: tx.to },
162
- { type: 'uint256', value: tx.amount }
163
- );
164
-
165
- return tree.getHexProof(element);
166
- };
167
-
168
- const updateEnvFile = (updates) => {
169
- const filePath = path.join(__dirname, '../', process.env.NODE_ENV ? `.env.${process.env.NODE_ENV}` : '.env');
170
- if (!fs.existsSync(filePath)) {
171
- fs.writeFileSync(filePath, '');
172
- }
173
- const env = Object.assign(parse(fs.readFileSync(filePath, 'utf8')), updates);
174
- fs.writeFileSync(filePath, stringify(env));
175
- };
176
-
177
- const getEnvFile = () => {
178
- const filePath = path.join(__dirname, '../', process.env.NODE_ENV ? `.env.${process.env.NODE_ENV}` : '.env');
179
- if (!fs.existsSync(filePath)) {
180
- return {};
181
- }
182
- const env = parse(fs.readFileSync(filePath, 'utf8'));
183
- return env;
184
- };
185
-
186
- const getNonces = async (contract, force = 0) => {
187
- const nonces = {};
188
- const validators = await contract.getValidators();
189
- for (const validator of validators) {
190
- const nonce = await contract.getNonce(validator);
191
- nonces[validator] = force || nonce.toNumber();
192
- }
193
-
194
- return nonces;
195
- };
196
-
197
- module.exports = {
198
- BLACK_HOLE,
199
- web3,
200
- accounts,
201
- insertBlock,
202
- seedValidators,
203
- seedSigners,
204
- getSingers,
205
- getContracts,
206
- getSignatures,
207
- getUser,
208
- getTxHash,
209
- getTx,
210
- getTxs,
211
- getBlocks,
212
- getProofs,
213
- getNonces,
214
- updateEnvFile,
215
- getEnvFile,
216
- };