波场多签功能使用示例

TRON 操作示例
安装
# 参考 https://tronweb.network/docu/zh-Hans/docs/5.1.0/quickstart
npm install tronweb

在这个示例中,我们使用了 TronWeb 库来执行一些 TRON 区块链上的操作。

导入 TronWeb
const TronWeb = require('tronweb');
// 创建钱包
const tronWeb = new TronWeb({
    fullHost: 'https://nile.trongrid.io',
    headers: {'TRON-PRO-API-KEY': 'a9225b3f-a5a5-450e-b316-b0c679570284'},
});
打印默认地址
console.log(tronWeb.defaultAddress.base58);
console.log("starting...");
签名测试
// 签名
async function signatureTest() {
    const signature = await TronWeb.Trx.signMessageV2('消息', '0a3d0c5190a33764e8bf44f342d50357d71cf2fa4867ab9d609f46b207efec8d');
    console.log("当前签名:", signature)
    const base58Address = await TronWeb.Trx.verifyMessageV2('消息', signature);
    console.log("验证签名地址是:", base58Address)
}

// signatureTest().then()

创建钱包
// 创建钱包
async function createAccount() {
    // 创建离线钱包
    const res = await TronWeb.createAccount();
    console.log(res)
    // 创建随机的助记词和私钥
    // const {mnemonic, privateKey} = TronWeb.createRandom();
    // 使用上述的privateKey
    // const address = TronWeb.address.fromPrivateKey(privateKey);
    // console.log("创建钱包", mnemonic.phrase, privateKey, address)
    // 激活钱包转一定的trx就可以激活
    const amount = 100000000; // 转账的 TRX 数量 0.00001
    const transaction = await tronWeb.trx.sendTransaction(res.address.base58, amount, '0a3d0c5190a33764e8bf44f342d50357d71cf2fa4867ab9d609f46b207efec8d');
    console.log(transaction);
}

// createAccount().then()
创建多签钱包,修改账户权限
// 创建多签钱包,修改账户权限
async function createMultiAccount(address, privateKey) {
    let ownerPermission = {type: 0, permission_name: 'owner'};
    ownerPermission.threshold = 2;
    ownerPermission.keys = [];
    ownerPermission.keys.push({address: TronWeb.address.toHex('TNpVE2Nh1gywwFhnxU6aJGnKoe9NuxbRVx'), weight: 1});
    ownerPermission.keys.push({address: TronWeb.address.toHex('TEn82Kbc9oYqTdY4o5QaJpdDRxJvUjmupU'), weight: 1});
    ownerPermission.keys.push({address: TronWeb.address.toHex('TEtLEaFVPQr3bpkzSmGxd5YkGGaMjEG2Z7'), weight: 1});

    let activePermission = {type: 2, permission_name: 'active0'}; // 这里是否可以将active0换成ubao字符串
    activePermission.threshold = 2; // 总weight中有2个同意签名,即可完成交易
    activePermission.operations = '7fff1fc0033e0000000000000000000000000000000000000000000000000000'; // 如果线上需要更换 7fff1fc0033e0300000000000000000000000000000000000000000000000000
    activePermission.keys = [];
    activePermission.keys.push({address: TronWeb.address.toHex('TNpVE2Nh1gywwFhnxU6aJGnKoe9NuxbRVx'), weight: 1});
    activePermission.keys.push({address: TronWeb.address.toHex('TEn82Kbc9oYqTdY4o5QaJpdDRxJvUjmupU'), weight: 1});
    activePermission.keys.push({address: TronWeb.address.toHex('TEtLEaFVPQr3bpkzSmGxd5YkGGaMjEG2Z7'), weight: 1});

    console.log("开始修改")
    // 将 base58 地址转换为 hex 地址
    const hexAddress = TronWeb.address.toHex(address);
    const updateTransaction = await tronWeb.transactionBuilder.updateAccountPermissions(hexAddress, ownerPermission, null, [activePermission]);
    console.log(updateTransaction)

    // 签署交易
    const signedTransaction = await tronWeb.trx.sign(updateTransaction, privateKey);

    // 广播交易
    const result = await tronWeb.trx.sendRawTransaction(signedTransaction);

    console.log('Transaction Result:', result);
}


