Compare commits

..

6 Commits

Author SHA1 Message Date
Daniel Cousens
fbaad04f24
Merge pull request #1165 from fybwid/patch-1
Update README.md
2018-08-08 13:30:17 +10:00
Yosef Benny Widyokarsono
64b9646870
Update README.md
Fix example anchor at Documentation
2018-08-08 10:53:20 +08:00
Jonathan Underwood
e672875394
Merge pull request #1160 from bitcoinjs/notealt
CHANGELOG: note alternative to templates
2018-08-07 10:54:10 +09:00
Daniel Cousens
e639914d22
CHANGELOG: note alternative to templates 2018-08-03 10:34:49 +10:00
Jonathan Underwood
836f2b4263
Merge pull request #1146 from bitcoinjs/ectests
tests/ECPair: test fromPublic/fromPrivate in isolation
2018-08-01 11:47:05 +09:00
Daniel Cousens
85b1b92b6d tests/ECPair: test fromPublic/fromPrivate in isolation 2018-07-26 18:05:39 +10:00
13 changed files with 83 additions and 98 deletions

View File

@ -32,7 +32,7 @@ __removed__
- Removed `bufferutils` (#1035)
- Removed `networks.litecoin`, BYO non-Bitcoin networks instead (#1095)
- Removed `script.isCanonicalSignature`, use `script.isCanonicalScriptSignature` instead (#1094)
- Removed `script.*.input/output/check` functions (`templates`) (previously added in #681, #682) (#1119)
- Removed `script.*.input/output/check` functions (`templates`), use `payments.*` instead (`templates` previously added in #681, #682) (#1119)
- Removed dependency `bigi`, uses `bn.js` internally now (via `tiny-secp256k1`) (#1070, #1112)
- Removed public access to `ECPair` constructor, use exported functions `ECPair.fromPrivateKey`, `ECPair.fromWIF`, `ECPair.makeRandom`, or `ECPair.fromPublicKey` (#1070)

View File

@ -27,7 +27,7 @@ Mistakes and bugs happen, but with your help in resolving and reporting [issues]
- Friendly, with a strong and helpful community, ready to answer questions.
## Documentation
Presently, we do not have any formal documentation other than our [examples](#Examples), please [ask for help](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new) if our examples aren't enough to guide you.
Presently, we do not have any formal documentation other than our [examples](#examples), please [ask for help](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new) if our examples aren't enough to guide you.
## Installation

View File

@ -5,9 +5,6 @@ const types = require('./types')
const wif = require('wif')
const NETWORKS = require('./networks')
// TODO: why is the function name toJSON weird?
function isPoint (x) { return ecc.isPoint(x) }
const isOptions = typeforce.maybe(typeforce.compile({
compressed: types.maybe(types.Boolean),
network: types.maybe(types.Network)
@ -57,7 +54,7 @@ function fromPrivateKey (buffer, options) {
}
function fromPublicKey (buffer, options) {
typeforce(isPoint, buffer)
typeforce(ecc.isPoint, buffer)
typeforce(isOptions, options)
return new ECPair(null, buffer, options)
}

View File

@ -1,10 +1,10 @@
const lazy = require('./lazy')
const typef = require('typeforce')
const OPS = require('bitcoin-ops')
const ecc = require('tiny-secp256k1')
let lazy = require('./lazy')
let typef = require('typeforce')
let OPS = require('bitcoin-ops')
let ecc = require('tiny-secp256k1')
const bscript = require('../script')
const BITCOIN_NETWORK = require('../networks').bitcoin
let bscript = require('../script')
let BITCOIN_NETWORK = require('../networks').bitcoin
// input: {signature}
// output: {pubKey} OP_CHECKSIG
@ -27,10 +27,10 @@ function p2pk (a, opts) {
input: typef.maybe(typef.Buffer)
}, a)
const _chunks = lazy.value(function () { return bscript.decompile(a.input) })
let _chunks = lazy.value(function () { return bscript.decompile(a.input) })
const network = a.network || BITCOIN_NETWORK
const o = { network }
let network = a.network || BITCOIN_NETWORK
let o = { network }
lazy.prop(o, 'output', function () {
if (!a.pubkey) return
@ -58,19 +58,22 @@ function p2pk (a, opts) {
// extended validation
if (opts.validate) {
if (a.pubkey && a.output) {
if (!a.pubkey.equals(o.pubkey)) throw new TypeError('Pubkey mismatch')
}
if (a.output) {
if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid')
if (!ecc.isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid')
if (a.pubkey && !a.pubkey.equals(o.pubkey)) throw new TypeError('Pubkey mismatch')
}
if (a.signature) {
if (a.input && !a.input.equals(o.input)) throw new TypeError('Signature mismatch')
if (a.input && !a.input.equals(o.input)) throw new TypeError('Input mismatch')
}
if (a.input) {
if (_chunks().length !== 1) throw new TypeError('Input is invalid')
if (!bscript.isCanonicalScriptSignature(o.signature)) throw new TypeError('Input has invalid signature')
if (!bscript.isCanonicalScriptSignature(_chunks()[0])) throw new TypeError('Input has invalid signature')
}
}

View File

@ -106,19 +106,18 @@ function p2pkh (a, opts) {
a.output[23] !== OPS.OP_EQUALVERIFY ||
a.output[24] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid')
const hash2 = a.output.slice(3, 23)
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
else hash = hash2
if (hash && !hash.equals(a.output.slice(3, 23))) throw new TypeError('Hash mismatch')
else hash = a.output.slice(3, 23)
}
if (a.pubkey) {
const pkh = bcrypto.hash160(a.pubkey)
let pkh = bcrypto.hash160(a.pubkey)
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
else hash = pkh
}
if (a.input) {
const chunks = _chunks()
let chunks = _chunks()
if (chunks.length !== 2) throw new TypeError('Input is invalid')
if (!bscript.isCanonicalScriptSignature(chunks[0])) throw new TypeError('Input has invalid signature')
if (!ecc.isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey')
@ -126,7 +125,7 @@ function p2pkh (a, opts) {
if (a.signature && !a.signature.equals(chunks[0])) throw new TypeError('Signature mismatch')
if (a.pubkey && !a.pubkey.equals(chunks[1])) throw new TypeError('Pubkey mismatch')
const pkh = bcrypto.hash160(chunks[1])
let pkh = bcrypto.hash160(chunks[1])
if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch')
}
}

View File

@ -58,7 +58,7 @@ function p2sh (a, opts) {
const _redeem = lazy.value(function () {
const chunks = _chunks()
return {
network,
network: network,
output: chunks[chunks.length - 1],
input: bscript.compile(chunks.slice(0, -1)),
witness: a.witness || []
@ -111,7 +111,7 @@ function p2sh (a, opts) {
if (a.address) {
if (_address().version !== network.scriptHash) throw new TypeError('Invalid version or Network mismatch')
if (_address().hash.length !== 20) throw new TypeError('Invalid address')
hash = _address().hash
else hash = _address().hash
}
if (a.hash) {
@ -125,7 +125,6 @@ function p2sh (a, opts) {
a.output[0] !== OPS.OP_HASH160 ||
a.output[1] !== 0x14 ||
a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid')
const hash2 = a.output.slice(2, 22)
if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch')
else hash = hash2
@ -166,10 +165,9 @@ function p2sh (a, opts) {
if (a.redeem) {
if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch')
if (a.input) {
const redeem = _redeem()
if (a.redeem.output && !a.redeem.output.equals(redeem.output)) throw new TypeError('Redeem.output mismatch')
if (a.redeem.input && !a.redeem.input.equals(redeem.input)) throw new TypeError('Redeem.input mismatch')
if (o.redeem) {
if (a.redeem.output && !a.redeem.output.equals(o.redeem.output)) throw new TypeError('Redeem.output mismatch')
if (a.redeem.input && !a.redeem.input.equals(o.redeem.input)) throw new TypeError('Redeem.input mismatch')
}
checkRedeem(a.redeem)

View File

@ -93,6 +93,7 @@ function p2wpkh (a, opts) {
if (network && network.bech32 !== _address().prefix) throw new TypeError('Invalid prefix or Network mismatch')
if (_address().version !== 0x00) throw new TypeError('Invalid address version')
if (_address().data.length !== 20) throw new TypeError('Invalid address data')
// if (hash && !hash.equals(_address().data)) throw new TypeError('Hash mismatch')
hash = _address().data
}

View File

@ -122,7 +122,7 @@ function p2wsh (a, opts) {
if (_address().prefix !== network.bech32) throw new TypeError('Invalid prefix or Network mismatch')
if (_address().version !== 0x00) throw new TypeError('Invalid address version')
if (_address().data.length !== 32) throw new TypeError('Invalid address data')
hash = _address().data
else hash = _address().data
}
if (a.hash) {

View File

@ -22,7 +22,23 @@ const GROUP_ORDER = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03
const GROUP_ORDER_LESS_1 = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140', 'hex')
describe('ECPair', function () {
describe('constructor', function () {
describe('getPublicKey', function () {
let keyPair
beforeEach(function () {
keyPair = ECPair.fromPrivateKey(ONE)
})
it('calls pointFromScalar lazily', hoodwink(function () {
assert.strictEqual(keyPair.__Q, null)
// .publicKey forces the memoization
assert.strictEqual(keyPair.publicKey.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
assert.strictEqual(keyPair.__Q.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
}))
})
describe('fromPrivateKey', function () {
it('defaults to compressed', function () {
const keyPair = ECPair.fromPrivateKey(ONE)
@ -49,8 +65,6 @@ describe('ECPair', function () {
fixtures.valid.forEach(function (f) {
it('derives public key for ' + f.WIF, function () {
const d = Buffer.from(f.d, 'hex')
console.log(d)
const keyPair = ECPair.fromPrivateKey(d, {
compressed: f.compressed
})
@ -59,37 +73,25 @@ describe('ECPair', function () {
})
})
fixtures.invalid.constructor.forEach(function (f) {
fixtures.invalid.fromPrivateKey.forEach(function (f) {
it('throws ' + f.exception, function () {
if (f.d) {
const d = Buffer.from(f.d, 'hex')
assert.throws(function () {
ECPair.fromPrivateKey(d, f.options)
}, new RegExp(f.exception))
} else {
const Q = Buffer.from(f.Q, 'hex')
assert.throws(function () {
ECPair.fromPublicKey(Q, f.options)
}, new RegExp(f.exception))
}
const d = Buffer.from(f.d, 'hex')
assert.throws(function () {
ECPair.fromPrivateKey(d, f.options)
}, new RegExp(f.exception))
})
})
})
describe('getPublicKey', function () {
let keyPair
beforeEach(function () {
keyPair = ECPair.fromPrivateKey(ONE)
describe('fromPublicKey', function () {
fixtures.invalid.fromPublicKey.forEach(function (f) {
it('throws ' + f.exception, function () {
const Q = Buffer.from(f.Q, 'hex')
assert.throws(function () {
ECPair.fromPublicKey(Q, f.options)
}, new RegExp(f.exception))
})
})
it('calls pointFromScalar lazily', hoodwink(function () {
assert.strictEqual(keyPair.__Q, null)
// .publicKey forces the memoization
assert.strictEqual(keyPair.publicKey.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
assert.strictEqual(keyPair.__Q.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
}))
})
describe('fromWIF', function () {

View File

@ -66,7 +66,7 @@
}
],
"invalid": {
"constructor": [
"fromPrivateKey": [
{
"exception": "Private key not in range \\[1, n\\)",
"d": "0000000000000000000000000000000000000000000000000000000000000000"
@ -93,7 +93,26 @@
"network": {}
}
}
],
"fromPublicKey": [
{
"exception": "Expected isPoint, got Buffer",
"Q": "",
"options": {}
},
{
"exception": "Expected property \"network.messagePrefix\" of type Buffer|String, got undefined",
"Q": "044289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34cec320a0565fb7caf11b1ca2f445f9b7b012dda5718b3cface369ee3a034ded6",
"options": {
"network": {}
}
},
{
"description": "Bad X coordinate (== P)",
"exception": "Expected isPoint, got Buffer",
"Q": "040000000000000000000000000000000000000000000000000000000000000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
"options": {}
}
],
"fromWIF": [
{

View File

@ -7,7 +7,7 @@
},
"expected": {
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
"signature": null,
"signatures": null,
"input": null,
"witness": null
}
@ -19,7 +19,7 @@
},
"expected": {
"output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG",
"signature": null,
"signatures": null,
"input": null,
"witness": null
}
@ -116,19 +116,6 @@
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
"input": "ffffffffffffffff"
}
},
{
"exception": "Input has invalid signature",
"arguments": {
"input": "30060201ff0201ff01"
}
},
{
"exception": "Signature mismatch",
"arguments": {
"signature": "300602010002010001",
"input": "300602010302010301"
}
}
],
"dynamic": {

View File

@ -204,13 +204,6 @@
"hash": "ffffffffffffffffffffffffffffffffffffffff",
"input": "300602010002010001 030000000000000000000000000000000000000000000000000000000000000001"
}
},
{
"exception": "Signature mismatch",
"arguments": {
"signature": "300602010002010001",
"input": "300602010302010301 030000000000000000000000000000000000000000000000000000000000000001"
}
}
],
"dynamic": {

View File

@ -269,20 +269,6 @@
]
}
},
{
"exception": "Witness and redeem.witness mismatch",
"arguments": {
"redeem": {
"output": "OP_TRUE",
"witness": [
"04000000ff"
]
},
"witness": [
"04000000ee"
]
}
},
{
"exception": "Ambiguous witness source",
"arguments": {