transactions – Using a bitcoin library to create digital signatures in TypeScript/Javascript


I have been trying to sign my transactions with a digital signature for the past few days but seems that libraries in bitcoin, as far as to my knowledge do not provide a simple way to sign the transactions with digital signatures. I have been trying to use bitcoinjs-lib to achieve my result.

Bitcoinjs-lib came out with a new way of signing transactions with the class Psbt (partially signed bitcoin transaction) and I have been trying to serialize the unsigned transaction and then create a signature with the unsigned transaction and then finally sign the transaction with the signature.

These are the steps that I have followed, can someone please guide me as to where I might be going wrong and what to do next?

I have created a new address say: bc1qjkg99q654997uz5lxanjugzz2hfwum7ph9fcvw, with the compressed public key as 039b219ff489f9f5d3c674602e2280cc803f068f07db7295c7d2a2f9d51844cc45 (in hexadecimal representation).

Script used is P2WPKH.

Say I want to transfer funds to 3HmEiQfaghizrNNn3tt5ydzvejLeYMzbD1, with some amount of BTC.
Now I want to create an unsigned transaction so that I can generate a digital signature and then sign the transaction.

This is how I proceeded:

First I created a payment object like so:

function createPayment(type, keys) {
        const network = bitcoin.networks.bitcoin;
        const payment = (bitcoin.payments)(type)({
                pubkey: keys(0).publicKey,
                network,
        });
        return {
                payment,
                keys,
        };
}
.
.
.
keys = ({ publicKey: Buffer.from('039b219ff489f9f5d3c674602e2280cc803f068f07db7295c7d2a2f9d51844cc45', 'hex') });
const p2wpkh = createPayment('p2wpkh', keys);

Now I maybe going wrong here…

I then proceeded to create an input data object like so (maybe I am missing scriptPubKey, if so how to derive it in a situation where you are not connected to the internet?)?!

        const psbt = new bitcoin.Psbt({ network: bitcoin.networks.bitcoin })
                .addInput(inputData)
                .addOutput({
                        address: '3HmEiQfaghizrNNn3tt5ydzvejLeYMzbD1',
                        value: 2e4,
                })

Can someone guide me on how to proceed or where to change the logic?

Also if I understand correctly all unspent inputs (if I have multiple) would need to be signed separately, so do digital signatures need to be created separately and then sign the inputs one by one?
How to create a hash of the an unsigned transaction from PSBT? Also if there is any other way to do this using bitcoin-core or some other library please let me know. I would be much grateful.

Also please tell me if this does not make sense at all. I think I might be a little confused on how I should use digital signatures here.