Skip to content

Commit

Permalink
support scriptSig.asm
Browse files Browse the repository at this point in the history
  • Loading branch information
Oded Leiba committed Mar 30, 2016
1 parent 579a34e commit 8944c96
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
15 changes: 11 additions & 4 deletions assetIdEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ var createIdFromPreviousOutputScriptPubKey = function (previousOutputHex, paddin

var createIdFromPubKeyHashInput = function (scriptSig, padding, divisibility) {
debug('createIdFromPubKeyHashInput')
if (!scriptSig.asm) {
scriptSig.asm = bitcoin.script.toASM(scriptSig.hex)
}
var publicKey = scriptSig.asm.split(' ')[1]
debug('publicKey = ', publicKey)
publicKey = new Buffer(publicKey, 'hex')
debug('publicKey = ', publicKey)
var hash256 = hash.sha256(publicKey)
var pubKeyHash = hash.ripemd160(hash256)
debug('pubKeyHash = ', pubKeyHash)
var pubKeyHashOutput = bitcoin.script.pubKeyHashOutput(pubKeyHash)
debug('pubKeyHashOutput = ', pubKeyHashOutput)
return hashAndBase58CheckEncode(pubKeyHashOutput, padding, divisibility)
Expand Down Expand Up @@ -91,8 +94,11 @@ var createIdFromAddress = function (address, padding, divisibility) {
}

var hashAndBase58CheckEncode = function (payloadToHash, padding, divisibility) {
debug('hashAndBase58CheckEncode')
debug('padding and divisibility = ' + padding.toString(16) + ', ' + divisibility)
var hash256 = hash.sha256(payloadToHash)
var hash160 = hash.ripemd160(hash256)
debug('hash160 = ', hash160)
padding = new Buffer(padLeadingZeros(padding.toString(16)), 'hex')
divisibility = new Buffer(padLeadingZeros(divisibility.toString(16), POSTFIXBYTELENGTH), 'hex')
var concatenation = Buffer.concat([padding, hash160, divisibility])
Expand All @@ -119,10 +125,11 @@ module.exports = function (bitcoinTransaction) {
return createIdFromPreviousOutputScriptPubKey(firstInput.previousOutput.hex, padding, divisibility)
}

if (firstInput.scriptSig && firstInput.scriptSig.hex) {
if (firstInput.scriptSig && (firstInput.scriptSig.hex || firstInput.scriptSig.asm)) {
var scriptSig = firstInput.scriptSig
console.log('scriptSig.hex = ', scriptSig.hex)
var buffer = new Buffer(scriptSig.hex, 'hex')
scriptSig.hex = scriptSig.hex || bitcoin.script.fromASM(scriptSig.asm)
debug('scriptSig.hex = ', scriptSig.hex)
var buffer = Buffer.isBuffer(scriptSig.hex) ? scriptSig.hex : new Buffer(scriptSig.hex, 'hex')
var type = bitcoin.script.classifyInput(buffer)
if (type === 'pubkeyhash') {
return createIdFromPubKeyHashInput(scriptSig, padding, divisibility)
Expand Down
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,44 @@ describe('Test asset ID encoder', function () {
})
})
})

describe('create assetID from scriptSig.asm', function () {
it('should return the right asset ID', function (done) {
var bitcoinTransaction = {
'vin': [
{
'scriptSig': {
'asm': '3045022100b5aaae72b05c0698ea22e2f4cb3f3a46e5a0a1c1a98772b1c7305476b9ae5e1f02200276a003694eab8d12bc5791624b60b1c68486e4b985f2a672751bb35295202b01 02b509613c7e5d9e47347635f872c3aa271d01ac4a9a6445839ce2c5820a0f48a8'
}
}
],
'ccdata': [{
'payments': [
{
'input': 0,
'amount': 26,
'output': 0,
'range': false,
'precent': false
}
],
'protocol': 17219,
'version': 2,
'type': 'issuance',
'lockStatus': false,
'aggregationPolicy': 'dispersed',
'divisibility': 2,
'amount': 26,
'multiSig': [],
'torrentHash': 'd36854c461e1ce4d90dd73857ae9fa5349f6ba4a',
'sha2': 'da9f9603314dcbc0225f39b9313b7d67a5059e00e0b179e01cf8f879eaaa7fa3'
}]
}
bitcoinTransaction.ccdata[0].aggregationPolicy = 'dispersed'
var assetId = assetIdEncoder(bitcoinTransaction)
assert.equal(assetId, 'UdDszTzN2NV4frsfVeXQKDiTZMbRvMqDfK6KF4')
console.log(assetId)
done()
})
})

0 comments on commit 8944c96

Please sign in to comment.