至此,采用Node.js v18.16.0 执行CreateWallet 完成:
// import { ethers } from "ethers";
// import { Accounts } from "web3-eth-accounts";
// import { UserOpReceipt, BaseWalletLib, UserOperation, packSignatureHash, signMessage, encodeSignature } from 'bonus-wallet-js-sdk';
const platrust = require("platrust-wallet-js-sdk")
const ethers = require("ethers")
const AddressZero = '0x0000000000000000000000000000000000000000';
const SignatureMode = {
owner : 0x0,
guardians : 0x1,
session : 0x2
}
async function main() {
//组 1
const pks = ['']
let owners = ['']
//组 2
//const pks = ['']
//let owners = ['']
const chainURL = 'https://devnet2openapi2.platon.network/rpc'
const bundleURL = 'https://testbundler.platon.network'
const walletLib = new platrust.WalletLib();
const walletLogic = '0x3b682b956E65b5F5b8150f75F2235f156A8F4b7B'
const walletFactory = '0x97429FFFdE9223C92Cb00F66D8352B0642f70FA4' // wallet proxy factory contract address
const relayerManagerAddr = '0xD7998fC16185cC619b0918028D9BBc77A844a880'
const salt = ethers.utils.formatBytes32String("abc");
/**
* 同步等待getSetupCode完成,并将返回值赋予initializer
*/
const initializer = await walletLib.getSetupCode(
relayerManagerAddr, // <address> EntryPoint Contract Address
owners, // <[address]> owner Address List
1, // <number> threshold
AddressZero, // <address> to Address
'0x', // <string> wallet init execute data
AddressZero, // <string> fallbackHandler
86400, // <number> lockPerid
)
console.log("1. initializer:", initializer,'\n');
/**
* 计算即将生成的钱包地址
*/
const walletAddress = await walletLib.calculateWalletAddress(
walletLogic, // <address> BonusWalletLogic Contract Address
initializer, // <string> initializer
salt, // <string> salt (Hex string)
walletFactory // <address> wallet Factory Address
);
console.log("2. walletAddress:", walletAddress,'\n');
const initcode = walletLib.getInitCode(walletFactory, walletLogic, initializer, salt);
console.log("3. initcode:",initcode,'\n');
const activateOp = walletLib.activateWalletOp(
walletLogic, // <address> BonusWallet Logic Contract Address
initializer, // <string> initializer
undefined, // <bytes> paymasterAndData
salt, // <string> salt (Hex string)
walletFactory, // <address> Wallet factory Contract Address
100,// <number> maxFeePerGas 100Gwei
1000,// <number> maxPriorityFeePerGas 10Gwei
5000000,
500000,
100000
);
console.log("4. activateOp:",activateOp,'\n');
console.log("5. user op:", activateOp.toTuple(), '\n');
const userOpHash = await activateOp.getUserOpHashFromContract(
relayerManagerAddr, // <address> EntryPoint Contract Address
new ethers.providers.JsonRpcProvider( chainURL), // ethers.providers
);
console.log("6. userOpHash:", userOpHash,'\n');
const signedHash = platrust.packSignatureHash(userOpHash, SignatureMode.owner, 1690963200000, 1690977600000);
console.log("7. signedMsg: ", signedHash,'\n');
let sigs = '0x'
for (var i = 0; i < pks.length; i++) {
const sig = platrust.signMessage(signedHash, pks[i])
sigs = ethers.utils.solidityPack(
['bytes', 'bytes'],
[sigs, sig]
)
}
console.log('8. sig: ', sigs,'\n');
activateOp.signature = platrust.encodeSignature(SignatureMode.owner, sigs, 0, 0);
console.log("9. signature: ", activateOp.signature,'\n');
const bundler = new walletLib.Bundler(
relayerManagerAddr, // <address> EntryPoint Contract Address
new ethers.providers.JsonRpcProvider(chainURL),
bundleURL
);
console.log("10. bundler: ", bundler,'\n');
const entryPoints = await bundler.platon_supportedEntryPoints()
console.log('11. entry points: ', entryPoints,'\n');
const validation = await bundler.simulateHandleOp(activateOp);
console.log('11. validation: ', validation,'\n');
const bundlerEvent = bundler.sendUserOperation(activateOp);
console.log("11. bundlerEvent: ", bundlerEvent,'\n');
bundlerEvent.on('error', (err) => {
console.log("error: ", err);
});
bundlerEvent.on('send', async (userOpHash) => {
console.log('send: ' + userOpHash);
});
bundlerEvent.on('receipt', (receipt) => {
console.log('receipt: ' + JSON.stringify(receipt));
});
bundlerEvent.on('timeout', () => {
console.log('timeout');
});
}
main(); //创建钱包
但是会报告 AA23 reverted: wallet: signer not a owner