@alexbosworth/blockchain 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +16 -0
- package/index.js +2 -0
- package/package.json +2 -2
- package/script/index.js +3 -0
- package/script/script_elements_as_script.js +42 -0
- package/test/script/test_script_elements_as_script.js +30 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -104,3 +104,19 @@ Convert a number to compact size integer serialization
|
|
|
104
104
|
{
|
|
105
105
|
encoded: <Serialized Compact Integer Buffer Object>
|
|
106
106
|
}
|
|
107
|
+
|
|
108
|
+
### scriptElementsAsScript
|
|
109
|
+
|
|
110
|
+
Map array of script buffer elements to a fully formed script
|
|
111
|
+
|
|
112
|
+
{
|
|
113
|
+
elements: [<Data Buffer>, <Script OP_CODE Number>]
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@throws
|
|
117
|
+
<Error>
|
|
118
|
+
|
|
119
|
+
@returns
|
|
120
|
+
{
|
|
121
|
+
script: <Script Hex String>
|
|
122
|
+
}
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const {componentsOfTransaction} = require('./transactions');
|
|
|
3
3
|
const {idForBlock} = require('./hashes');
|
|
4
4
|
const {noLocktimeIdForTransaction} = require('./hashes');
|
|
5
5
|
const {numberAsCompactInt} = require('./numbers');
|
|
6
|
+
const {scriptElementsAsScript} = require('./script');
|
|
6
7
|
|
|
7
8
|
module.exports = {
|
|
8
9
|
compactIntAsNumber,
|
|
@@ -10,4 +11,5 @@ module.exports = {
|
|
|
10
11
|
idForBlock,
|
|
11
12
|
noLocktimeIdForTransaction,
|
|
12
13
|
numberAsCompactInt,
|
|
14
|
+
scriptElementsAsScript,
|
|
13
15
|
};
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"url": "https://github.com/alexbosworth/blockchain.git"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
-
"test": "tap -j 2 --branches=1 --functions=1 --lines=1 --statements=1 -t 200 test/hashes/*.js test/numbers/*.js test/transactions/*.js"
|
|
23
|
+
"test": "tap -j 2 --branches=1 --functions=1 --lines=1 --statements=1 -t 200 test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
|
|
24
24
|
},
|
|
25
|
-
"version": "1.
|
|
25
|
+
"version": "1.4.0"
|
|
26
26
|
}
|
package/script/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const {numberAsCompactInt} = require('./../numbers');
|
|
2
|
+
|
|
3
|
+
const bufferAsHex = buffer => buffer.toString('hex');
|
|
4
|
+
const {concat} = Buffer;
|
|
5
|
+
const encode = number => numberAsCompactInt({number}).encoded;
|
|
6
|
+
const flatten = arr => arr.reduce((sum, n) => Buffer.concat([sum, n]));
|
|
7
|
+
const {from} = Buffer;
|
|
8
|
+
const {isArray} = Array;
|
|
9
|
+
const {isBuffer} = Buffer;
|
|
10
|
+
|
|
11
|
+
/** Map array of script buffer elements to a fully formed script
|
|
12
|
+
|
|
13
|
+
{
|
|
14
|
+
elements: [<Data Buffer>, <Script OP_CODE Number>]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@throws
|
|
18
|
+
<Error>
|
|
19
|
+
|
|
20
|
+
@returns
|
|
21
|
+
{
|
|
22
|
+
script: <Script Hex String>
|
|
23
|
+
}
|
|
24
|
+
*/
|
|
25
|
+
module.exports = ({elements}) => {
|
|
26
|
+
if (!isArray(elements)) {
|
|
27
|
+
throw new Error('ExpectedArrayOfScriptElementsToEncodeScript');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Convert numbers to buffers and hex data to pushdata
|
|
31
|
+
const fullScript = elements.map(element => {
|
|
32
|
+
// Exit early when element is a data push
|
|
33
|
+
if (isBuffer(element)) {
|
|
34
|
+
return concat([encode(element.length), element]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Non data elements are direct bytes
|
|
38
|
+
return from([element]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return {script: bufferAsHex(flatten(fullScript))};
|
|
42
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const {test} = require('@alexbosworth/tap');
|
|
2
|
+
|
|
3
|
+
const {scriptElementsAsScript} = require('./../../');
|
|
4
|
+
|
|
5
|
+
const tests = [
|
|
6
|
+
{
|
|
7
|
+
args: {},
|
|
8
|
+
description: 'An array of eleemnts is required',
|
|
9
|
+
error: 'ExpectedArrayOfScriptElementsToEncodeScript',
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
args: {elements: [118, 169, Buffer.alloc(20), 136, 172]},
|
|
13
|
+
description: 'Elements are mapped to an output script',
|
|
14
|
+
expected: {script: '76a914000000000000000000000000000000000000000088ac'},
|
|
15
|
+
},
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
19
|
+
return test(description, ({end, strictSame, throws}) => {
|
|
20
|
+
if (!!error) {
|
|
21
|
+
throws(() => scriptElementsAsScript(args), new Error(error), 'Got err');
|
|
22
|
+
} else {
|
|
23
|
+
const res = scriptElementsAsScript(args);
|
|
24
|
+
|
|
25
|
+
strictSame(res, expected, 'Got expected result');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return end();
|
|
29
|
+
});
|
|
30
|
+
});
|