|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('bsert'); |
| 4 | +const FullNode = require('../lib/node/fullnode'); |
| 5 | +const Network = require('../lib/protocol/network'); |
| 6 | +const Address = require('../lib/primitives/address'); |
| 7 | +const rules = require('../lib/covenants/rules'); |
| 8 | + |
| 9 | +/** @typedef {import('../lib/wallet/wallet')} Wallet */ |
| 10 | + |
| 11 | +const network = Network.get('regtest'); |
| 12 | + |
| 13 | +const node = new FullNode({ |
| 14 | + memory: true, |
| 15 | + network: network.type, |
| 16 | + plugins: [require('../lib/wallet/plugin')] |
| 17 | +}); |
| 18 | + |
| 19 | +const { wdb } = node.require('walletdb'); |
| 20 | + |
| 21 | +async function mineBlocks(n, addr) { |
| 22 | + addr = addr ? addr : new Address().toString(network); |
| 23 | + for (let i = 0; i < n; i++) { |
| 24 | + const block = await node.miner.mineBlock(null, addr); |
| 25 | + await node.chain.add(block); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +describe('Wallet Import Nonce', function () { |
| 30 | + /** @type {Wallet} */ |
| 31 | + let walletA; |
| 32 | + |
| 33 | + /** @type {Wallet} */ |
| 34 | + let walletB; |
| 35 | + |
| 36 | + const NAME = rules.grindName(10, 1, network); |
| 37 | + const NAMEHASH = rules.hashName(NAME); |
| 38 | + const BIDS = [ |
| 39 | + { value: 1e6, lockup: 2e6, addr: undefined }, // sendbid |
| 40 | + { value: 2e6, lockup: 4e6, addr: undefined }, // -|sendbatch |
| 41 | + { value: 4e6, lockup: 8e6, addr: undefined } // -|sendbatch |
| 42 | + ]; |
| 43 | + |
| 44 | + before(async () => { |
| 45 | + await node.ensure(); |
| 46 | + await node.open(); |
| 47 | + |
| 48 | + // Both wallets have the same seed |
| 49 | + walletA = await wdb.create(); |
| 50 | + walletB = await wdb.create({ mnemonic: walletA.master.mnemonic }); |
| 51 | + assert.bufferEqual(walletA.master.writeKey(), walletB.master.writeKey()); |
| 52 | + }); |
| 53 | + |
| 54 | + after(async () => { |
| 55 | + await node.close(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should fund wallet', async () => { |
| 59 | + await mineBlocks(2, await walletA.receiveAddress()); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should open an auction and advance to bidding period', async () => { |
| 63 | + await walletA.sendOpen(NAME, false); |
| 64 | + await mineBlocks(network.names.treeInterval + 1); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should bid with sendbid', async () => { |
| 68 | + const bid = BIDS[0]; |
| 69 | + |
| 70 | + const bidTx = await walletA.sendBid(NAME, bid.value, bid.lockup); |
| 71 | + |
| 72 | + // Save address for importnonce later |
| 73 | + bid.addr = bidTx.outputs[0].address; |
| 74 | + }); |
| 75 | + |
| 76 | + it('should bid with sendbatch', async () => { |
| 77 | + const batch = [ |
| 78 | + ['BID', NAME, BIDS[1].value, BIDS[1].lockup], |
| 79 | + ['BID', NAME, BIDS[2].value, BIDS[2].lockup] |
| 80 | + ]; |
| 81 | + |
| 82 | + const bidTx = await walletA.sendBatch(batch); |
| 83 | + |
| 84 | + // Save address for importnonce later |
| 85 | + for (const output of bidTx.outputs) { |
| 86 | + if (!output.covenant.isBid()) |
| 87 | + continue; |
| 88 | + |
| 89 | + const index = BIDS.findIndex(bid => bid.lockup === output.value); |
| 90 | + BIDS[index].addr = output.address; |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('should verify bids were placed', async () => { |
| 95 | + await mineBlocks(1); |
| 96 | + const bidsA = await walletA.getBidsByName(NAME); |
| 97 | + assert.strictEqual(bidsA.length, BIDS.length); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should not be known by other wallet', async () => { |
| 101 | + const bidsB = await walletB.getBidsByName(NAME); |
| 102 | + assert.strictEqual(bidsB.length, BIDS.length); |
| 103 | + |
| 104 | + for (const bid of bidsB) |
| 105 | + assert.strictEqual(bid.value, -1); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should be imported by other wallet', async () => { |
| 109 | + for (const bid of BIDS) |
| 110 | + await walletB.generateBlinds(NAMEHASH, bid.addr, bid.value); |
| 111 | + |
| 112 | + const bidsB = await walletB.getBidsByName(NAME); |
| 113 | + assert.strictEqual(bidsB.length, BIDS.length); |
| 114 | + |
| 115 | + // Ensure bids have correct true bid values |
| 116 | + for (const bid of bidsB) { |
| 117 | + const index = BIDS.findIndex(x => x.lockup === bid.lockup); |
| 118 | + assert.strictEqual(BIDS[index].value, bid.value); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + it('should reaveal all bids from other wallet', async () => { |
| 123 | + await mineBlocks(network.names.biddingPeriod); |
| 124 | + |
| 125 | + const revealTx = await walletB.sendRevealAll(); |
| 126 | + const revealOutputs = revealTx.outputs.filter(out => out.covenant.isReveal()); |
| 127 | + assert.strictEqual(revealOutputs.length, BIDS.length); |
| 128 | + }); |
| 129 | +}); |
0 commit comments