import { SegwitBech32Wallet } from '../../class'; const bitcoin = require('bitcoinjs-lib'); const assert = require('assert'); describe('Segwit P2SH wallet', () => { it('can create transaction', async () => { const wallet = new SegwitBech32Wallet(); wallet.setSecret('L4vn2KxgMLrEVpxjfLwxfjnPPQMnx42DCjZJ2H7nN4mdHDyEUWXd'); assert.strictEqual(wallet.getAddress(), 'bc1q3rl0mkyk0zrtxfmqn9wpcd3gnaz00yv9yp0hxe'); assert.deepStrictEqual(wallet.getAllExternalAddresses(), ['bc1q3rl0mkyk0zrtxfmqn9wpcd3gnaz00yv9yp0hxe']); assert.strictEqual(await wallet.getChangeAddressAsync(), wallet.getAddress()); const utxos = [ { txid: '57d18bc076b919583ff074cfba6201edd577f7fe35f69147ea512e970f95ffeb', vout: 0, value: 100000, }, ]; let txNew = wallet.createTransaction(utxos, [{ value: 90000, address: '1GX36PGBUrF8XahZEGQqHqnJGW2vCZteoB' }], 1, wallet.getAddress()); let tx = bitcoin.Transaction.fromHex(txNew.tx.toHex()); const satPerVbyte = txNew.fee / tx.virtualSize(); assert.strictEqual(Math.round(satPerVbyte), 1); assert.strictEqual( txNew.tx.toHex(), '02000000000101ebff950f972e51ea4791f635fef777d5ed0162bacf74f03f5819b976c08bd1570000000000ffffffff02905f0100000000001976a914aa381cd428a4e91327fd4434aa0a08ff131f1a5a88ac7e2600000000000016001488fefdd8967886b32760995c1c36289f44f7918502483045022100fc0ba843588a5156878cd9d6e3c6b0cbce6acd5c8cf04dc09dcb8d0f23da07f002207a2d5bed25936b1eeda7e836a4734f5a0e66b4996aff64b3b3759ae81805722301210314cf2bf53f221e58c5adc1dd95adba9239b248f39b09eb2c550aadc1926fe7aa00000000', ); assert.strictEqual(tx.ins.length, 1); assert.strictEqual(tx.outs.length, 2); assert.strictEqual('1GX36PGBUrF8XahZEGQqHqnJGW2vCZteoB', bitcoin.address.fromOutputScript(tx.outs[0].script)); // to address assert.strictEqual(bitcoin.address.fromOutputScript(tx.outs[1].script), wallet.getAddress()); // change address // sendMax txNew = wallet.createTransaction(utxos, [{ address: '1GX36PGBUrF8XahZEGQqHqnJGW2vCZteoB' }], 1, wallet.getAddress()); tx = bitcoin.Transaction.fromHex(txNew.tx.toHex()); assert.strictEqual(tx.ins.length, 1); assert.strictEqual(tx.outs.length, 1); assert.strictEqual('1GX36PGBUrF8XahZEGQqHqnJGW2vCZteoB', bitcoin.address.fromOutputScript(tx.outs[0].script)); // to address // batch send + send max txNew = wallet.createTransaction( utxos, [{ address: '1GX36PGBUrF8XahZEGQqHqnJGW2vCZteoB' }, { address: '14YZ6iymQtBVQJk6gKnLCk49UScJK7SH4M', value: 10000 }], 1, wallet.getAddress(), ); tx = bitcoin.Transaction.fromHex(txNew.tx.toHex()); assert.strictEqual(tx.ins.length, 1); assert.strictEqual(tx.outs.length, 2); assert.strictEqual('1GX36PGBUrF8XahZEGQqHqnJGW2vCZteoB', bitcoin.address.fromOutputScript(tx.outs[0].script)); // to address assert.strictEqual('14YZ6iymQtBVQJk6gKnLCk49UScJK7SH4M', bitcoin.address.fromOutputScript(tx.outs[1].script)); // to address }); it('can sign and verify messages', async () => { const l = new SegwitBech32Wallet(); l.setSecret('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1'); // from bitcoinjs-message examples const signature = l.signMessage('This is an example of a signed message.', l.getAddress()); assert.strictEqual(signature, 'J9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='); assert.strictEqual(l.verifyMessage('This is an example of a signed message.', l.getAddress(), signature), true); }); });