FinalizePsbt
FinalizePsbt expects a partial transaction with all inputs and outputs fully declared and tries to sign all inputs that belong to the wallet. Lnd must be the last signer of the transaction. That means, if there are any unsigned non-witness inputs or inputs without UTXO information attached or inputs without witness data that do not belong to lnd's wallet, this method will fail. If no error is returned, the PSBT is ready to be extracted and the final TX within to be broadcast.
NOTE: This method does NOT publish the transaction once finalized. It is the caller's responsibility to either publish the transaction on success or unlock/release any locked UTXOs in case of an error in this method.
Source: walletrpc/walletkit.proto
gRPC
rpc FinalizePsbt (FinalizePsbtRequest) returns (FinalizePsbtResponse);
REST
HTTP Method | Path |
---|---|
POST | /v2/wallet/psbt/finalize |
Code Samples
- gRPC
- REST
- Shell
- Javascript
- Python
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const GRPC_HOST = 'localhost:10009'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
const TLS_PATH = 'LND_DIR/tls.cert'
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const packageDefinition = protoLoader.loadSync(['lightning.proto', 'walletrpc/walletkit.proto'], loaderOptions);
const walletrpc = grpc.loadPackageDefinition(packageDefinition).walletrpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const tlsCert = fs.readFileSync(TLS_PATH);
const sslCreds = grpc.credentials.createSsl(tlsCert);
const macaroon = fs.readFileSync(MACAROON_PATH).toString('hex');
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
let metadata = new grpc.Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
let client = new walletrpc.WalletKit(GRPC_HOST, creds);
let request = {
funded_psbt: <bytes>,
account: <string>,
};
client.finalizePsbt(request, function(err, response) {
console.log(response);
});
// Console output:
// {
// "signed_psbt": <bytes>,
// "raw_final_tx": <bytes>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the walletrpc/walletkit.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import walletkit_pb2 as walletrpc, walletkit_pb2_grpc as walletkitstub
GRPC_HOST = 'localhost:10009'
MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
TLS_PATH = 'LND_DIR/tls.cert'
# create macaroon credentials
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
def metadata_callback(context, callback):
callback([('macaroon', macaroon)], None)
auth_creds = grpc.metadata_call_credentials(metadata_callback)
# create SSL credentials
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
# combine macaroon and SSL credentials
combined_creds = grpc.composite_channel_credentials(ssl_creds, auth_creds)
# make the request
channel = grpc.secure_channel(GRPC_HOST, combined_creds)
stub = walletkitstub.WalletKitStub(channel)
request = walletrpc.FinalizePsbtRequest(
funded_psbt=<bytes>,
account=<string>,
)
response = stub.FinalizePsbt(request)
print(response)
# {
# "signed_psbt": <bytes>,
# "raw_final_tx": <bytes>,
# }
- Javascript
- Python
const fs = require('fs');
const request = require('request');
const REST_HOST = 'localhost:8080'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
let requestBody = {
funded_psbt: <string>, // <bytes> (base64 encoded)
account: <string>, // <string>
};
let options = {
url: `https://${REST_HOST}/v2/wallet/psbt/finalize`,
// Work-around for self-signed certificates.
rejectUnauthorized: false,
json: true,
headers: {
'Grpc-Metadata-macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
form: JSON.stringify(requestBody),
}
request.post(options, function(error, response, body) {
console.log(body);
});
// Console output:
// {
// "signed_psbt": <string>, // <bytes>
// "raw_final_tx": <string>, // <bytes>
// }
import base64, codecs, json, requests
REST_HOST = 'localhost:8080'
MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
TLS_PATH = 'LND_DIR/tls.cert'
url = f'https://{REST_HOST}/v2/wallet/psbt/finalize'
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
headers = {'Grpc-Metadata-macaroon': macaroon}
data = {
'funded_psbt': base64.b64encode(<bytes>),
'account': <string>,
}
r = requests.post(url, headers=headers, data=json.dumps(data), verify=TLS_PATH)
print(r.json())
# {
# "signed_psbt": <bytes>,
# "raw_final_tx": <bytes>,
# }
$ lncli wallet psbt finalize --help
NAME:
lncli wallet psbt finalize - Finalize a Partially Signed Bitcoin Transaction (PSBT).
USAGE:
lncli wallet psbt finalize [command options] funded_psbt
DESCRIPTION:
The finalize command expects a partial transaction with all inputs
and outputs fully declared and tries to sign all inputs that belong to
the wallet. Lnd must be the last signer of the transaction. That means,
if there are any unsigned non-witness inputs or inputs without UTXO
information attached or inputs without witness data that do not belong
to lnd's wallet, this method will fail. If no error is returned, the
PSBT is ready to be extracted and the final TX within to be broadcast.
This method does NOT publish the transaction after it's been finalized
successfully.
OPTIONS:
--funded_psbt value the base64 encoded PSBT to finalize
--account value (optional) the name of the account to finalize the PSBT with
Messages
walletrpc.FinalizePsbtRequest
Source: walletrpc/walletkit.proto
Field | gRPC Type | REST Type | REST Placement |
---|---|---|---|
funded_psbt | bytes | string | body |
account | string | string | body |
walletrpc.FinalizePsbtResponse
Source: walletrpc/walletkit.proto
Field | gRPC Type | REST Type |
---|---|---|
signed_psbt | bytes | string |
raw_final_tx | bytes | string |