UNPKG

blockstack

Version:

The Blockstack Javascript library for authentication, identity, and storage.

996 lines (839 loc) 75.3 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); exports.runOperationsTests = runOperationsTests; var _tape = require('tape'); var _tape2 = _interopRequireDefault(_tape); var _fetchMock = require('fetch-mock'); var _fetchMock2 = _interopRequireDefault(_fetchMock); var _bitcoinjsLib = require('bitcoinjs-lib'); var _bitcoinjsLib2 = _interopRequireDefault(_bitcoinjsLib); var _bigi = require('bigi'); var _bigi2 = _interopRequireDefault(_bigi); var _network = require('../../../lib/network'); var _utils = require('../../../lib/operations/utils'); var _lib = require('../../../lib'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var testAddresses = [{ skHex: '85b33fdfa5efeca980806c6ad3c8a55d67a850bd987237e7d49c967566346fbd01', address: '1br553PVnK6F5nyBtb4ju1owwBKdsep5c' }, { skHex: '744196d67ed78fe39009c71fbfd53e6ecca98353fbfe81ccba21b0703a69be9c01', address: '16xVjkJ3nY62B9t9q3N9wY6hx1duAfwRZR' }, { address: '1HEjCcUjZXtbiDnCYviHLVZvSQsSZoDRFa', skHex: '12f90d1b9e34d8df56f0dc6754a97ab4a2eb962918c281b1b552162438e313c001' }, { address: '16TaQJi78o4A3nKDSzswqZiX3bhecNuNBQ', skHex: '58f7b29ee4a9a8b05855591b8a5405a0647c74c0a539515173adb9a32c964a9a01' }, { address: '15eNSvgT3UFvHSonajxFswnmHFifJPE5LB', skHex: 'f5360140d18c6a34fbd2c45b98c1857c3fdad5454350249688a90efe936d475101' }, { address: '1Lt8ajRt7i8ajkQsYQZbk3ULCVTsSn2TNV', skHex: '6eaed28d7f26f57fac925283aa0fe49c031028212863219f1c0141e4b0de2b2d01' }, { address: '1GvM4xksXrQsq4xPRck11toRLXVq9UYj2B', skHex: '4c103c5c3de544c90f18a3ed29aaeebd33feedb1bb4f026df24aa3eddae826aa01' }]; function networkTests() { (0, _tape2.default)('insight-client', function (t) { t.plan(5); var mynet = new _network.InsightClient('https://utxo.tester.com'); _fetchMock2.default.restore(); _fetchMock2.default.get('https://bitcoinfees.earn.com/api/v1/fees/recommended', { fastestFee: 1000 }); var txhashFound = 'txhash-found'; var blockHash = 'block-hash'; var txhashNotFound = 'txhash-not-found'; _fetchMock2.default.get('https://utxo.tester.com/tx/' + txhashNotFound, { body: JSON.stringify({ message: 'error fetching transaction details', error: '-5: No information available about transaction' }), status: 400 }); _fetchMock2.default.get('https://utxo.tester.com/tx/' + txhashFound, { blockHash: blockHash }); _fetchMock2.default.get('https://utxo.tester.com/block/' + blockHash, { height: 300 }); _fetchMock2.default.get('https://utxo.tester.com/addr/' + testAddresses[0].address + '/utxo', [{ value: 1, satoshis: 1e8, confirmations: 2, txid: 'bar', vout: 10 }]); _fetchMock2.default.get('https://utxo.tester.com/status', { blocks: 500 }); _fetchMock2.default.post('https://utxo.tester.com/tx/send', { body: 'true', status: 202 }); mynet.broadcastTransaction('test-transaction-text').then(function (response) { t.ok(response, 'Should broadcast successfully'); }); mynet.getBlockHeight().then(function (response) { t.equal(response, 500, 'Should return block height'); }); mynet.getTransactionInfo(txhashNotFound).then(function () { return t.ok(false, 'Should not return txinfo for not-found transaction.'); }).catch(function () { return t.ok(true, 'Should throw exception for not-found transaction.'); }); mynet.getTransactionInfo(txhashFound).then(function (txInfo) { return t.equal(txInfo.block_height, 300, 'Should return txinfo.block_height'); }).catch(function () { return t.ok(false, 'Should not throw exception for a found transaction.'); }); mynet.getNetworkedUTXOs(testAddresses[0].address).then(function (utxos) { t.deepEqual(utxos, [{ value: 1e8, confirmations: 2, tx_hash: 'bar', tx_output_n: 10 }]); }); }); (0, _tape2.default)('bitcoind-client', function (t) { t.plan(2); var mynet = new _network.BitcoindAPI('https://utxo.tester.com', { username: 'foo', password: 'bar' }); _fetchMock2.default.restore(); _fetchMock2.default.postOnce({ name: 'Broadcast', matcher: function matcher(url, opts) { return url === 'https://utxo.tester.com' && opts && opts.body.indexOf('importaddress') > 0; }, response: { body: {}, status: 200 } }); _fetchMock2.default.post({ name: 'Broadcast', matcher: function matcher(url, opts) { return url === 'https://utxo.tester.com' && opts && opts.body.indexOf('listunspent') > 0; }, response: { body: JSON.stringify({ result: [] }), status: 200 } }); mynet.getNetworkedUTXOs(testAddresses[0].address).then(function (utxos) { t.deepEqual(utxos, []); }).catch(function (err) { console.log(err); t.fail(); }).then(function () { return mynet.getNetworkedUTXOs(testAddresses[0].address); }).then(function (utxos) { t.deepEqual(utxos, []); }).catch(function (err) { console.log(err); t.fail(); }); }); } function utilsTests() { (0, _tape2.default)('estimateTXBytes', function (t) { t.plan(2); var txHex = '010000000288e68977fab8038af07746e5d687652a44aa15f532509c202749d' + 'bad8a418733000000006b483045022100813ef3534b5030b544e5a5bd1db93f85dc89e2' + 'a565197a14784edff5564bd65b022008005213c6aa4c7ebe06cfd86bdaf3e662ae58371' + '896a0a841e81106fbe1507401210236b07942707a86ab666bb300b58d295d988ce9c3a3' + '38a0e08380dd98732fd4faffffffff3ba3edfd7a7b12b27ac72c3e67768f617fc81bc38' + '88a51323a9fb8aa4b1e5e4a000000006b483045022100d0c9b1594137186a1dc6c0b3a6' + 'cbe08399b57e2b8c953584f2ce20bef5642eb902206b9c88b8d2d311db26601acf3068d' + 'd118649ead4a1f93d029a52c0c61cb2cd2901210236b07942707a86ab666bb300b58d29' + '5d988ce9c3a338a0e08380dd98732fd4faffffffff030000000000000000296a2769643' + 'f363da95bc8d5203d1c07bd87c564a1e6395826cfdfe87cfd31ffa2a3b8101e3e93096f' + '2b7c150000000000001976a91441577ec99314a293acbc17d8152137cf4862f7f188ace' + '8030000000000001976a9142ebe7b4729185f68c7185c3c6af60fad1b6eeebf88ac00000000'; var tx = _bitcoinjsLib2.default.Transaction.fromHex(txHex); tx.ins.forEach(function (x) { x.script = null; }); var actualLength = txHex.length / 2; var estimatedLength = (0, _utils.estimateTXBytes)(tx, 0, 0); var tx2 = new _bitcoinjsLib2.default.TransactionBuilder(); tx2.addOutput(tx.outs[0].script, 0); var estimatedLength2 = (0, _utils.estimateTXBytes)(tx2, 2, 2); t.ok(estimatedLength >= actualLength - 5 && estimatedLength <= actualLength + 5, 'TX size estimate is roughly accurate? (estimated: ' + estimatedLength + ',\n actual: ' + actualLength + ')'); t.ok(estimatedLength2 >= actualLength - 5 && estimatedLength2 <= actualLength + 5, 'TX size estimate is roughly accurate? (estimated: ' + estimatedLength2 + ',\n actual: ' + actualLength + ')'); }); (0, _tape2.default)('encoding routines', function (t) { t.plan(5); t.equal((0, _utils.hash160)(Buffer.from('99999566ahjhqwuywqehpzlzlzlzl09189128921jkjlqjosq')).toString('hex'), '7ea1fa0f2003c31b015a72af9f4a5f104b5c2840'); t.equal((0, _utils.hash160)(Buffer.from('1234')).toString('hex'), 'fd7a0d80999bedd76c9a0828057817fc6049a507'); t.equal((0, _utils.hash128)(Buffer.from('999')).toString('hex'), '83cf8b609de60036a8277bd0e9613575'); t.equal((0, _utils.hash128)(Buffer.from('99999566ahjhqwuywqehpzlzlzlzl09189128921jkjlqjosqaaa')).toString('hex'), '740ae7f18c939cf5e7c189a2c77a012f'); t.equal((0, _utils.decodeB40)('0123456789abcdefghijklmnopqrstuvwxyz-_.+0123456789abcdefghi' + 'jklmnopqrstuvwxyz-_.+0123456789abcdefghijklmnopqrstuvwxyz-_' + '.+0123456789abcdefghijklmnopqrstuvwxyz-_.+0123456789abcdefg' + 'hijklmnopqrstuvwxyz-_.+'), '384a516059e707615a1992d3101f6f346df3326d03ea7b673e3754078895db48da2d0' + 'fcb1bd89d618b0863bd8bac6db43a2d9cff5cc307310922d3cb8cf9c159d31c6a9c91' + '03197263a4e88f52d1b77dfc610e1b8dc9616ba6c2d0a1b792f0d73784c698c69f34a' + 'e5e7900753627a3ac87529035fb1a6cba7ce2e1df590941cf30a44557'); }); (0, _tape2.default)('not enough UTXOs to fund', function (t) { t.plan(1); var txB = new _bitcoinjsLib2.default.TransactionBuilder(); txB.addOutput(testAddresses[0].address, 10000); txB.addOutput(testAddresses[1].address, 0); var utxos = [{ value: 50000, tx_hash: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', tx_output_n: 0 }]; t.throws(function () { return (0, _utils.addUTXOsToFund)(txB, utxos, 60000, 10); }, /^NotEnoughFundsError: Not enough UTXOs to fund./, 'Errors when not enough value to fund'); }); (0, _tape2.default)('addUTXOsToFundSingleUTXO', function (t) { t.plan(2); var txB = new _bitcoinjsLib2.default.TransactionBuilder(); txB.addOutput(testAddresses[0].address, 10000); txB.addOutput(testAddresses[1].address, 0); var utxos = [{ value: 50000, tx_hash: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', tx_output_n: 0 }]; var change = (0, _utils.addUTXOsToFund)(txB, utxos, 10000, 10); t.equal(change, 38520); // gots to pay the fee! t.equal(txB.__tx.ins[0].hash.toString('hex'), Buffer.from(utxos[0].tx_hash, 'hex').reverse().toString('hex')); }); (0, _tape2.default)('addUTXOsToFundTwoUTXOs', function (t) { t.plan(3); var txB = new _bitcoinjsLib2.default.TransactionBuilder(); txB.addOutput(testAddresses[0].address, 10000); txB.addOutput(testAddresses[1].address, 0); var utxos = [{ value: 50000, tx_hash: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', tx_output_n: 0 }, { value: 10000, tx_hash: '3387418aaddb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e688', tx_output_n: 0 }]; var change = (0, _utils.addUTXOsToFund)(txB, utxos, 55000, 10); t.ok(change <= 5000, txB.__tx.outs[1].value + ' should be less than 5k'); t.equal(txB.__tx.ins[0].hash.toString('hex'), Buffer.from(utxos[0].tx_hash, 'hex').reverse().toString('hex')); t.equal(txB.__tx.ins[1].hash.toString('hex'), Buffer.from(utxos[1].tx_hash, 'hex').reverse().toString('hex')); }); (0, _tape2.default)('modifiedTXSets', function (t) { t.plan(11); var txStarterHex = '01000000013ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323' + 'a9fb8aa4b1e5e4a000000006a473044022050176492b92c79ba' + '23fb815e62a7778ccb45a50ca11b8dabdbadc1828e6ba34002200ce770' + '82a072eba8d3ce49e6a316e6173c1f97d955064574fe620cc25002eadb' + '01210236b07942707a86ab666bb300b58d295d988ce9c3a338a0e08380' + 'dd98732fd4faffffffff030000000000000000296a2769643f363da95b' + 'c8d5203d1c07bd87c564a1e6395826cfdfe87cfd31ffa2a3b8101e3e93' + '096f2be02c0000000000001976a91441577ec99314a293acbc17d81521' + '37cf4862f7f188ac39050000000000001976a9142ebe7b4729185f68c7' + '185c3c6af60fad1b6eeebf88ac00000000'; var txStarter = _bitcoinjsLib2.default.Transaction.fromHex(txStarterHex); var txHash = '22a024f16944d2f568de4a613566fcfab53b86d37f1903668d399f9a366883de'; t.equal(txStarter.getHash().reverse().toString('hex'), txHash); var usedTXHash = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'; var utxoValues = [287825, 287825]; var utxoSet1 = [{ value: utxoValues[0], tx_hash_big_endian: usedTXHash, tx_output_n: 0 }, { value: utxoValues[1], tx_hash_big_endian: '3387418aaddb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e688', tx_output_n: 0 }]; var utxoSet2 = []; _lib.config.network.modifyUTXOSetFrom(txStarterHex); var testAddress1 = '16xVjkJ3nY62B9t9q3N9wY6hx1duAfwRZR'; var testAddress2 = '15GAGiT2j2F1EzZrvjk3B8vBCfwVEzQaZx'; _fetchMock2.default.restore(); _fetchMock2.default.get('https://bitcoinfees.earn.com/api/v1/fees/recommended', { fastestFee: 1000 }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddress1 + '&cors=true', { unspent_outputs: utxoSet1 }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddress2 + '&cors=true', { unspent_outputs: utxoSet2 }); Promise.all([_lib.config.network.getUTXOs(testAddress1), _lib.config.network.getUTXOs(testAddress2)]).then(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), utxos1 = _ref2[0], utxos2 = _ref2[1]; t.equal(utxos1.length, 2); t.equal(utxos2.length, 1); t.ok(utxos1.find(function (x) { return x.tx_hash === txHash && x.value === 11488; }), 'UTXO set should include the new transaction\'s outputs'); t.ok(utxos2.find(function (x) { return x.tx_hash === txHash && x.value === 1337; }), 'UTXO set should include the new transaction\'s outputs'); t.ok(!utxos1.find(function (x) { return x.tx_hash === usedTXHash; }), 'UTXO set shouldn\'t include the transaction\'s spent input'); }).then(function () { _lib.config.network.resetUTXOs(testAddress1); _lib.config.network.resetUTXOs(testAddress2); return Promise.all([_lib.config.network.getUTXOs(testAddress1), _lib.config.network.getUTXOs(testAddress2)]); }).then(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2), utxos1 = _ref4[0], utxos2 = _ref4[1]; t.equal(utxos1.length, 2); t.equal(utxos2.length, 0); t.ok(!utxos1.find(function (x) { return x.tx_hash === txHash && x.value === 11488; }), 'UTXO set should not include the new transaction\'s outputs after reset'); t.ok(!utxos2.find(function (x) { return x.tx_hash === txHash && x.value === 1337; }), 'UTXO set should not include the new transaction\'s outputs after reset'); t.ok(utxos1.find(function (x) { return x.tx_hash === usedTXHash; }), 'UTXO set should include the transaction\'s input after reset'); }); }); } function transactionTests() { var utxoValues = [288000, 287825, 287825]; var namespaceUtxoValues = [288000, 287825, 287825]; var tokenUtxoValues = [288000, 287825, 287825]; var BURN_AMT = 6500; var NAMESPACE_PRICE = { units: 'STACKS', amount: '6400000000' }; var BURN_ADDR = '15GAGiT2j2F1EzZrvjk3B8vBCfwVEzQaZx'; var NAMESPACE_BURN_ADDR = '1111111111111111111114oLvT2'; var utxoSet = [{ value: utxoValues[0], tx_hash_big_endian: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', tx_output_n: 0 }, { value: utxoValues[1], tx_hash_big_endian: '3387418aaddb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e688', tx_output_n: 0 }, { value: utxoValues[2], tx_hash_big_endian: 'ffffffffffdb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e688', tx_output_n: 2 }]; var utxoSet2 = [{ value: 5500, tx_hash_big_endian: 'ffffffffaab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdedffff', tx_output_n: 0 }]; var namespaceUtxoSet = [{ value: namespaceUtxoValues[0], tx_hash_big_endian: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33c', tx_output_n: 0 }, { value: namespaceUtxoValues[1], tx_hash_big_endian: '3387418aaddb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e689', tx_output_n: 0 }, { value: namespaceUtxoValues[2], tx_hash_big_endian: 'ffffffffffdb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e689', tx_output_n: 2 }]; var namespaceUtxoSet2 = [{ value: 654321, tx_hash_big_endian: 'ffffffffaab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdee0000', tx_output_n: 0 }]; var tokenUtxoSet = [{ value: tokenUtxoValues[0], tx_hash_big_endian: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33c', tx_output_n: 0 }, { value: tokenUtxoValues[1], tx_hash_big_endian: '3387418aaddb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e689', tx_output_n: 0 }, { value: tokenUtxoValues[2], tx_hash_big_endian: 'ffffffffffdb4927209c5032f515aa442a6587d6e54677f08a03b8fa7789e689', tx_output_n: 2 }]; var tokenUtxoSet2 = [{ value: 1654321, tx_hash_big_endian: 'fefefefeaab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdee0000', tx_output_n: 0 }]; function setupMocks() { _fetchMock2.default.restore(); _fetchMock2.default.get('https://bitcoinfees.earn.com/api/v1/fees/recommended', { fastestFee: 1000 }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddresses[1].address + '&cors=true', { unspent_outputs: utxoSet }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddresses[0].address + '&cors=true', { unspent_outputs: utxoSet2 }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddresses[2].address + '&cors=true', { unspent_outputs: namespaceUtxoSet }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddresses[3].address + '&cors=true', { unspent_outputs: namespaceUtxoSet2 }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddresses[4].address + '&cors=true', { unspent_outputs: tokenUtxoSet }); _fetchMock2.default.get('https://blockchain.info/unspent?format=json&active=' + testAddresses[5].address + '&cors=true', { unspent_outputs: tokenUtxoSet2 }); _fetchMock2.default.get('https://core.blockstack.org/v2/prices/names/foo.test', { name_price: { units: 'BTC', amount: String(BURN_AMT) } }); _fetchMock2.default.get('https://core.blockstack.org/v2/prices/names/bar.test2', { name_price: { units: 'STACKS', amount: String(BURN_AMT) } }); _fetchMock2.default.get('https://core.blockstack.org/v2/prices/namespaces/hello', NAMESPACE_PRICE); _fetchMock2.default.get('https://core.blockstack.org/v1/namespaces/test', { version: 2, address: BURN_ADDR, reveal_block: 600 }); _fetchMock2.default.get('https://core.blockstack.org/v1/namespaces/test2', { version: 3, address: BURN_ADDR, reveal_block: 600 }); _fetchMock2.default.get('https://core.blockstack.org/v1/blockchains/bitcoin/consensus', { consensus_hash: 'dfe87cfd31ffa2a3b8101e3e93096f2b' }); _fetchMock2.default.get('https://blockchain.info/latestblock?cors=true', { height: 601 }); } function getInputVals(inputTXArgument) { var utxoSets = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : utxoSet; var utxosAll = utxoSets.concat(); return inputTXArgument.ins.reduce(function (agg, x) { var inputTX = utxosAll.find(function (y) { return Buffer.from(y.tx_hash_big_endian, 'hex').reverse().compare(x.hash) === 0; }); if (inputTX) { return agg + inputTX.value; } else { return agg; } }, 0); } (0, _tape2.default)('address coercion', function (t) { t.plan(8); var stashed = _lib.config.network.layer1; try { var singleSigAddressMain = '1EJh2y3xKUwFjJ8v29a2NRruPJ71neozEE'; var singleSigAddressTest = 'mtpeL28w8WNWWQcXjiYQCM5EFHhijXMF62'; var multiSigAddressTest = '2N6GHvciC9M4ze5QXpW2jZxjf5trnPFsyqZ'; var multiSigAddressMain = '3Ei5rsnAXtZeSHmz9NQrx1kPsYecbfdKiy'; _lib.config.network.layer1 = _bitcoinjsLib2.default.networks.testnet; t.equal(_lib.config.network.coerceAddress(singleSigAddressMain), singleSigAddressTest); t.equal(_lib.config.network.coerceAddress(singleSigAddressTest), singleSigAddressTest); t.equal(_lib.config.network.coerceAddress(multiSigAddressMain), multiSigAddressTest); t.equal(_lib.config.network.coerceAddress(multiSigAddressTest), multiSigAddressTest); _lib.config.network.layer1 = _bitcoinjsLib2.default.networks.bitcoin; t.equal(_lib.config.network.coerceAddress(singleSigAddressMain), singleSigAddressMain); t.equal(_lib.config.network.coerceAddress(singleSigAddressTest), singleSigAddressMain); t.equal(_lib.config.network.coerceAddress(multiSigAddressMain), multiSigAddressMain); t.equal(_lib.config.network.coerceAddress(multiSigAddressTest), multiSigAddressMain); } finally { _lib.config.network.layer1 = stashed; } }); (0, _tape2.default)('build incomplete', function (t) { setupMocks(); t.plan(2); var getAddress = function getAddress() { return Promise.resolve(testAddresses[2].address); }; var signTransaction = function signTransaction() { return Promise.resolve(); }; var nullSigner = { getAddress: getAddress, signTransaction: signTransaction }; return _lib.transactions.makeNamespacePreorder('hello', testAddresses[3].address, nullSigner).then(function () { t.fail('Should have failed to build unsigned TX.'); }).catch(function () { t.pass('Should have failed to build unsigned TX.'); }).then(function () { return _lib.transactions.makeNamespacePreorder('hello', testAddresses[3].address, nullSigner, true); }).then(function (txhex) { t.ok(txhex, 'Should have built incomplete TX when buildIncomplete = true'); }).catch(function (err) { t.fail('Should have built incomplete TX when buildIncomplete = true: Error: ' + err); }); }); (0, _tape2.default)('build and fund namespace preorder', function (t) { t.plan(6); setupMocks(); Promise.all([_lib.transactions.estimateNamespacePreorder('hello', testAddresses[3].address, testAddresses[2].address, 2), _lib.transactions.makeNamespacePreorder('hello', testAddresses[3].address, testAddresses[2].skHex)]).then(function (_ref5) { var _ref6 = _slicedToArray(_ref5, 2), estimatedCost = _ref6[0], hexTX = _ref6[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx, namespaceUtxoSet); var fee = inputVals - outputVals; var burnAddress = _bitcoinjsLib2.default.address.fromOutputScript(tx.outs[2].script); var change = tx.outs[1].value; t.equal(inputVals - change, estimatedCost - 5500, 'Estimated cost should be +DUST_MINIMUM of actual.'); t.equal(burnAddress, NAMESPACE_BURN_ADDR, 'Burn address should be ' + NAMESPACE_BURN_ADDR); t.equal(tx.outs[2].value, 5500, 'Output should have paid +DUST_MINIMUM for namespace'); t.equal(tx.ins.length, 2, 'Should use 2 utxos for the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund namespace reveal', function (t) { t.plan(4); setupMocks(); var ns = new _lib.transactions.BlockstackNamespace('hello'); ns.setVersion(3); ns.setLifetime(52595); ns.setCoeff(4); ns.setBase(4); ns.setBuckets([6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); ns.setNonalphaDiscount(10); ns.setNoVowelDiscount(10); Promise.all([_lib.transactions.estimateNamespaceReveal(ns, testAddresses[3].address, testAddresses[2].address), _lib.transactions.makeNamespaceReveal(ns, testAddresses[3].address, testAddresses[2].skHex)]).then(function (_ref7) { var _ref8 = _slicedToArray(_ref7, 2), estimatedCost = _ref8[0], hexTX = _ref8[1]; var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx, namespaceUtxoSet); var fee = inputVals - outputVals; // change address is the 3rd output usually... var change = tx.outs[2].value; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[2].script), testAddresses[2].address, 'Payer change should be third output'); t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 1, 'Should use 1 utxo for the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund name import', function (t) { t.plan(4); setupMocks(); Promise.all([_lib.transactions.estimateNameImport('import.hello', '151nahdGD9Dxd7xpwPeBECn5iEi4Thb7Rv', 'cabdbc18ece9ffb6a7378faa4ac4ce58dcaaf575'), _lib.transactions.makeNameImport('import.hello', '151nahdGD9Dxd7xpwPeBECn5iEi4Thb7Rv', 'cabdbc18ece9ffb6a7378faa4ac4ce58dcaaf575', testAddresses[3].skHex)]).then(function (_ref9) { var _ref10 = _slicedToArray(_ref9, 2), estimatedCost = _ref10[0], hexTX = _ref10[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx, namespaceUtxoSet2); var fee = inputVals - outputVals; var change = tx.outs[3].value; t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 1, 'Should use 1 utxo for the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund namespace ready', function (t) { t.plan(4); setupMocks(); Promise.all([_lib.transactions.estimateNamespaceReady('hello'), _lib.transactions.makeNamespaceReady('hello', testAddresses[3].skHex)]).then(function (_ref11) { var _ref12 = _slicedToArray(_ref11, 2), estimatedCost = _ref12[0], hexTX = _ref12[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx, namespaceUtxoSet2); var fee = inputVals - outputVals; var change = tx.outs[1].value; t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 1, 'Should use 1 utxo for the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund announce', function (t) { t.plan(4); setupMocks(); Promise.all([_lib.transactions.estimateAnnounce('53bb740c47435a51b07ecf0b9e086a2ad3c12c1d'), _lib.transactions.makeAnnounce('53bb740c47435a51b07ecf0b9e086a2ad3c12c1d', testAddresses[3].skHex)]).then(function (_ref13) { var _ref14 = _slicedToArray(_ref13, 2), estimatedCost = _ref14[0], hexTX = _ref14[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx, namespaceUtxoSet2); var fee = inputVals - outputVals; var change = tx.outs[1].value; t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 1, 'Should use 1 utxo for the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund token transfer', function (t) { t.plan(6); setupMocks(); Promise.all([_lib.transactions.estimateTokenTransfer(testAddresses[1].address, 'STACKS', _bigi2.default.fromByteArrayUnsigned('123'), 'hello world!', 2), _lib.transactions.makeTokenTransfer(testAddresses[1].address, 'STACKS', _bigi2.default.fromByteArrayUnsigned('123'), 'hello world!', testAddresses[4].skHex)]).then(function (_ref15) { var _ref16 = _slicedToArray(_ref15, 2), estimatedCost = _ref16[0], hexTX = _ref16[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx, tokenUtxoSet); var fee = inputVals - outputVals; var change = tx.outs[2].value; var recipientAddr = _bitcoinjsLib2.default.address.fromOutputScript(tx.outs[1].script); console.log(outputVals); console.log(inputVals); console.log(fee); console.log(change); console.log(recipientAddr); t.equal(inputVals - change, estimatedCost, 'Estimated cost should be equal'); t.equal(recipientAddr, testAddresses[1].address, 'Recipient address is correct'); t.equal(tx.outs[1].value, 5500, 'Recipient address should have +DUST_MINIMUM'); t.equal(tx.ins.length, 2, 'Should use 2 utxos from the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx length ' + txLen + ' should equal 1K satoshi/byte'); }); }); (0, _tape2.default)('build and fund token transfer with separate funder', function (t) { t.plan(6); setupMocks(); Promise.all([_lib.transactions.estimateTokenTransfer(testAddresses[1].address, 'STACKS', _bigi2.default.fromByteArrayUnsigned('123'), 'hello world!', 2, 2), _lib.transactions.makeTokenTransfer(testAddresses[1].address, 'STACKS', _bigi2.default.fromByteArrayUnsigned('123'), 'hello world!', testAddresses[4].skHex, testAddresses[5].skHex)]).then(function (_ref17) { var _ref18 = _slicedToArray(_ref17, 2), estimatedCost = _ref18[0], hexTX = _ref18[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx, tokenUtxoSet) + getInputVals(tx, tokenUtxoSet2); var fee = inputVals - outputVals; var change = tx.outs[3].value; var recipientAddr = _bitcoinjsLib2.default.address.fromOutputScript(tx.outs[1].script); var funderInputVals = getInputVals(tx, tokenUtxoSet2); t.equal(funderInputVals - change, estimatedCost, 'Estimated cost should be equal'); t.equal(recipientAddr, testAddresses[1].address, 'Recipient address is correct'); t.equal(tx.outs[1].value, 5500, 'Recipient address should have +DUST_MINIMUM'); t.equal(tx.ins.length, 2, 'Should use 2 utxos from (token-payer, btc-payer)'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx length ' + txLen + ' should equal 1K satoshi/byte'); }).catch(function (err) { console.log(err); t.fail(err); }); }); (0, _tape2.default)('build and fund preorder', function (t) { t.plan(6); setupMocks(); Promise.all([_lib.transactions.estimatePreorder('foo.test', testAddresses[0].address, testAddresses[1].address), _lib.transactions.makePreorder('foo.test', testAddresses[0].address, testAddresses[1].skHex)]).then(function (_ref19) { var _ref20 = _slicedToArray(_ref19, 2), estimatedCost = _ref20[0], hexTX = _ref20[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; var burnAddress = _bitcoinjsLib2.default.address.fromOutputScript(tx.outs[2].script); var change = tx.outs[1].value; t.equal(inputVals - change, estimatedCost - 5500, 'Estimated cost should be +DUST_MINIMUM of actual.'); t.equal(burnAddress, BURN_ADDR, 'Burn address should be ' + BURN_ADDR); t.equal(tx.outs[2].value, BURN_AMT, 'Output should have funded name price ' + BURN_AMT); t.equal(tx.ins.length, 1, 'Should use 1 utxo for the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund preorder with stacks', function (t) { t.plan(6); setupMocks(); Promise.all([_lib.transactions.estimatePreorder('bar.test2', testAddresses[0].address, testAddresses[1].address, 2), _lib.transactions.makePreorder('bar.test2', testAddresses[0].address, testAddresses[1].skHex)]).then(function (_ref21) { var _ref22 = _slicedToArray(_ref21, 2), estimatedCost = _ref22[0], hexTX = _ref22[1]; t.ok(hexTX); var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; var burnAddress = _bitcoinjsLib2.default.address.fromOutputScript(tx.outs[2].script); var change = tx.outs[1].value; t.equal(inputVals - change, estimatedCost - 5500, 'Estimated cost should be +DUST_MINIMUM of actual.'); t.equal(burnAddress, NAMESPACE_BURN_ADDR, 'Burn address should be ' + NAMESPACE_BURN_ADDR); t.equal(tx.outs[2].value, 5500, 'Output should not have burned more than +DUST_MINIMUM'); t.equal(tx.ins.length, 2, 'Should use 2 utxos for the payer'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund register', function (t) { t.plan(4); setupMocks(); Promise.all([_lib.transactions.estimateRegister('foo.test', testAddresses[0].address, testAddresses[1].address, true, 2), _lib.transactions.makeRegister('foo.test', testAddresses[0].address, testAddresses[1].skHex, 'hello world')]).then(function (_ref23) { var _ref24 = _slicedToArray(_ref23, 2), estimatedCost = _ref24[0], hexTX = _ref24[1]; var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; // change address is the 3rd output usually... var change = tx.outs[2].value; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[2].script), testAddresses[1].address, 'Payer change should be third output'); t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 2, 'Should use both payer utxos'); t.equal(Math.floor(fee / txLen), 1000, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund update', function (t) { t.plan(5); setupMocks(); Promise.all([_lib.transactions.estimateUpdate('foo.test', testAddresses[0].address, testAddresses[1].address, 3), _lib.transactions.makeUpdate('foo.test', testAddresses[0].skHex, testAddresses[1].skHex, 'hello world')]).then(function (_ref25) { var _ref26 = _slicedToArray(_ref25, 2), estimatedCost = _ref26[0], hexTX = _ref26[1]; var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; // payer change address is the 3rd output... var changeOut = tx.outs[2]; var ownerChange = tx.outs[1]; var change = changeOut.value; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(changeOut.script), testAddresses[1].address, 'Owner change should be second output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(ownerChange.script), testAddresses[0].address, 'Payer change should be third output'); t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 4, 'Should use all payer utxos and one owner utxo'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should roughly equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund transfer', function (t) { t.plan(6); setupMocks(); Promise.all([_lib.transactions.estimateTransfer('foo.test', testAddresses[2].address, testAddresses[0].address, testAddresses[1].address, 3), _lib.transactions.makeTransfer('foo.test', testAddresses[2].address, testAddresses[0].skHex, testAddresses[1].skHex)]).then(function (_ref27) { var _ref28 = _slicedToArray(_ref27, 2), estimatedCost = _ref28[0], hexTX = _ref28[1]; var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; // payer change address is the 4th output... var changeOut = tx.outs[3]; // old owner change address is the 3rd output var ownerChange = tx.outs[2]; var change = changeOut.value; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[1].script), testAddresses[2].address, 'New owner should be second output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(ownerChange.script), testAddresses[0].address, 'Prior owner should be third output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(changeOut.script), testAddresses[1].address, 'Payer change should be fourth output'); t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 4, 'Should use both payer utxos and one owner utxo'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should roughly equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund revoke', function (t) { t.plan(4); setupMocks(); Promise.all([_lib.transactions.estimateRevoke('foo.test', testAddresses[0].address, testAddresses[1].address, 2), _lib.transactions.makeRevoke('foo.test', testAddresses[0].skHex, testAddresses[1].skHex)]).then(function (_ref29) { var _ref30 = _slicedToArray(_ref29, 2), estimatedCost = _ref30[0], hexTX = _ref30[1]; var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; // change address is the 3rd output usually... var change = tx.outs[2].value; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[2].script), testAddresses[1].address, 'Payer change should be third output'); t.equal(inputVals - change, estimatedCost, 'Estimated cost should match actual.'); t.equal(tx.ins.length, 3, 'Should use both payer utxos'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('fund bitcoin spends', function (t) { t.plan(14); setupMocks(); var TEST1_AMOUNT = 250000; var TEST2_AMOUNT = 80000; var TEST3_AMOUNT = 288000 + 287825 + 287825; var TEST4_AMOUNT = 288000 + 287825 + 287825 + 1; _lib.transactions.makeBitcoinSpend(testAddresses[2].address, testAddresses[1].skHex, TEST1_AMOUNT).then(function (hexTX) { var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; t.equal(tx.ins.length, 1, 'Should use 1 input'); t.equal(tx.outs.length, 2, 'Should have a change output'); var changeOut = tx.outs[1]; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(changeOut.script), testAddresses[1].address, 'Must be correct change address'); t.ok(Math.abs(1000 * txLen - fee) <= 2000, 'Fee should be roughly correct: Actual fee: ' + fee + ', expected: ' + 1000 * txLen); t.equal(inputVals - changeOut.value, TEST1_AMOUNT, 'Should fund correct amount'); }).then(function () { return _lib.transactions.makeBitcoinSpend(testAddresses[2].address, testAddresses[1].skHex, TEST2_AMOUNT); }).then(function () { return t.fail('Should reject with InvalidAmountError if not enough coin to fund fees.'); }).catch(function (err) { return t.equal(err.name, 'InvalidAmountError', 'Should reject with InvalidAmountError if not enough coin to fund fees.'); }).then(function () { return _lib.transactions.makeBitcoinSpend(testAddresses[2].address, testAddresses[1].skHex, TEST3_AMOUNT); }).then(function (hexTX) { var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; t.equal(tx.ins.length, 3, 'Should use 3 inputs'); t.equal(tx.outs.length, 1, 'Should not have a change output'); t.ok(Math.abs(1000 * txLen - fee) <= 2000, 'Fee should be roughly correct.'); t.equal(outputVals + fee, TEST3_AMOUNT, 'Should fund correct amount'); }).then(function () { return _lib.transactions.makeBitcoinSpend(testAddresses[2].address, testAddresses[1].skHex, TEST4_AMOUNT); }).then(function (hexTX) { var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; t.equal(tx.ins.length, 3, 'Should use 3 inputs'); t.equal(tx.outs.length, 1, 'Should not have a change output'); t.ok(Math.abs(1000 * txLen - fee) <= 2000, 'Fee should be roughly correct.'); t.equal(outputVals + fee, TEST3_AMOUNT, 'Should fund maximum amount'); }); }); (0, _tape2.default)('build and fund renewal', function (t) { t.plan(7); setupMocks(); Promise.all([_lib.transactions.estimateRenewal('foo.test', testAddresses[2].address, testAddresses[0].address, testAddresses[1].address, true, 3), _lib.transactions.makeRenewal('foo.test', testAddresses[2].address, testAddresses[0].skHex, testAddresses[1].skHex, 'hello world')]).then(function (_ref31) { var _ref32 = _slicedToArray(_ref31, 2), estimatedCost = _ref32[0], hexTX = _ref32[1]; var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; // payer change address is the 5th output... var changeOut = tx.outs[4]; // old owner change address is the 3rd output var ownerChange = tx.outs[2]; var change = changeOut.value; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[1].script), testAddresses[2].address, 'New owner should be second output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(ownerChange.script), testAddresses[0].address, 'Prior owner should be third output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[3].script), BURN_ADDR, 'Burn address should be fourth output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(changeOut.script), testAddresses[1].address, 'Payer change should be fifth output'); t.equal(inputVals - change, estimatedCost, 'Estimated cost should be accurate.'); t.equal(tx.ins.length, 4, 'Should use both payer utxos and one owner utxo'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should roughly equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('build and fund renewal with stacks', function (t) { t.plan(8); setupMocks(); Promise.all([_lib.transactions.estimateRenewal('bar.test2', testAddresses[2].address, testAddresses[0].address, testAddresses[1].address, true, 3), _lib.transactions.makeRenewal('bar.test2', testAddresses[2].address, testAddresses[0].skHex, testAddresses[1].skHex, 'hello world')]).then(function (_ref33) { var _ref34 = _slicedToArray(_ref33, 2), estimatedCost = _ref34[0], hexTX = _ref34[1]; var tx = _bitcoinjsLib2.default.Transaction.fromHex(hexTX); var txLen = hexTX.length / 2; var outputVals = (0, _utils.sumOutputValues)(tx); var inputVals = getInputVals(tx); var fee = inputVals - outputVals; // payer change address is the 5th output... var changeOut = tx.outs[4]; // old owner change address is the 3rd output var ownerChange = tx.outs[2]; var change = changeOut.value; t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[1].script), testAddresses[2].address, 'New owner should be second output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(ownerChange.script), testAddresses[0].address, 'Prior owner should be third output'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(tx.outs[3].script), NAMESPACE_BURN_ADDR, 'Burn address should be fourth output, and it should be 11111....'); t.equal(_bitcoinjsLib2.default.address.fromOutputScript(changeOut.script), testAddresses[1].address, 'Payer change should be fifth output'); t.equal(inputVals - change, estimatedCost, 'Estimated cost should be accurate.'); t.equal(tx.ins.length, 4, 'Should use both payer utxos and one owner utxo'); t.equal(tx.outs[3].value, 5500, 'Output should not have burned more than +DUST_MINIMUM'); t.ok(Math.floor(fee / txLen) > 990 && Math.floor(fee / txLen) < 1010, 'Paid fee of ' + fee + ' for tx of length ' + txLen + ' should roughly equal 1k satoshi/byte'); }).catch(function (err) { console.log(err.stack);throw err; }); }); (0, _tape2.default)('use alternative magic bytes', function (t) { t.plan(24); setupMocks(); var ns = new _lib.transactions.BlockstackNamespace('hello'); ns.setVersion(3); ns.setLifetime(52595); ns.setCoeff(4); ns.setBase(4); ns.setBuckets([6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); ns.setNonalphaDiscount(10); ns.setNoVowelDiscount(10); Promise.resolve().then(function () { _network.network.defaults.MAINNET_DEFAULT.MAGIC_BYTES = 'di'; return Promise.all([_lib.transactions.makeNamespacePreorder('hello', testAddresses[3].address, testAddresses[2].skHex), _lib.transactions.makeNamespaceReveal(ns, testAddresses[3].address, testAddresses[2].skHex), _lib.transactions.makeNameImport('import.hello', '151nahdGD9Dxd7xpwPeBECn5iEi4Thb7Rv', 'cabdbc18ece9ffb6a7378faa4ac4ce58dcaaf575', testAddresses[3].skHex), _lib.transactions.makeNamespaceReady('hello', testAddresses[3].skHex), _lib.transactions.makePreorder('foo.test', testAddresses[0].address, testAddresses[1].skHex), _lib.transactions.makeRegister('foo.test', testAddresses[0].address, testAddresses[1].skHex, 'hello world'), _lib.transactions.makeUpdate('foo.test', testAddresses[0].skHex, testAddresses[1].skHex, 'hello world'), _lib.transactions.makeTransfer('foo.test', testAddresses[2].address, testAddresses[0].skHex, testAddresses[1].skHex), _lib.transactions.makeRenewal('foo.test', testAddresses[2].address, testAddresses[0].skHex, testAddresses[1].skHex, 'hello world'), _lib.transactions.makeRevoke('foo.test', testAddresses[0].skHex, testAddresses[1].skHex), _lib.transactions.makeAnnounce('53bb740c47435a51b07ecf0b9e086a2ad3c12c1d', testAddresses[3].skHex), _lib.transactions.makeTokenTransfer(testAddresses[1].address, 'STACKS', _bigi2.default.fromByteArrayUnsigned('123'), 'hello world!', testAddresses[4].skHex)]); }).then(function (txs) { for (var i = 0; i < txs.length; i++) { var tx = _bitcoinjsLib2.default.Transaction.fromHex(txs[i]); var nullOut = tx.outs[0].script; t.equal(_network.network.defaults.MAINNET_