Multisig Account Key
AccountKeyWeightedMultiSig 是一种账户密钥类型,包含一个阈值和加权公钥(WeightedPublicKeys),后者包含一个由公钥及其权重组成的列表。
要使与 AccountKeyWeightedMultiSig 关联的账户的交易有效,必须满足以下条件: 已签名公钥的加权和应大于阈值。 _ 无效签名不应包含在交易中。 * 已签名公钥的数量应少于加权公钥的数量。
从 Web3j 和 kaia 库(web3j-ext
)中导入必要的类。
使用指定的 BAOBAB_URL 创建 Web3j 实例
此外,您还可以更改默认提供商。 例如,使用alchemy提供商。
用它们的密钥对创建 3 个多重签名证书
燃气价格和gas 限值**设置
将发件人地址设置为已加载凭据的地址
从网络中获取chain ID
将接收器地址设置为任何有效地址
获取发件人地址的nonce值
设置值以传输
将交易类型设置为 VALUE_TRANSFER
为价值转移创建原始交易
使用 3 个凭证 按顺序签署交易
将签署的交易发送至 kaia 网络
从签名交易中恢复发件人地址,并将其与收件人地址进行比较
关闭 Web3j 实例
SignTxWithMultiSigExample.java
package org.web3j.example.accountKey;import org.web3j.tx.response.PollingTransactionReceiptProcessor;import org.web3j.tx.response.TransactionReceiptProcessor;import org.web3j.example.keySample;import java.io.IOException;import java.math.BigInteger;import org.web3j.crypto.KlayCredentials;import org.web3j.crypto.KlayRawTransaction;import org.web3j.crypto.KlayTransactionEncoder;import org.web3j.crypto.transaction.type.TxType;import org.web3j.crypto.transaction.type.TxType.Type;import org.web3j.protocol.core.DefaultBlockParameterName;import org.web3j.protocol.core.methods.response.EthChainId;import org.web3j.protocol.http.HttpService;import org.web3j.protocol.kaia.Web3j;import org.web3j.protocol.kaia.core.method.response.KlayRecoverFromTransactionResponse;import org.web3j.utils.Numeric;import org.web3j.protocol.core.methods.response.EthSendTransaction;/** * */public class SignTxWithMultiSigExample implements keySample { /** * */ public static void run() throws Exception { Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL)); KlayCredentials credentials1 = KlayCredentials.create(keySample.MULTI_KEY_privkey1, keySample.MULTI_KEY_address); KlayCredentials credentials2 = KlayCredentials.create(keySample.MULTI_KEY_privkey2, keySample.MULTI_KEY_address); KlayCredentials credentials3 = KlayCredentials.create(keySample.MULTI_KEY_privkey3, keySample.MULTI_KEY_address); BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L); BigInteger GAS_LIMIT = BigInteger.valueOf(6721950); String from = credentials1.getAddress(); EthChainId EthchainId = web3j.ethChainId().send(); long chainId = EthchainId.getChainId().longValue(); String to = "0x000000000000000000000000000000000000dead"; BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send() .getTransactionCount(); BigInteger value = BigInteger.valueOf(100); TxType.Type type = Type.VALUE_TRANSFER; KlayRawTransaction raw = KlayRawTransaction.createTransaction( type, nonce, GAS_PRICE, GAS_LIMIT, to, value, from); byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials1); signedMessage = KlayTransactionEncoder.signMessage(signedMessage, chainId, credentials2); signedMessage = KlayTransactionEncoder.signMessage(signedMessage, chainId, credentials3); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send(); System.out.println("TxHash : \n " + transactionResponse.getResult()); String blockNumber = "latest"; KlayRecoverFromTransactionResponse response = web3j.klayRecoverFromTransaction(hexValue, blockNumber) .send(); System.out.println("Original address : " + from); System.out.println("Result address : " + response.getResult()); web3j.shutdown(); }}
output
❯ java SignTxWithMultiSigExample.javaTxHash : 0x957734684be8f79a21cef4de1842709b84c92e3920d656165ddb951981987b5aOriginal address : 0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7eResult address : 0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e