Script: add createPubKeyScriptPubKey

This commit is contained in:
Daniel Cousens 2014-05-25 10:21:04 +10:00
parent 05d0baae7c
commit 08951be66f
3 changed files with 44 additions and 6 deletions

View file

@ -268,6 +268,16 @@ Script.prototype.writeBytes = function(data) {
this.chunks.push(data)
}
// {pubKey} OP_CHECKSIG
Script.createPubKeyScriptPubKey = function(pubKey) {
var script = new Script()
script.writeBytes(pubKey.toBuffer())
script.writeOp(opcodes.OP_CHECKSIG)
return script
}
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
Script.createPubKeyHashScriptPubKey = function(hash) {
var script = new Script()
@ -309,6 +319,13 @@ Script.createMultisigScriptPubKey = function(m, pubKeys) {
return script
}
// {signature}
Script.createPubKeyScriptSig = function(signature) {
var script = new Script()
script.writeBytes(signature)
return script
}
// {signature} {pubKey}
Script.createPubKeyHashScriptSig = function(signature, pubKey) {
var script = new Script()

View file

@ -1,5 +1,13 @@
{
"valid": [
{
"description": "pay-to-PubKey",
"hex": "21031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95ac",
"type": "pubkey",
"hash": "26e645ab170255f2a0a82d29e48f35b14ae7c826",
"pubKey": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95",
"scriptPubKey": true
},
{
"description": "P2SH ScriptPubKey",
"hex": "a914e8c300c87986efa84c37c0519929019ef86eb5b487",

View file

@ -70,21 +70,34 @@ describe('Script', function() {
describe('pay-to-pubKeyHash', function() {
it('matches the test data', function() {
// FIXME: bad
var f = fixtures.valid[2]
var address = Address.fromBase58Check('19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu')
var script = Script.createPubKeyHashScriptPubKey(address.hash)
// FIXME: not good TDD
assert.equal(script.toHex(), fixtures.valid[1].hex)
assert.equal(script.toHex(), f.hex)
})
})
describe('pay-to-pubkey', function() {
it('matches the test data', function() {
// FIXME: bad
var f = fixtures.valid[0]
var pubKey = ECPubKey.fromHex(f.pubKey)
var script = Script.createPubKeyScriptPubKey(pubKey)
assert.equal(script.toHex(), f.hex)
})
})
describe('pay-to-scriptHash', function() {
it('matches the test data', function() {
var hash = new Buffer('e8c300c87986efa84c37c0519929019ef86eb5b4', 'hex')
var script = Script.createP2SHScriptPubKey(hash)
// FIXME: bad
var f = fixtures.valid[1]
var address = Address.fromBase58Check('3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8')
var script = Script.createP2SHScriptPubKey(address.hash)
// FIXME: not good TDD
assert.equal(script.toHex(), fixtures.valid[0].hex)
assert.equal(script.toHex(), f.hex)
})
})