// createMultiAccount("TGSmFGG6d249Xt4BuNWaCftQ2U7w5XMXov", "09a423dbb1f0f2ffe45447b83bb98e1a11cb8abc36a3a7181a88369c7009d380").then()
生成多签交易(这里是转账TRC20代币)
// 生成多签交易
async function createMultiTransaction() {
    // TGSmFGG6d249Xt4BuNWaCftQ2U7w5XMXov 转出 1 USDT 到 TNpVE2Nh1gywwFhnxU6aJGnKoe9NuxbRVx
    let parameter = [{type: 'address', value: 'TNpVE2Nh1gywwFhnxU6aJGnKoe9NuxbRVx'}, {
        type: 'uint256',
        value: 1.9 * 1000000
    }];
    // const currentTimestamp = new Date().getTime() + 3600 * 1000 * 24;
    let options = {
        feeLimit: 120 * 1000000, // gas限制
    };
    // HEX格式 合约地址
    const contractAddress = TronWeb.address.toHex("TEpkBH2Yb9NG3xgXUW6UbudakyuCHZ7ZVF");
    // 发起人 issuerAddress HEX格式
    const issuerAddress = TronWeb.address.toHex("TGSmFGG6d249Xt4BuNWaCftQ2U7w5XMXov");
    const tx = await tronWeb.transactionBuilder.triggerSmartContract(contractAddress, "transfer(address,uint256)", options, parameter, issuerAddress);
    console.log(JSON.stringify(tx))
    console.log("\n---000---\n")

    // 以秒为单位延长未签名交易的过期时间。 22小时.最大时间限制1天
    // await tronWeb.transactionBuilder.extendExpiration(tx.transaction, 3600 * 22);
    const extendExpirationTx = await tronWeb.transactionBuilder.extendExpiration(tx.transaction, 3600 * 22);
    console.log(JSON.stringify(extendExpirationTx))
    console.log("\n---111---\n")
    // -------------------------------------------------------------------------------------------

    // TNpVE2Nh1gywwFhnxU6aJGnKoe9NuxbRVx 账号签名 私钥=0a3d0c5190a33764e8bf44f342d50357d71cf2fa4867ab9d609f46b207efec8d
    const privateKeyAccount1 = "0a3d0c5190a33764e8bf44f342d50357d71cf2fa4867ab9d609f46b207efec8d"
    const signedTxAccount1 = await tronWeb.trx.multiSign(extendExpirationTx, privateKeyAccount1, 2); // 0 为ownerPermission, Witness ID = 1,  Active ID从2开始递增
    console.log(JSON.stringify(signedTxAccount1))
    console.log("\n---222---\n")
    // TEtLEaFVPQr3bpkzSmGxd5YkGGaMjEG2Z7 账号签名 私钥=76ebeb4d91e8508fc8d621d14e280b9e97aa0ee2da184d56e3dff5c3b2b3c723
    const privateKeyAccount2 = "76ebeb4d91e8508fc8d621d14e280b9e97aa0ee2da184d56e3dff5c3b2b3c723"
    const signedTxAccount2 = await tronWeb.trx.multiSign(signedTxAccount1, privateKeyAccount2, 2); // 0 为ownerPermission, Witness ID = 1,  Active ID从2开始递增
    console.log(JSON.stringify(signedTxAccount2))
    console.log("\n---333---\n")

    // // 查询当前签名情况,Weight当前够不够去广播交易
    // var signWeight = await tronWeb.trx.getSignWeight(signedTxAccount1);
    // console.log("查询当前签名情况,Weight当前够不够去广播交易", signWeight)
    // // 已签名账号列表
    // var approvedList = await tronWeb.trx.getApprovedList(signedTxAccount1);
    // console.log("已签名账号列表", approvedList)

    // 广播交易
    const result = await tronWeb.trx.sendRawTransaction(signedTxAccount2);
    if (result.result) {
        console.log("广播成功:", result)
    } else {
        console.log("广播失败:", TronWeb.toUtf8(result.message))
    }

}

createMultiTransaction().then()