Merge pull request #1094 from bitcoinjs/canon

script isCanonical* refactor/renames
This commit is contained in:
Daniel Cousens 2018-06-21 12:45:21 +10:00 committed by GitHub
commit e50679d2c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 25 deletions

View file

@ -1,5 +1,6 @@
var Buffer = require('safe-buffer').Buffer
var bip66 = require('bip66')
let ecc = require('tiny-secp256k1')
var pushdata = require('pushdata-bitcoin')
var typeforce = require('typeforce')
var types = require('./types')
@ -170,18 +171,7 @@ function toStack (chunks) {
}
function isCanonicalPubKey (buffer) {
if (!Buffer.isBuffer(buffer)) return false
if (buffer.length < 33) return false
switch (buffer[0]) {
case 0x02:
case 0x03:
return buffer.length === 33
case 0x04:
return buffer.length === 65
}
return false
return ecc.isPoint(buffer)
}
function isDefinedHashType (hashType) {
@ -191,7 +181,7 @@ function isDefinedHashType (hashType) {
return hashTypeMod > 0x00 && hashTypeMod < 0x04
}
function isCanonicalSignature (buffer) {
function isCanonicalScriptSignature (buffer) {
if (!Buffer.isBuffer(buffer)) return false
if (!isDefinedHashType(buffer[buffer.length - 1])) return false
@ -209,7 +199,7 @@ module.exports = {
signature: require('./script_signature'),
isCanonicalPubKey: isCanonicalPubKey,
isCanonicalSignature: isCanonicalSignature,
isCanonicalScriptSignature: isCanonicalScriptSignature,
isPushOnly: isPushOnly,
isDefinedHashType: isDefinedHashType
}

View file

@ -7,7 +7,7 @@ var typeforce = require('typeforce')
var OPS = require('bitcoin-ops')
function partialSignature (value) {
return value === OPS.OP_0 || bscript.isCanonicalSignature(value)
return value === OPS.OP_0 || bscript.isCanonicalScriptSignature(value)
}
function check (script, allowIncomplete) {
@ -19,7 +19,7 @@ function check (script, allowIncomplete) {
return chunks.slice(1).every(partialSignature)
}
return chunks.slice(1).every(bscript.isCanonicalSignature)
return chunks.slice(1).every(bscript.isCanonicalScriptSignature)
}
check.toJSON = function () { return 'multisig input' }

View file

@ -7,12 +7,12 @@ function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 1 &&
bscript.isCanonicalSignature(chunks[0])
bscript.isCanonicalScriptSignature(chunks[0])
}
check.toJSON = function () { return 'pubKey input' }
function encodeStack (signature) {
typeforce(bscript.isCanonicalSignature, signature)
typeforce(bscript.isCanonicalScriptSignature, signature)
return [signature]
}

View file

@ -7,14 +7,14 @@ function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalSignature(chunks[0]) &&
bscript.isCanonicalScriptSignature(chunks[0]) &&
bscript.isCanonicalPubKey(chunks[1])
}
check.toJSON = function () { return 'pubKeyHash input' }
function encodeStack (signature, pubKey) {
typeforce({
signature: bscript.isCanonicalSignature,
signature: bscript.isCanonicalScriptSignature,
pubKey: bscript.isCanonicalPubKey
}, {
signature: signature,

View file

@ -11,14 +11,14 @@ function check (script) {
var chunks = bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalSignature(chunks[0]) &&
bscript.isCanonicalScriptSignature(chunks[0]) &&
isCompressedCanonicalPubKey(chunks[1])
}
check.toJSON = function () { return 'witnessPubKeyHash input' }
function encodeStack (signature, pubKey) {
typeforce({
signature: bscript.isCanonicalSignature,
signature: bscript.isCanonicalScriptSignature,
pubKey: isCompressedCanonicalPubKey
}, {
signature: signature,

View file

@ -19,7 +19,8 @@ describe('script', function () {
}
})
})
describe.skip('isCanonicalSignature', function () {})
describe.skip('isCanonicalScriptSignature', function () {
})
describe('fromASM/toASM', function () {
fixtures.valid.forEach(function (f) {

View file

@ -303,7 +303,7 @@ describe('TransactionBuilder', function () {
describe('sign', function () {
it('supports the alternative abstract interface { publicKey, sign }', function () {
var keyPair = {
publicKey: Buffer.alloc(33, 0x03),
publicKey: ECPair.makeRandom({ rng: function () { return Buffer.alloc(32, 1) } }).publicKey,
sign: function (hash) { return Buffer.alloc(64, 0x5f) }
}
@ -312,7 +312,7 @@ describe('TransactionBuilder', function () {
txb.addInput('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 1)
txb.addOutput('1111111111111111111114oLvT2', 100000)
txb.sign(0, keyPair)
assert.equal(txb.build().toHex(), '0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f02205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f0121030303030303030303030303030303030303030303030303030303030303030303ffffffff01a0860100000000001976a914000000000000000000000000000000000000000088ac00000000')
assert.equal(txb.build().toHex(), '0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f02205f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f0121031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078fffffffff01a0860100000000001976a914000000000000000000000000000000000000000088ac00000000')
})
fixtures.invalid.sign.forEach(function (f) {