<p>you can use bitcoinjs-lib package instead. here's the code for reference</p>
<pre><code>const bitcoin = require(&quot;bitcoinjs-lib&quot;);
const ecc = require(&quot;tiny-secp256k1&quot;);
const { ECPairFactory } = require(&quot;ecpair&quot;);

const ECPair = ECPairFactory(ecc);
function generateAddress(data) {
    const chain = data.network
    let network;
if (chain === &quot;testnet&quot;){
     network = bitcoin.networks.testnet
} else {
     network = bitcoin.networks.mainnet
}
const keyPair = ECPair.makeRandom();
const p2wpkh = bitcoin.payments.p2wpkh({
    pubkey: keyPair.publicKey,
    network,
});
const privateKey = keyPair.toWIF();
return {
    address: p2wpkh.address,
    privateKey: privateKey
};
}

module.exports = {
    generateAddress
};
</code></pre>

CC BY-SA 4.0
2024-05-21T04:42:02.687Z
and to create and sign the transactions of SegWit I suggest using the bitcoin-sdk-js pacakge.
https://www.npmjs.com/package/bitcoin-sdk-js
Sudhanshu Sinha
CC BY-SA 4.0
2024-05-16T12:34:16.860Z
2024-05-16T12:34:16.860Z
Sudhanshu Sinha
<p>How to create a SegWit address and transaction using <a href="https://bitcore.io/api/lib" rel="nofollow noreferrer">bitcore-lib</a>?</p>

<address><segregated-witness><bitcore>
SegWit with BitCore-Lib
Goko Gorgiovski
# SegWit with BitCore-Lib

(Asked by: username_0 on 2017-10-02)

How to create a SegWit address and transaction using [bitcore-lib](https://bitcore.io/api/lib)?

**Tags:** address, segregated-witness, bitcore


---

## 1 answers

### Answer by username_1 on 2024-05-16. Score: 1 | Accepted answer

you can use bitcoinjs-lib package instead. here's the code for reference

```
const bitcoin = require("bitcoinjs-lib");
const ecc = require("tiny-secp256k1");
const { ECPairFactory } = require("ecpair");

const ECPair = ECPairFactory(ecc);
function generateAddress(data) {
    const chain = data.network
    let network;
if (chain === "testnet"){
     network = bitcoin.networks.testnet
} else {
     network = bitcoin.networks.mainnet
}
const keyPair = ECPair.makeRandom();
const p2wpkh = bitcoin.payments.p2wpkh({
    pubkey: keyPair.publicKey,
    network,
});
const privateKey = keyPair.toWIF();
return {
    address: p2wpkh.address,
    privateKey: privateKey
};
}

module.exports = {
    generateAddress
};

```

> **Comments:**
> **username_1**: and to create and sign the transactions of SegWit I suggest using the bitcoin-sdk-js pacakge. https://www.npmjs.com/package/bitcoin-sdk-js  

---

