-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolanaWalletService.cs
More file actions
94 lines (81 loc) · 3.23 KB
/
SolanaWalletService.cs
File metadata and controls
94 lines (81 loc) · 3.23 KB
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
using Ost.Services.Wallets;
using Ost.Services.Wallets.Enums;
using Ost.Services.Wallets.SubWallets;
using Solnet.Wallet;
using Solnet.Wallet.Bip39;
namespace Ost.Services
{
/// <summary>
/// Implements the common interface for all Solana based wallets.
/// </summary>
public class SolanaWalletService : IWallet
{
/// <summary>
/// The wallet.
/// </summary>
private Wallet _wallet;
/// <summary>
/// The primary account provided by this wallet.
/// </summary>
private Account _account;
/// <summary>
/// Initialize the <see cref="SolanaWalletService"/> with the given <see cref="IWalletStore"/> and <see cref="IDerivationIndexWallet"/>.
/// </summary>
/// <param name="mnemonic">The mnemonic.</param>
/// <param name="derivationIndexWallet">The derivation index wallet.</param>
public SolanaWalletService(string mnemonic, IDerivationIndexWallet derivationIndexWallet)
{
//_walletStore = mainWallet;
AliasedWallet = derivationIndexWallet;
_wallet = new Wallet(mnemonic, WordList.AutoDetect(mnemonic));
SubWalletType = SubWalletType.DerivationIndex;
InitializeAccount();
}
/// <summary>
/// Initialize the <see cref="SolanaWalletService"/> with the given <see cref="IWalletStore"/> and <see cref="IPrivateKeyWallet"/>.
/// </summary>
/// <param name="mainWallet">The main wallet.</param>
/// <param name="privateKeyWallet">The private key file based wallet.</param>
public SolanaWalletService(IPrivateKeyWallet privateKeyWallet)
{
_wallet = SolanaPrivateKeyLoader.Load(privateKeyWallet.Path);
AliasedWallet = privateKeyWallet;
SubWalletType = SubWalletType.PrivateKey;
InitializeAccount();
}
/// <summary>
/// Initialize the underlying account provided by the wallet.
/// </summary>
private void InitializeAccount()
{
if (AliasedWallet is IDerivationIndexWallet derivationIndexWallet)
{
_account = _wallet.GetAccount(derivationIndexWallet.DerivationIndex);
}
else if (AliasedWallet is IPrivateKeyWallet)
{
_account = _wallet.Account;
}
else
{
_account = _wallet.Account;
}
}
/// <inheritdoc cref="IWallet.AliasedWallet"/>
public IAliasedWallet AliasedWallet { get; }
/// <inheritdoc cref="IWallet.Alias"/>
public string Alias
{
get => AliasedWallet.Alias;
set => AliasedWallet.Alias = value;
}
/// <inheritdoc cref="IWallet.ShortenedAddress"/>
public string ShortenedAddress => _account.PublicKey.Key[..6] + "..." + _account.PublicKey.Key[^6..];
/// <inheritdoc cref="IWallet.SubWalletType"/>
public SubWalletType SubWalletType { get; init; }
/// <inheritdoc cref="IWallet.Address"/>
public PublicKey Address => _account;
/// <inheritdoc cref="IWallet.Sign(byte[])"/>
public byte[] Sign(byte[] data) => _account.Sign(data);
}
}