예제 #1
0
        public async Task TrackSpendableOutput(NRustLightningNetwork network,
                                               SpendableOutputDescriptor desc, CancellationToken ct = default)
        {
            var client = _nbXplorerClientProvider.GetClient(network);

            switch (desc)
            {
            case SpendableOutputDescriptor.StaticOutput staticOutput:
            {
                var addr = staticOutput.Item.Output.ScriptPubKey.GetDestinationAddress(network.NBitcoinNetwork);
                var s    = new AddressTrackedSource(addr);
                await client.TrackAsync(s, ct);

                await client.RPCClient.ImportAddressAsync(addr);

                break;
            }

            case SpendableOutputDescriptor.DynamicOutputP2WSH p2wshOutput:
            {
                var(param1, param2) = p2wshOutput.Item.KeyDerivationParams.ToValueTuple();
                var amountSat              = (ulong)p2wshOutput.Item.Output.Value.Satoshi;
                var channelKeys            = _keysRepository.DeriveChannelKeys(amountSat, param1, param2);
                var delayedPaymentKey      = Generators.derivePrivKey(_secp256k1Ctx, channelKeys.DelayedPaymentBaseKey, p2wshOutput.Item.PerCommitmentPoint.PubKey);
                var toSelfDelay            = p2wshOutput.Item.ToSelfDelay;
                var revokeableRedeemScript = _keysRepository.GetRevokeableRedeemScript(p2wshOutput.Item.RemoteRevocationPubKey, toSelfDelay, delayedPaymentKey.PubKey);
                Debug.Assert(p2wshOutput.Item.Output.ScriptPubKey.Equals(revokeableRedeemScript.WitHash.ScriptPubKey));

                var addr =
                    revokeableRedeemScript.WitHash.ScriptPubKey.GetDestinationAddress(network.NBitcoinNetwork);
                var s = new AddressTrackedSource(addr);
                await client.TrackAsync(s, ct);

                await client.RPCClient.ImportAddressAsync(addr);

                break;
            }

            case SpendableOutputDescriptor.StaticOutputRemotePayment remoteOutput:
            {
                var(p1, p2) = remoteOutput.Item.KeyDerivationParams.ToValueTuple();
                var amountSat = (ulong)remoteOutput.Item.Output.Value;
                var keys      = _keysRepository.DeriveChannelKeys(amountSat, p1, p2);
                Debug.Assert(
                    keys.PaymentKey.PubKey.WitHash.ScriptPubKey.Equals(remoteOutput.Item.Output.ScriptPubKey));

                var addr = remoteOutput.Item.Output.ScriptPubKey.GetDestinationAddress(network.NBitcoinNetwork);
                await client.TrackAsync(new AddressTrackedSource(addr), ct);

                await client.RPCClient.ImportAddressAsync(addr);

                break;
            }
            }
        }