波場(Tron)錢包設定多籤

lijingkun發表於2023-06-26

經常有人問我多籤程式碼怎麼寫,官網有很多資料,都是稀裡糊塗的沒有完整的例子,而且也沒有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));

Github 原始碼

https://github.com/jared1106/Tron.Wallet.Net

//介面
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 Tron.Wallet

設定波場主網RPC資訊(配置)

using Microsoft.Extensions.Options;

namespace ConsoleApp1;

using Microsoft.Extensions.DependencyInjection;
using Tron.Wallet;

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)離線生成私鑰和地址筆記

相關文章