Source: script/common.js

  1. /*!
  2. * common.js - common script functions for bcoin
  3. * Copyright (c) 2014-2015, Fedor Indutny (MIT License)
  4. * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
  5. * https://github.com/bcoin-org/bcoin
  6. */
  7. 'use strict';
  8. /**
  9. * @module script/common
  10. */
  11. const assert = require('bsert');
  12. const secp256k1 = require('bcrypto/lib/secp256k1');
  13. const ScriptNum = require('./scriptnum');
  14. /**
  15. * Script opcodes.
  16. * @enum {Number}
  17. * @default
  18. */
  19. exports.opcodes = {
  20. // Push
  21. OP_0: 0x00,
  22. OP_PUSHDATA1: 0x4c,
  23. OP_PUSHDATA2: 0x4d,
  24. OP_PUSHDATA4: 0x4e,
  25. OP_1NEGATE: 0x4f,
  26. OP_RESERVED: 0x50,
  27. OP_1: 0x51,
  28. OP_2: 0x52,
  29. OP_3: 0x53,
  30. OP_4: 0x54,
  31. OP_5: 0x55,
  32. OP_6: 0x56,
  33. OP_7: 0x57,
  34. OP_8: 0x58,
  35. OP_9: 0x59,
  36. OP_10: 0x5a,
  37. OP_11: 0x5b,
  38. OP_12: 0x5c,
  39. OP_13: 0x5d,
  40. OP_14: 0x5e,
  41. OP_15: 0x5f,
  42. OP_16: 0x60,
  43. // Control
  44. OP_NOP: 0x61,
  45. OP_VER: 0x62,
  46. OP_IF: 0x63,
  47. OP_NOTIF: 0x64,
  48. OP_VERIF: 0x65,
  49. OP_VERNOTIF: 0x66,
  50. OP_ELSE: 0x67,
  51. OP_ENDIF: 0x68,
  52. OP_VERIFY: 0x69,
  53. OP_RETURN: 0x6a,
  54. // Stack
  55. OP_TOALTSTACK: 0x6b,
  56. OP_FROMALTSTACK: 0x6c,
  57. OP_2DROP: 0x6d,
  58. OP_2DUP: 0x6e,
  59. OP_3DUP: 0x6f,
  60. OP_2OVER: 0x70,
  61. OP_2ROT: 0x71,
  62. OP_2SWAP: 0x72,
  63. OP_IFDUP: 0x73,
  64. OP_DEPTH: 0x74,
  65. OP_DROP: 0x75,
  66. OP_DUP: 0x76,
  67. OP_NIP: 0x77,
  68. OP_OVER: 0x78,
  69. OP_PICK: 0x79,
  70. OP_ROLL: 0x7a,
  71. OP_ROT: 0x7b,
  72. OP_SWAP: 0x7c,
  73. OP_TUCK: 0x7d,
  74. // Splice
  75. OP_CAT: 0x7e,
  76. OP_SUBSTR: 0x7f,
  77. OP_LEFT: 0x80,
  78. OP_RIGHT: 0x81,
  79. OP_SIZE: 0x82,
  80. // Bit
  81. OP_INVERT: 0x83,
  82. OP_AND: 0x84,
  83. OP_OR: 0x85,
  84. OP_XOR: 0x86,
  85. OP_EQUAL: 0x87,
  86. OP_EQUALVERIFY: 0x88,
  87. OP_RESERVED1: 0x89,
  88. OP_RESERVED2: 0x8a,
  89. // Numeric
  90. OP_1ADD: 0x8b,
  91. OP_1SUB: 0x8c,
  92. OP_2MUL: 0x8d,
  93. OP_2DIV: 0x8e,
  94. OP_NEGATE: 0x8f,
  95. OP_ABS: 0x90,
  96. OP_NOT: 0x91,
  97. OP_0NOTEQUAL: 0x92,
  98. OP_ADD: 0x93,
  99. OP_SUB: 0x94,
  100. OP_MUL: 0x95,
  101. OP_DIV: 0x96,
  102. OP_MOD: 0x97,
  103. OP_LSHIFT: 0x98,
  104. OP_RSHIFT: 0x99,
  105. OP_BOOLAND: 0x9a,
  106. OP_BOOLOR: 0x9b,
  107. OP_NUMEQUAL: 0x9c,
  108. OP_NUMEQUALVERIFY: 0x9d,
  109. OP_NUMNOTEQUAL: 0x9e,
  110. OP_LESSTHAN: 0x9f,
  111. OP_GREATERTHAN: 0xa0,
  112. OP_LESSTHANOREQUAL: 0xa1,
  113. OP_GREATERTHANOREQUAL: 0xa2,
  114. OP_MIN: 0xa3,
  115. OP_MAX: 0xa4,
  116. OP_WITHIN: 0xa5,
  117. // Crypto
  118. OP_RIPEMD160: 0xa6,
  119. OP_SHA1: 0xa7,
  120. OP_SHA256: 0xa8,
  121. OP_HASH160: 0xa9,
  122. OP_HASH256: 0xaa,
  123. OP_CODESEPARATOR: 0xab,
  124. OP_CHECKSIG: 0xac,
  125. OP_CHECKSIGVERIFY: 0xad,
  126. OP_CHECKMULTISIG: 0xae,
  127. OP_CHECKMULTISIGVERIFY: 0xaf,
  128. // Expansion
  129. OP_NOP1: 0xb0,
  130. OP_CHECKLOCKTIMEVERIFY: 0xb1,
  131. OP_CHECKSEQUENCEVERIFY: 0xb2,
  132. OP_NOP4: 0xb3,
  133. OP_NOP5: 0xb4,
  134. OP_NOP6: 0xb5,
  135. OP_NOP7: 0xb6,
  136. OP_NOP8: 0xb7,
  137. OP_NOP9: 0xb8,
  138. OP_NOP10: 0xb9,
  139. // Custom
  140. OP_INVALIDOPCODE: 0xff
  141. };
  142. /**
  143. * Opcodes by value.
  144. * @const {Object}
  145. */
  146. exports.opcodesByVal = {
  147. // Push
  148. 0x00: 'OP_0',
  149. 0x4c: 'OP_PUSHDATA1',
  150. 0x4d: 'OP_PUSHDATA2',
  151. 0x4e: 'OP_PUSHDATA4',
  152. 0x4f: 'OP_1NEGATE',
  153. 0x50: 'OP_RESERVED',
  154. 0x51: 'OP_1',
  155. 0x52: 'OP_2',
  156. 0x53: 'OP_3',
  157. 0x54: 'OP_4',
  158. 0x55: 'OP_5',
  159. 0x56: 'OP_6',
  160. 0x57: 'OP_7',
  161. 0x58: 'OP_8',
  162. 0x59: 'OP_9',
  163. 0x5a: 'OP_10',
  164. 0x5b: 'OP_11',
  165. 0x5c: 'OP_12',
  166. 0x5d: 'OP_13',
  167. 0x5e: 'OP_14',
  168. 0x5f: 'OP_15',
  169. 0x60: 'OP_16',
  170. // Control
  171. 0x61: 'OP_NOP',
  172. 0x62: 'OP_VER',
  173. 0x63: 'OP_IF',
  174. 0x64: 'OP_NOTIF',
  175. 0x65: 'OP_VERIF',
  176. 0x66: 'OP_VERNOTIF',
  177. 0x67: 'OP_ELSE',
  178. 0x68: 'OP_ENDIF',
  179. 0x69: 'OP_VERIFY',
  180. 0x6a: 'OP_RETURN',
  181. // Stack
  182. 0x6b: 'OP_TOALTSTACK',
  183. 0x6c: 'OP_FROMALTSTACK',
  184. 0x6d: 'OP_2DROP',
  185. 0x6e: 'OP_2DUP',
  186. 0x6f: 'OP_3DUP',
  187. 0x70: 'OP_2OVER',
  188. 0x71: 'OP_2ROT',
  189. 0x72: 'OP_2SWAP',
  190. 0x73: 'OP_IFDUP',
  191. 0x74: 'OP_DEPTH',
  192. 0x75: 'OP_DROP',
  193. 0x76: 'OP_DUP',
  194. 0x77: 'OP_NIP',
  195. 0x78: 'OP_OVER',
  196. 0x79: 'OP_PICK',
  197. 0x7a: 'OP_ROLL',
  198. 0x7b: 'OP_ROT',
  199. 0x7c: 'OP_SWAP',
  200. 0x7d: 'OP_TUCK',
  201. // Splice
  202. 0x7e: 'OP_CAT',
  203. 0x7f: 'OP_SUBSTR',
  204. 0x80: 'OP_LEFT',
  205. 0x81: 'OP_RIGHT',
  206. 0x82: 'OP_SIZE',
  207. // Bit
  208. 0x83: 'OP_INVERT',
  209. 0x84: 'OP_AND',
  210. 0x85: 'OP_OR',
  211. 0x86: 'OP_XOR',
  212. 0x87: 'OP_EQUAL',
  213. 0x88: 'OP_EQUALVERIFY',
  214. 0x89: 'OP_RESERVED1',
  215. 0x8a: 'OP_RESERVED2',
  216. // Numeric
  217. 0x8b: 'OP_1ADD',
  218. 0x8c: 'OP_1SUB',
  219. 0x8d: 'OP_2MUL',
  220. 0x8e: 'OP_2DIV',
  221. 0x8f: 'OP_NEGATE',
  222. 0x90: 'OP_ABS',
  223. 0x91: 'OP_NOT',
  224. 0x92: 'OP_0NOTEQUAL',
  225. 0x93: 'OP_ADD',
  226. 0x94: 'OP_SUB',
  227. 0x95: 'OP_MUL',
  228. 0x96: 'OP_DIV',
  229. 0x97: 'OP_MOD',
  230. 0x98: 'OP_LSHIFT',
  231. 0x99: 'OP_RSHIFT',
  232. 0x9a: 'OP_BOOLAND',
  233. 0x9b: 'OP_BOOLOR',
  234. 0x9c: 'OP_NUMEQUAL',
  235. 0x9d: 'OP_NUMEQUALVERIFY',
  236. 0x9e: 'OP_NUMNOTEQUAL',
  237. 0x9f: 'OP_LESSTHAN',
  238. 0xa0: 'OP_GREATERTHAN',
  239. 0xa1: 'OP_LESSTHANOREQUAL',
  240. 0xa2: 'OP_GREATERTHANOREQUAL',
  241. 0xa3: 'OP_MIN',
  242. 0xa4: 'OP_MAX',
  243. 0xa5: 'OP_WITHIN',
  244. // Crypto
  245. 0xa6: 'OP_RIPEMD160',
  246. 0xa7: 'OP_SHA1',
  247. 0xa8: 'OP_SHA256',
  248. 0xa9: 'OP_HASH160',
  249. 0xaa: 'OP_HASH256',
  250. 0xab: 'OP_CODESEPARATOR',
  251. 0xac: 'OP_CHECKSIG',
  252. 0xad: 'OP_CHECKSIGVERIFY',
  253. 0xae: 'OP_CHECKMULTISIG',
  254. 0xaf: 'OP_CHECKMULTISIGVERIFY',
  255. // Expansion
  256. 0xb0: 'OP_NOP1',
  257. 0xb1: 'OP_CHECKLOCKTIMEVERIFY',
  258. 0xb2: 'OP_CHECKSEQUENCEVERIFY',
  259. 0xb3: 'OP_NOP4',
  260. 0xb4: 'OP_NOP5',
  261. 0xb5: 'OP_NOP6',
  262. 0xb6: 'OP_NOP7',
  263. 0xb7: 'OP_NOP8',
  264. 0xb8: 'OP_NOP9',
  265. 0xb9: 'OP_NOP10',
  266. // Custom
  267. 0xff: 'OP_INVALIDOPCODE'
  268. };
  269. /**
  270. * Small ints (1 indexed, 1==0).
  271. * @const {Buffer[]}
  272. */
  273. exports.small = [
  274. Buffer.from([0x81]),
  275. Buffer.from([]),
  276. Buffer.from([0x01]),
  277. Buffer.from([0x02]),
  278. Buffer.from([0x03]),
  279. Buffer.from([0x04]),
  280. Buffer.from([0x05]),
  281. Buffer.from([0x06]),
  282. Buffer.from([0x07]),
  283. Buffer.from([0x08]),
  284. Buffer.from([0x09]),
  285. Buffer.from([0x0a]),
  286. Buffer.from([0x0b]),
  287. Buffer.from([0x0c]),
  288. Buffer.from([0x0d]),
  289. Buffer.from([0x0e]),
  290. Buffer.from([0x0f]),
  291. Buffer.from([0x10])
  292. ];
  293. /**
  294. * Script and locktime flags. See {@link VerifyFlags}.
  295. * @enum {Number}
  296. */
  297. exports.flags = {
  298. VERIFY_NONE: 0,
  299. VERIFY_P2SH: 1 << 0,
  300. VERIFY_STRICTENC: 1 << 1,
  301. VERIFY_DERSIG: 1 << 2,
  302. VERIFY_LOW_S: 1 << 3,
  303. VERIFY_NULLDUMMY: 1 << 4,
  304. VERIFY_SIGPUSHONLY: 1 << 5,
  305. VERIFY_MINIMALDATA: 1 << 6,
  306. VERIFY_DISCOURAGE_UPGRADABLE_NOPS: 1 << 7,
  307. VERIFY_CLEANSTACK: 1 << 8,
  308. VERIFY_CHECKLOCKTIMEVERIFY: 1 << 9,
  309. VERIFY_CHECKSEQUENCEVERIFY: 1 << 10,
  310. VERIFY_WITNESS: 1 << 11,
  311. VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM: 1 << 12,
  312. VERIFY_MINIMALIF: 1 << 13,
  313. VERIFY_NULLFAIL: 1 << 14,
  314. VERIFY_WITNESS_PUBKEYTYPE: 1 << 15,
  315. VERIFY_CONST_SCRIPTCODE: 1 << 16
  316. };
  317. /**
  318. * Consensus verify flags (used for block validation).
  319. * @const {VerifyFlags}
  320. * @default
  321. */
  322. exports.flags.MANDATORY_VERIFY_FLAGS = exports.flags.VERIFY_P2SH;
  323. /**
  324. * Standard verify flags (used for mempool validation).
  325. * @const {VerifyFlags}
  326. * @default
  327. */
  328. exports.flags.STANDARD_VERIFY_FLAGS = 0
  329. | exports.flags.MANDATORY_VERIFY_FLAGS
  330. | exports.flags.VERIFY_DERSIG
  331. | exports.flags.VERIFY_STRICTENC
  332. | exports.flags.VERIFY_MINIMALDATA
  333. | exports.flags.VERIFY_NULLDUMMY
  334. | exports.flags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS
  335. | exports.flags.VERIFY_CLEANSTACK
  336. | exports.flags.VERIFY_MINIMALIF
  337. | exports.flags.VERIFY_NULLFAIL
  338. | exports.flags.VERIFY_CHECKLOCKTIMEVERIFY
  339. | exports.flags.VERIFY_CHECKSEQUENCEVERIFY
  340. | exports.flags.VERIFY_LOW_S
  341. | exports.flags.VERIFY_WITNESS
  342. | exports.flags.VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM
  343. | exports.flags.VERIFY_WITNESS_PUBKEYTYPE
  344. | exports.flags.VERIFY_CONST_SCRIPTCODE;
  345. /**
  346. * Standard flags without mandatory bits.
  347. * @const {VerifyFlags}
  348. * @default
  349. */
  350. exports.flags.ONLY_STANDARD_VERIFY_FLAGS =
  351. exports.flags.STANDARD_VERIFY_FLAGS & ~exports.flags.MANDATORY_VERIFY_FLAGS;
  352. /**
  353. * Sighash Types.
  354. * @enum {SighashType}
  355. * @default
  356. */
  357. exports.hashType = {
  358. /*
  359. * Sign all outputs.
  360. */
  361. ALL: 1,
  362. /*
  363. * Do not sign outputs (zero sequences).
  364. */
  365. NONE: 2,
  366. /*
  367. * Sign output at the same index (zero sequences).
  368. */
  369. SINGLE: 3,
  370. /*
  371. * Sign only the current input (mask).
  372. */
  373. ANYONECANPAY: 0x80
  374. };
  375. /**
  376. * Sighash types by value.
  377. * @const {Object}
  378. */
  379. exports.hashTypeByVal = {
  380. 1: 'ALL',
  381. 2: 'NONE',
  382. 3: 'SINGLE',
  383. 0x80: 'ANYONECANPAY'
  384. };
  385. /**
  386. * Output script types.
  387. * @enum {Number}
  388. */
  389. exports.types = {
  390. NONSTANDARD: 0,
  391. PUBKEY: 1,
  392. PUBKEYHASH: 2,
  393. SCRIPTHASH: 3,
  394. MULTISIG: 4,
  395. NULLDATA: 5,
  396. WITNESSMALFORMED: 0x80,
  397. WITNESSSCRIPTHASH: 0x81,
  398. WITNESSPUBKEYHASH: 0x82
  399. };
  400. /**
  401. * Output script types by value.
  402. * @const {Object}
  403. */
  404. exports.typesByVal = {
  405. 0: 'NONSTANDARD',
  406. 1: 'PUBKEY',
  407. 2: 'PUBKEYHASH',
  408. 3: 'SCRIPTHASH',
  409. 4: 'MULTISIG',
  410. 5: 'NULLDATA',
  411. 0x80: 'WITNESSMALFORMED',
  412. 0x81: 'WITNESSSCRIPTHASH',
  413. 0x82: 'WITNESSPUBKEYHASH'
  414. };
  415. /**
  416. * Test a signature to see whether it contains a valid sighash type.
  417. * @param {Buffer} sig
  418. * @returns {Boolean}
  419. */
  420. exports.isHashType = function isHashType(sig) {
  421. assert(Buffer.isBuffer(sig));
  422. if (sig.length === 0)
  423. return false;
  424. const type = sig[sig.length - 1] & ~exports.hashType.ANYONECANPAY;
  425. if (type < exports.hashType.ALL || type > exports.hashType.SINGLE)
  426. return false;
  427. return true;
  428. };
  429. /**
  430. * Test a signature to see whether it contains a low S value.
  431. * @param {Buffer} sig
  432. * @returns {Boolean}
  433. */
  434. exports.isLowDER = function isLowDER(sig) {
  435. if (!exports.isSignatureEncoding(sig))
  436. return false;
  437. return secp256k1.isLowDER(sig.slice(0, -1));
  438. };
  439. /**
  440. * Test whether the data element is a valid key.
  441. * @param {Buffer} key
  442. * @returns {Boolean}
  443. */
  444. exports.isKeyEncoding = function isKeyEncoding(key) {
  445. assert(Buffer.isBuffer(key));
  446. if (key.length < 33)
  447. return false;
  448. if (key[0] === 0x04) {
  449. if (key.length !== 65)
  450. return false;
  451. } else if (key[0] === 0x02 || key[0] === 0x03) {
  452. if (key.length !== 33)
  453. return false;
  454. } else {
  455. return false;
  456. }
  457. return true;
  458. };
  459. /**
  460. * Test whether the data element is a compressed key.
  461. * @param {Buffer} key
  462. * @returns {Boolean}
  463. */
  464. exports.isCompressedEncoding = function isCompressedEncoding(key) {
  465. assert(Buffer.isBuffer(key));
  466. if (key.length !== 33)
  467. return false;
  468. if (key[0] !== 0x02 && key[0] !== 0x03)
  469. return false;
  470. return true;
  471. };
  472. /**
  473. * Test a signature to see if it abides by BIP66.
  474. * @see https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki
  475. * @param {Buffer} sig
  476. * @returns {Boolean}
  477. */
  478. exports.isSignatureEncoding = function isSignatureEncoding(sig) {
  479. assert(Buffer.isBuffer(sig));
  480. // Format:
  481. // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] [sighash]
  482. // * total-length: 1-byte length descriptor of everything that follows,
  483. // excluding the sighash byte.
  484. // * R-length: 1-byte length descriptor of the R value that follows.
  485. // * R: arbitrary-length big-endian encoded R value. It must use the shortest
  486. // possible encoding for a positive integers (which means no null bytes at
  487. // the start, except a single one when the next byte has its highest bit
  488. // set).
  489. // * S-length: 1-byte length descriptor of the S value that follows.
  490. // * S: arbitrary-length big-endian encoded S value. The same rules apply.
  491. // * sighash: 1-byte value indicating what data is hashed (not part of the DER
  492. // signature)
  493. // Minimum and maximum size constraints.
  494. if (sig.length < 9)
  495. return false;
  496. if (sig.length > 73)
  497. return false;
  498. // A signature is of type 0x30 (compound).
  499. if (sig[0] !== 0x30)
  500. return false;
  501. // Make sure the length covers the entire signature.
  502. if (sig[1] !== sig.length - 3)
  503. return false;
  504. // Extract the length of the R element.
  505. const lenR = sig[3];
  506. // Make sure the length of the S element is still inside the signature.
  507. if (5 + lenR >= sig.length)
  508. return false;
  509. // Extract the length of the S element.
  510. const lenS = sig[5 + lenR];
  511. // Verify that the length of the signature matches the sum of the length
  512. // of the elements.
  513. if (lenR + lenS + 7 !== sig.length)
  514. return false;
  515. // Check whether the R element is an integer.
  516. if (sig[2] !== 0x02)
  517. return false;
  518. // Zero-length integers are not allowed for R.
  519. if (lenR === 0)
  520. return false;
  521. // Negative numbers are not allowed for R.
  522. if (sig[4] & 0x80)
  523. return false;
  524. // Null bytes at the start of R are not allowed, unless R would
  525. // otherwise be interpreted as a negative number.
  526. if (lenR > 1 && (sig[4] === 0x00) && !(sig[5] & 0x80))
  527. return false;
  528. // Check whether the S element is an integer.
  529. if (sig[lenR + 4] !== 0x02)
  530. return false;
  531. // Zero-length integers are not allowed for S.
  532. if (lenS === 0)
  533. return false;
  534. // Negative numbers are not allowed for S.
  535. if (sig[lenR + 6] & 0x80)
  536. return false;
  537. // Null bytes at the start of S are not allowed, unless S would otherwise be
  538. // interpreted as a negative number.
  539. if (lenS > 1 && (sig[lenR + 6] === 0x00) && !(sig[lenR + 7] & 0x80))
  540. return false;
  541. return true;
  542. };
  543. /**
  544. * Format stack item into bitcoind asm format.
  545. * @param {Buffer} item
  546. * @param {Boolean?} decode - Attempt to decode hash types.
  547. * @returns {String} Human-readable string.
  548. */
  549. exports.toASM = function toASM(item, decode) {
  550. if (item.length <= 4) {
  551. const num = ScriptNum.decode(item);
  552. return num.toString(10);
  553. }
  554. if (decode && exports.isSignatureEncoding(item)) {
  555. const type = item[item.length - 1];
  556. let symbol = exports.hashTypeByVal[type & 0x1f] || '';
  557. if (symbol) {
  558. if (type & exports.hashType.ANYONECANPAY)
  559. symbol += '|ANYONECANPAY';
  560. symbol = `[${symbol}]`;
  561. }
  562. return item.slice(0, -1).toString('hex') + symbol;
  563. }
  564. return item.toString('hex');
  565. };