Ejemplo n.º 1
0
        private static PSBT BuildPsbt(Network network, HDFingerprint fingerprint, ExtPubKey xpub, KeyPath xpubKeyPath)
        {
            var deriveSendFromKeyPath = new KeyPath("1/0");
            var deriveSendToKeyPath   = new KeyPath("0/0");

            KeyPath sendFromKeyPath = xpubKeyPath.Derive(deriveSendFromKeyPath);
            KeyPath sendToKeyPath   = xpubKeyPath.Derive(deriveSendToKeyPath);

            PubKey sendFromPubKey = xpub.Derive(deriveSendFromKeyPath).PubKey;
            PubKey sendToPubKey   = xpub.Derive(deriveSendToKeyPath).PubKey;

            BitcoinAddress sendFromAddress = sendFromPubKey.GetAddress(ScriptPubKeyType.Segwit, network);
            BitcoinAddress sendToAddress   = sendToPubKey.GetAddress(ScriptPubKeyType.Segwit, network);

            TransactionBuilder builder = network.CreateTransactionBuilder();

            builder = builder.AddCoins(new Coin(uint256.One, 0, Money.Coins(1), sendFromAddress.ScriptPubKey));
            builder.Send(sendToAddress.ScriptPubKey, Money.Coins(0.99999m));
            PSBT psbt = builder
                        .SendFees(Money.Coins(0.00001m))
                        .BuildPSBT(false);

            var rootKeyPath1 = new RootedKeyPath(fingerprint, sendFromKeyPath);
            var rootKeyPath2 = new RootedKeyPath(fingerprint, sendToKeyPath);

            psbt.AddKeyPath(sendFromPubKey, rootKeyPath1, sendFromAddress.ScriptPubKey);
            psbt.AddKeyPath(sendToPubKey, rootKeyPath2, sendToAddress.ScriptPubKey);
            return(psbt);
        }