-
Notifications
You must be signed in to change notification settings - Fork 3
/
IconSDKManager.cs
113 lines (91 loc) · 3.73 KB
/
IconSDKManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using UnityEngine;
using IconSDK.RPCs;
using System.Numerics;
using IconSDK;
using IconSDK.Account;
using IconSDK.Types;
using NBitcoin;
using NBitcoin.DataEncoders;
using Network = NBitcoin.Network;
using Cysharp.Threading.Tasks;
public class IconSDKManager : MonoBehaviour
{
public string MainNetApiUri = "https://wallet.icon.foundation/api/v3";
public string TestNetApiUri = "https://testwallet.icon.foundation/api/v3";
public static IconSDKManager Instance;
async void Start()
{
Instance = this;
}
public async UniTask<Hash32> GetBlockByHeight(BigInteger height)
{
var get = new GetBlockByHeight(Consts.ApiUrl.MainNet);
var result = await get.Invoke(height);
return result.Hash;
}
public async UniTask<Hash32> GetLastBlockAsync()
{
var get = new GetLastBlock(Consts.ApiUrl.MainNet);
var result = await get.Invoke();
return result.Hash;
}
public async UniTask<BigInteger> GetTotalSupplyAsync()
{
var getTotalSupply = new GetTotalSupply(Consts.ApiUrl.MainNet);
var totalSupply = await getTotalSupply.Invoke();
return totalSupply;
}
public Wallet GetWalletFromMnemonic(string mnemonicString)
{
Mnemonic mnemonic = new Mnemonic(mnemonicString);
byte[] bytes = GetPrivateKeyBytesFromMnemonic(mnemonic.ToString());
Wallet wallet = Wallet.Create(new PrivateKey(bytes));
return wallet;
}
public byte[] GetPrivateKeyBytesFromMnemonic(string mnemonicString)
{
Mnemonic mnemonic = new Mnemonic(mnemonicString);
ExtKey extKey = mnemonic.DeriveExtKey();
Key privKey = extKey.PrivateKey;
return privKey.ToBytes();
}
public async UniTask<double> GetBalanceAsync(string address, NetworkType network)
{
GetBalance getBalance = new GetBalance(GetApiUri(network));
BigInteger balance = await getBalance.Invoke(address);
return balance.ToCoins(BigIntegerExtension.ICXDecimals);
}
private string GetApiUri(NetworkType network)
{
if (network == NetworkType.Mainnet)
return MainNetApiUri;
return TestNetApiUri;
}
}
public enum NetworkType
{
Mainnet, Testnet
}
public class MainnetNetwork : Network
{
public MainnetNetwork()
{
this.Base58Prefixes = new byte[12][];
this.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS] = new byte[] { 127 }; // t
this.Base58Prefixes[(int)Base58Type.SCRIPT_ADDRESS] = new byte[] { 137 }; // x
this.Base58Prefixes[(int)Base58Type.SECRET_KEY] = new byte[] { (239) };
this.Base58Prefixes[(int)Base58Type.ENCRYPTED_SECRET_KEY_NO_EC] = new byte[] { 0x01, 0x42 };
this.Base58Prefixes[(int)Base58Type.ENCRYPTED_SECRET_KEY_EC] = new byte[] { 0x01, 0x43 };
this.Base58Prefixes[(int)Base58Type.EXT_PUBLIC_KEY] = new byte[] { (0x04), (0x35), (0x87), (0xCF) };
this.Base58Prefixes[(int)Base58Type.EXT_SECRET_KEY] = new byte[] { (0x04), (0x35), (0x83), (0x94) };
this.Base58Prefixes[(int)Base58Type.PASSPHRASE_CODE] = new byte[] { 0x2C, 0xE9, 0xB3, 0xE1, 0xFF, 0x39, 0xE2 };
this.Base58Prefixes[(int)Base58Type.CONFIRMATION_CODE] = new byte[] { 0x64, 0x3B, 0xF6, 0xA8, 0x9A };
this.Base58Prefixes[(int)Base58Type.STEALTH_ADDRESS] = new byte[] { 0x2b };
this.Base58Prefixes[(int)Base58Type.ASSET_ID] = new byte[] { 115 };
this.Base58Prefixes[(int)Base58Type.COLORED_ADDRESS] = new byte[] { 0x13 };
Bech32Encoder encoder = Encoders.Bech32("tb");
this.Bech32Encoders = new Bech32Encoder[2];
this.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS] = encoder;
this.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS] = encoder;
}
}