Browse Source

Refactor outputs to array of objects instead of array of arrays

master
Luke Childs 6 years ago
parent
commit
5ecdf88b4e
  1. 10
      README.md
  2. 2
      src/index.js
  3. 14
      test/unit.js

10
README.md

@ -21,7 +21,7 @@ npm install build-output-script
Send 1 BTC to `1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj` Send 1 BTC to `1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj`
```js ```js
buildOutputScript([['1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', 100000000]]); buildOutputScript([{ address: '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', value: 100000000 }]);
// '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac' // '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac'
``` ```
@ -29,8 +29,8 @@ Send 1 BTC to `1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj` and 1 BTC to `1BitcoinEaterAd
```js ```js
buildOutputScript([ buildOutputScript([
['1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', 100000000], { address: '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', value: 100000000 },
['1BitcoinEaterAddressDontSendf59kuE', 100000000] { address: '1BitcoinEaterAddressDontSendf59kuE', value: 100000000 }
]); ]);
// '0200e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac00e1f505000000001976a914759d6677091e973b9e9d99f19c68fbf43e3f05f988ac' // '0200e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac00e1f505000000001976a914759d6677091e973b9e9d99f19c68fbf43e3f05f988ac'
``` ```
@ -38,13 +38,13 @@ buildOutputScript([
Send 1 LTC to `Lf8hucmupbtenQ3VPdxvvJ8gTfAsaon2gf` Send 1 LTC to `Lf8hucmupbtenQ3VPdxvvJ8gTfAsaon2gf`
```js ```js
buildOutputScript([['Lf8hucmupbtenQ3VPdxvvJ8gTfAsaon2gf', 100000000]]); buildOutputScript([{ address: 'Lf8hucmupbtenQ3VPdxvvJ8gTfAsaon2gf', value: 100000000 }]);
// '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac' // '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac'
``` ```
## API ## API
### buildOutputScript([[address, value], ...]); ### buildOutputScript(Array<[address, value]>)
Returns a (hex string) P2PKH transaction output script. Returns a (hex string) P2PKH transaction output script.

2
src/index.js

@ -32,7 +32,7 @@ const buildOutputScript = outputs => {
let outputScript = [...numberToCompactSizeUInt(outputs.length)]; let outputScript = [...numberToCompactSizeUInt(outputs.length)];
for (const [address, value] of outputs) { for (const { address, value } of outputs) {
const pubKeyHash = addressDecode(address); const pubKeyHash = addressDecode(address);
const scriptPubKey = [ const scriptPubKey = [

14
test/unit.js

@ -2,7 +2,7 @@ import test from 'ava';
import buildOutputScript from '..'; import buildOutputScript from '..';
test('Single address and value input returns valid output script', t => { test('Single address and value input returns valid output script', t => {
const outputScipt = buildOutputScript([['1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', 100000000]]); const outputScipt = buildOutputScript([{ address: '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', value: 100000000 }]);
const expectedOutputScript = '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac'; const expectedOutputScript = '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac';
t.is(outputScipt, expectedOutputScript); t.is(outputScipt, expectedOutputScript);
@ -10,8 +10,8 @@ test('Single address and value input returns valid output script', t => {
test('Multiple address and value inputs returns valid output script', t => { test('Multiple address and value inputs returns valid output script', t => {
const outputScipt = buildOutputScript([ const outputScipt = buildOutputScript([
['1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', 100000000], { address: '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', value: 100000000},
['1BitcoinEaterAddressDontSendf59kuE', 100000000] { address: '1BitcoinEaterAddressDontSendf59kuE', value: 100000000}
]); ]);
const expectedOutputScript = '0200e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac00e1f505000000001976a914759d6677091e973b9e9d99f19c68fbf43e3f05f988ac'; const expectedOutputScript = '0200e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac00e1f505000000001976a914759d6677091e973b9e9d99f19c68fbf43e3f05f988ac';
@ -19,8 +19,8 @@ test('Multiple address and value inputs returns valid output script', t => {
}); });
test('Altcoin P2PKH addresses with a different pubkey hash prefix returns valid ouput script', t => { test('Altcoin P2PKH addresses with a different pubkey hash prefix returns valid ouput script', t => {
const bitcoinOutputScipt = buildOutputScript([['1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', 100000000]]); const bitcoinOutputScipt = buildOutputScript([{ address: '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', value: 100000000 }]);
const litecoinOutputScipt = buildOutputScript([['Lf8hucmupbtenQ3VPdxvvJ8gTfAsaon2gf', 100000000]]); const litecoinOutputScipt = buildOutputScript([{ address: 'Lf8hucmupbtenQ3VPdxvvJ8gTfAsaon2gf', value: 100000000 }]);
const expectedOutputScript = '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac'; const expectedOutputScript = '0100e1f505000000001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac';
t.is(bitcoinOutputScipt, litecoinOutputScipt); t.is(bitcoinOutputScipt, litecoinOutputScipt);
@ -30,12 +30,12 @@ test('Altcoin P2PKH addresses with a different pubkey hash prefix returns valid
test('Unsafe integer value throws error', t => { test('Unsafe integer value throws error', t => {
const MAX_SAFE_INTEGER = 9007199254740991; const MAX_SAFE_INTEGER = 9007199254740991;
const outputScipt = buildOutputScript([['1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', MAX_SAFE_INTEGER]]); const outputScipt = buildOutputScript([{ address: '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', value: MAX_SAFE_INTEGER }]);
const expectedOutputScript = '01ffffffffffff1f001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac'; const expectedOutputScript = '01ffffffffffff1f001976a914da6473ed373e08f46dd8003fca7ba72fbe9c555e88ac';
t.is(outputScipt, expectedOutputScript); t.is(outputScipt, expectedOutputScript);
t.throws(() => buildOutputScript([['1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', (MAX_SAFE_INTEGER + 1)]])); t.throws(() => buildOutputScript([{ address: '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj', value: (MAX_SAFE_INTEGER + 1) }]));
}); });
test('Not passing an array throws error', t => { test('Not passing an array throws error', t => {

Loading…
Cancel
Save