Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lazy initialization for scripts in HdPubKey #11848

Merged
merged 3 commits into from Nov 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 16 additions & 10 deletions WalletWasabi/Blockchain/Keys/HdPubKey.cs
Expand Up @@ -18,6 +18,12 @@ public class HdPubKey : NotifyPropertyChangedBase, IEquatable<HdPubKey>
private double _anonymitySet = DefaultHighAnonymitySet;
private Cluster _cluster;

private Lazy<Script> _p2pkScript;
private Lazy<Script> _p2pkhScript;
private Lazy<Script> _p2wpkhScript;
private Lazy<Script> _p2shOverP2wpkhScript;
private Lazy<Script> _p2Taproot;
turbolay marked this conversation as resolved.
Show resolved Hide resolved

public HdPubKey(PubKey pubKey, KeyPath fullKeyPath, LabelsArray labels, KeyState keyState)
{
PubKey = Guard.NotNull(nameof(pubKey), pubKey);
Expand All @@ -27,11 +33,11 @@ public HdPubKey(PubKey pubKey, KeyPath fullKeyPath, LabelsArray labels, KeyState
Cluster.UpdateLabels();
KeyState = keyState;

P2pkScript = PubKey.ScriptPubKey;
P2pkhScript = PubKey.GetScriptPubKey(ScriptPubKeyType.Legacy);
P2wpkhScript = PubKey.GetScriptPubKey(ScriptPubKeyType.Segwit);
P2shOverP2wpkhScript = PubKey.GetScriptPubKey(ScriptPubKeyType.SegwitP2SH);
P2Taproot = PubKey.GetScriptPubKey(ScriptPubKeyType.TaprootBIP86);
_p2pkScript = new Lazy<Script>(PubKey.ScriptPubKey);
turbolay marked this conversation as resolved.
Show resolved Hide resolved
_p2pkhScript = new Lazy<Script>(() => PubKey.GetScriptPubKey(ScriptPubKeyType.Legacy));
_p2wpkhScript = new Lazy<Script>(() => PubKey.GetScriptPubKey(ScriptPubKeyType.Segwit));
_p2shOverP2wpkhScript = new Lazy<Script>(() => PubKey.GetScriptPubKey(ScriptPubKeyType.SegwitP2SH));
_p2Taproot = new Lazy<Script>(() => PubKey.GetScriptPubKey(ScriptPubKeyType.TaprootBIP86));

PubKeyHash = PubKey.Hash;
HashCode = PubKeyHash.GetHashCode();
Expand Down Expand Up @@ -86,11 +92,11 @@ public double AnonymitySet
/// <remarks>Value can be non-<c>null</c> only for <see cref="IsInternal">internal keys</see> as they should be used just once.</remarks>
public Height? LatestSpendingHeight { get; set; }

public Script P2pkScript { get; }
public Script P2pkhScript { get; }
public Script P2wpkhScript { get; }
public Script P2shOverP2wpkhScript { get; }
public Script P2Taproot { get; }
public Script P2pkScript => _p2pkScript.Value;
public Script P2pkhScript => _p2pkhScript.Value;
public Script P2wpkhScript => _p2wpkhScript.Value;
public Script P2shOverP2wpkhScript => _p2shOverP2wpkhScript.Value;
public Script P2Taproot => _p2Taproot.Value;

public KeyId PubKeyHash { get; }

Expand Down