-
Notifications
You must be signed in to change notification settings - Fork 492
/
KeyChain.cs
75 lines (61 loc) · 2.54 KB
/
KeyChain.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
using NBitcoin;
using System.Collections.Generic;
using System.Linq;
using WalletWasabi.Blockchain.Keys;
using WalletWasabi.Crypto;
using WalletWasabi.Extensions;
using WalletWasabi.Wallets;
namespace WalletWasabi.WabiSabi.Client;
public class KeyChain : IKeyChain
{
public KeyChain(KeyManager keyManager, Kitchen kitchen)
{
if (keyManager.IsWatchOnly)
{
throw new ArgumentException("A watch-only key manager cannot be used to initialize a key chain.");
}
KeyManager = keyManager;
Kitchen = kitchen;
}
private KeyManager KeyManager { get; }
private Kitchen Kitchen { get; }
private Key GetMasterKey()
{
return KeyManager.GetMasterExtKey(Kitchen.SaltSoup()).PrivateKey;
}
public void TrySetScriptStates(KeyState state, IEnumerable<Script> scripts)
{
foreach (var hdPubKey in KeyManager.GetKeys(key => scripts.Any(key.ContainsScript)))
{
KeyManager.SetKeyState(state, hdPubKey);
}
KeyManager.ToFile();
}
public OwnershipProof GetOwnershipProof(IDestination destination, CoinJoinInputCommitmentData commitmentData)
{
ExtKey hdKey = KeyManager.GetSecrets(Kitchen.SaltSoup(), destination.ScriptPubKey).SingleOrDefault()
?? throw new InvalidOperationException($"The signing key for '{destination.ScriptPubKey}' was not found.");
Key masterKey = GetMasterKey();
BitcoinSecret secret = hdKey.GetBitcoinSecret(KeyManager.GetNetwork(), destination.ScriptPubKey);
return NBitcoinExtensions.GetOwnershipProof(masterKey, secret, destination.ScriptPubKey, commitmentData);
}
public Transaction Sign(Transaction transaction, Coin coin, PrecomputedTransactionData precomputedTransactionData)
{
transaction = transaction.Clone();
if (transaction.Inputs.Count == 0)
{
throw new ArgumentException("No inputs to sign.", nameof(transaction));
}
var txInput = transaction.Inputs.AsIndexedInputs().FirstOrDefault(input => input.PrevOut == coin.Outpoint)
?? throw new InvalidOperationException("Missing input.");
ExtKey hdKey = KeyManager.GetSecrets(Kitchen.SaltSoup(), coin.ScriptPubKey).SingleOrDefault()
?? throw new InvalidOperationException($"The signing key for '{coin.ScriptPubKey}' was not found.");
BitcoinSecret secret = hdKey.GetBitcoinSecret(KeyManager.GetNetwork(), coin.ScriptPubKey);
TransactionBuilder builder = Network.Main.CreateTransactionBuilder();
builder.AddKeys(secret);
builder.AddCoins(coin);
builder.SetSigningOptions(new SigningOptions(TaprootSigHash.All, (TaprootReadyPrecomputedTransactionData)precomputedTransactionData));
builder.SignTransactionInPlace(transaction);
return transaction;
}
}