波场(Tron)钱包设置多签
经常有人问我多签代码怎么写,官网有很多资料,都是稀里糊涂的没有完整的例子,而且也没有C#的例子,最近刚好有需求就自己动手写了。
代码
//封装
private static async Task<dynamic> AccountPermissionUpdateAsync(string privateKey, string toAddress) {
var record = TronServiceExtension.GetRecord();
var transactionClient = record.TronClient?.GetTransaction();
var account = new TronAccount(privateKey, TronNetwork.MainNet);
var transactionExtention = await transactionClient?.CreateAccountPermissionUpdateTransactionAsync(account.Address, toAddress)!;
var transactionSigned = transactionClient.GetTransactionSign(transactionExtention.Transaction, privateKey);
var returnObj = await transactionClient.BroadcastTransactionAsync(transactionSigned);
return new { Result = returnObj.Result, Message = returnObj.Message, TransactionId = transactionExtention.Transaction.GetTxid() };
}
//调用
var privateKey = ""; //这里设置需要被多签的私钥
var ownerAddress = ""; //这里设置控制地址
//注意被设置多签的地址,最少需要 100TRX 余额
var result = await AccountPermissionUpdateAsync(privateKey, ownerAddress);
Console.WriteLine(JsonConvert.SerializeObject(result));
源码
//接口
Task<TransactionExtention> CreateAccountPermissionUpdateTransactionAsync(string fromAddress, string toAddress);
//实现
public async Task<TransactionExtention> CreateAccountPermissionUpdateTransactionAsync(string fromAddress, string toAddress) {
var wallet = _walletClient.GetProtocol();
var accountPermissionUpdateContract = new AccountPermissionUpdateContract {
OwnerAddress = _walletClient.ParseAddress(fromAddress),
Owner = new Permission() { PermissionName = "owner", Threshold = 1, Type = Permission.Types.PermissionType.Owner }
};
accountPermissionUpdateContract.Owner.Keys.Add(new Key { Address = _walletClient.ParseAddress(toAddress), Weight = 1 });
var activePermission = new Permission {
PermissionName = "active",
Threshold = 1, //授权需要的权重数量
Type = Permission.Types.PermissionType.Active,
// 7fff1fc0037e0000000000000000000000000000000000000000000000000000 包含了除更新权限以外的全部权限,Owner 默认都有,不需要设置
Operations = ByteString.CopyFrom("7fff1fc0037e0000000000000000000000000000000000000000000000000000".HexToByteArray())
};
var activeKey = new Key {
Address = _walletClient.ParseAddress(toAddress), //授予地址
Weight = 1 //权重值
};
activePermission.Keys.Add(activeKey);
var permissions = new Google.Protobuf.Collections.RepeatedField<Permission> {
activePermission
};
accountPermissionUpdateContract.Actives.Add(permissions);
return await wallet.AccountPermissionUpdateAsync(accountPermissionUpdateContract);
}
引入依赖库
PM> Install-Package TronNet.Wallet -Version 1.0.1
设置波场主网RPC信息(配置)
using Microsoft.Extensions.Options;
namespace ConsoleApp1;
using Microsoft.Extensions.DependencyInjection;
using Tron;
public record TronRecord(IServiceProvider ServiceProvider, ITronClient? TronClient, IOptions<TronNetOptions>? Options);
public static class TronServiceExtension {
private static IServiceProvider AddTronNet() {
IServiceCollection services = new ServiceCollection();
services.AddTronNet(x =>
{
x.Network = TronNetwork.MainNet;
x.Channel = new GrpcChannelOption { Host = "grpc.trongrid.io", Port = 50051 };
x.SolidityChannel = new GrpcChannelOption { Host = "grpc.trongrid.io", Port = 50052 };
x.ApiKey = "80a8b20f-a917-43a9-a2f1-809fe6eec0d6";
});
services.AddLogging();
return services.BuildServiceProvider();
}
public static TronRecord GetRecord() {
var provider = AddTronNet();
var client = provider.GetService<ITronClient>();
var options = provider.GetService<IOptions<TronNetOptions>>();
return new TronRecord(provider, client, options);
}
}
参考
https://cn.developers.tron.network/reference/accountpermissionupdate
其他
波场(Tron)钱包设置多签
波场(Tron)网页版(本地)钱包开源
波场(Tron)项目常用工具分享
波场(Tron)离线签名、广播交易笔记
波场(Tron)离线生成私钥和地址笔记