波场(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 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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值