address/crypto/script/types: adhere to no-use-before-define
This commit is contained in:
parent
d3ccbb6277
commit
a1d3e33c84
4 changed files with 51 additions and 51 deletions
|
@ -15,15 +15,6 @@ function fromBase58Check (address) {
|
||||||
return { hash: hash, version: version }
|
return { hash: hash, version: version }
|
||||||
}
|
}
|
||||||
|
|
||||||
function fromOutputScript (scriptPubKey, network) {
|
|
||||||
network = network || networks.bitcoin
|
|
||||||
|
|
||||||
if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(3, 23), network.pubKeyHash)
|
|
||||||
if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(2, 22), network.scriptHash)
|
|
||||||
|
|
||||||
throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address')
|
|
||||||
}
|
|
||||||
|
|
||||||
function toBase58Check (hash, version) {
|
function toBase58Check (hash, version) {
|
||||||
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
||||||
|
|
||||||
|
@ -34,6 +25,15 @@ function toBase58Check (hash, version) {
|
||||||
return bs58check.encode(payload)
|
return bs58check.encode(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fromOutputScript (scriptPubKey, network) {
|
||||||
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
|
if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(3, 23), network.pubKeyHash)
|
||||||
|
if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(2, 22), network.scriptHash)
|
||||||
|
|
||||||
|
throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address')
|
||||||
|
}
|
||||||
|
|
||||||
function toOutputScript (address, network) {
|
function toOutputScript (address, network) {
|
||||||
network = network || networks.bitcoin
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,5 @@
|
||||||
var createHash = require('create-hash')
|
var createHash = require('create-hash')
|
||||||
|
|
||||||
function hash160 (buffer) {
|
|
||||||
return ripemd160(sha256(buffer))
|
|
||||||
}
|
|
||||||
|
|
||||||
function hash256 (buffer) {
|
|
||||||
return sha256(sha256(buffer))
|
|
||||||
}
|
|
||||||
|
|
||||||
function ripemd160 (buffer) {
|
function ripemd160 (buffer) {
|
||||||
return createHash('rmd160').update(buffer).digest()
|
return createHash('rmd160').update(buffer).digest()
|
||||||
}
|
}
|
||||||
|
@ -20,6 +12,14 @@ function sha256 (buffer) {
|
||||||
return createHash('sha256').update(buffer).digest()
|
return createHash('sha256').update(buffer).digest()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hash160 (buffer) {
|
||||||
|
return ripemd160(sha256(buffer))
|
||||||
|
}
|
||||||
|
|
||||||
|
function hash256 (buffer) {
|
||||||
|
return sha256(sha256(buffer))
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
hash160: hash160,
|
hash160: hash160,
|
||||||
hash256: hash256,
|
hash256: hash256,
|
||||||
|
|
|
@ -15,32 +15,6 @@ var REVERSE_OPS = (function () {
|
||||||
|
|
||||||
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
||||||
|
|
||||||
function toASM (chunks) {
|
|
||||||
if (Buffer.isBuffer(chunks)) {
|
|
||||||
chunks = decompile(chunks)
|
|
||||||
}
|
|
||||||
|
|
||||||
return chunks.map(function (chunk) {
|
|
||||||
// data?
|
|
||||||
if (Buffer.isBuffer(chunk)) return chunk.toString('hex')
|
|
||||||
|
|
||||||
// opcode!
|
|
||||||
return REVERSE_OPS[chunk]
|
|
||||||
}).join(' ')
|
|
||||||
}
|
|
||||||
|
|
||||||
function fromASM (asm) {
|
|
||||||
typeforce(types.String, asm)
|
|
||||||
|
|
||||||
return compile(asm.split(' ').map(function (chunkStr) {
|
|
||||||
// opcode?
|
|
||||||
if (OPS[chunkStr] !== undefined) return OPS[chunkStr]
|
|
||||||
|
|
||||||
// data!
|
|
||||||
return new Buffer(chunkStr, 'hex')
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
function compile (chunks) {
|
function compile (chunks) {
|
||||||
// TODO: remove me
|
// TODO: remove me
|
||||||
if (Buffer.isBuffer(chunks)) return chunks
|
if (Buffer.isBuffer(chunks)) return chunks
|
||||||
|
@ -118,6 +92,32 @@ function decompile (buffer) {
|
||||||
return chunks
|
return chunks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toASM (chunks) {
|
||||||
|
if (Buffer.isBuffer(chunks)) {
|
||||||
|
chunks = decompile(chunks)
|
||||||
|
}
|
||||||
|
|
||||||
|
return chunks.map(function (chunk) {
|
||||||
|
// data?
|
||||||
|
if (Buffer.isBuffer(chunk)) return chunk.toString('hex')
|
||||||
|
|
||||||
|
// opcode!
|
||||||
|
return REVERSE_OPS[chunk]
|
||||||
|
}).join(' ')
|
||||||
|
}
|
||||||
|
|
||||||
|
function fromASM (asm) {
|
||||||
|
typeforce(types.String, asm)
|
||||||
|
|
||||||
|
return compile(asm.split(' ').map(function (chunkStr) {
|
||||||
|
// opcode?
|
||||||
|
if (OPS[chunkStr] !== undefined) return OPS[chunkStr]
|
||||||
|
|
||||||
|
// data!
|
||||||
|
return new Buffer(chunkStr, 'hex')
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
function isCanonicalPubKey (buffer) {
|
function isCanonicalPubKey (buffer) {
|
||||||
if (!Buffer.isBuffer(buffer)) return false
|
if (!Buffer.isBuffer(buffer)) return false
|
||||||
if (buffer.length < 33) return false
|
if (buffer.length < 33) return false
|
||||||
|
@ -133,13 +133,6 @@ function isCanonicalPubKey (buffer) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
function isCanonicalSignature (buffer) {
|
|
||||||
if (!Buffer.isBuffer(buffer)) return false
|
|
||||||
if (!isDefinedHashType(buffer[buffer.length - 1])) return false
|
|
||||||
|
|
||||||
return bip66.check(buffer.slice(0, -1))
|
|
||||||
}
|
|
||||||
|
|
||||||
function isDefinedHashType (hashType) {
|
function isDefinedHashType (hashType) {
|
||||||
var hashTypeMod = hashType & ~0x80
|
var hashTypeMod = hashType & ~0x80
|
||||||
|
|
||||||
|
@ -147,6 +140,13 @@ function isDefinedHashType (hashType) {
|
||||||
return hashTypeMod > 0x00 && hashTypeMod < 0x04
|
return hashTypeMod > 0x00 && hashTypeMod < 0x04
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isCanonicalSignature (buffer) {
|
||||||
|
if (!Buffer.isBuffer(buffer)) return false
|
||||||
|
if (!isDefinedHashType(buffer[buffer.length - 1])) return false
|
||||||
|
|
||||||
|
return bip66.check(buffer.slice(0, -1))
|
||||||
|
}
|
||||||
|
|
||||||
function isPubKeyHashInput (script) {
|
function isPubKeyHashInput (script) {
|
||||||
var chunks = decompile(script)
|
var chunks = decompile(script)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
var typeforce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
|
|
||||||
function nBuffer (value, n) {
|
function nBuffer (value, n) {
|
||||||
typeforce(types.Buffer, value)
|
typeforce(typeforce.Buffer, value)
|
||||||
if (value.length !== n) throw new typeforce.TfTypeError('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit Buffer')
|
if (value.length !== n) throw new typeforce.TfTypeError('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit Buffer')
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue