Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve way to detect scripts and added tests #39

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/wallet/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,16 @@ function extractScriptHashFromP2SH (script) {
}

function getScriptType (script) {
const firstByte = script.slice(0, 1).toString('hex')

switch (firstByte) {
case '21':
return ScriptTypes.PAY_TO_PUBKEY
case '76':
return ScriptTypes.PAY_TO_PUBKEY_HASH
case 'a9':
return ScriptTypes.PAY_TO_SCRIPT_HASH
default:
return ScriptTypes.UNKNOWN
const firstByte = script.at(0)

if (firstByte === 0x41 && script.at(66) === 0xac) {
return ScriptTypes.PAY_TO_PUBKEY
} if (firstByte === 0x76 && script.at(24) === 0xac) {
return ScriptTypes.PAY_TO_PUBKEY_HASH
} if (firstByte === 0xa9 && script.at(22) === 0x87) {
return ScriptTypes.PAY_TO_SCRIPT_HASH
} else {
return ScriptTypes.UNKNOWN
}
}

Expand All @@ -240,3 +239,5 @@ module.exports = {
extractPubkeyHashFromP2PKH,
extractScriptHashFromP2SH
}

// 41049464205950188c29d377eebca6535e0f3699ce4069ecd77ffebfbd0bcf95e3c134cb7d2742d800a12df41413a09ef87a80516353a2f0a280547bb5512dc03da8ac
115 changes: 114 additions & 1 deletion test/test_vectors/tx.json
Original file line number Diff line number Diff line change
@@ -1 +1,114 @@
{"hex":"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff05028f110101ffffffff020010a5d4e80000002321028ae82f58d1e8c334411fb2232cbfb1ada81777dd4350b38055670efe97046efaac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf900000000","value":{"id":"799b985c00bfb2acbd5f29308c602746c5fcde7c51f0d3c9d51ef83480f740f4","version":1,"txInCount":1,"txIns":[{"previousOutput":{"hash":"0000000000000000000000000000000000000000000000000000000000000000","index":"ffffffff"},"script":"028f110101","sequence":4294967295}],"txOutCount":2,"txOuts":[{"value":"1000000000000","pkScriptSize":35,"pkScript":{"type":"Buffer","data":[33,2,138,232,47,88,209,232,195,52,65,31,178,35,44,191,177,173,168,23,119,221,67,80,179,128,85,103,14,254,151,4,110,250,172]}},{"value":"0","pkScriptSize":38,"pkScript":{"type":"Buffer","data":[106,36,170,33,169,237,226,246,28,63,113,209,222,253,63,169,153,223,163,105,83,117,92,105,6,137,121,153,98,180,139,235,216,54,151,78,140,249]}}],"locktime":0,"size":147}}
{
"hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff05028f110101ffffffff020010a5d4e80000002321028ae82f58d1e8c334411fb2232cbfb1ada81777dd4350b38055670efe97046efaac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf900000000",
"value": {
"id": "799b985c00bfb2acbd5f29308c602746c5fcde7c51f0d3c9d51ef83480f740f4",
"version": 1,
"txInCount": 1,
"txIns": [
{
"previousOutput": {
"hash": "0000000000000000000000000000000000000000000000000000000000000000",
"index": "ffffffff"
},
"script": "028f110101",
"sequence": 4294967295
}
],
"txOutCount": 2,
"txOuts": [
{
"value": "1000000000000",
"pkScriptSize": 35,
"pkScript": {
"type": "Buffer",
"data": [
33,
2,
138,
232,
47,
88,
209,
232,
195,
52,
65,
31,
178,
35,
44,
191,
177,
173,
168,
23,
119,
221,
67,
80,
179,
128,
85,
103,
14,
254,
151,
4,
110,
250,
172
]
}
},
{
"value": "0",
"pkScriptSize": 38,
"pkScript": {
"type": "Buffer",
"data": [
106,
36,
170,
33,
169,
237,
226,
246,
28,
63,
113,
209,
222,
253,
63,
169,
153,
223,
163,
105,
83,
117,
92,
105,
6,
137,
121,
153,
98,
180,
139,
235,
216,
54,
151,
78,
140,
249
]
}
}
],
"locktime": 0,
"size": 147
}
}
35 changes: 33 additions & 2 deletions test/unit/wallet.utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ const bip65 = require('bip65')
const {
pubkeyToAddress,
createPayToHash,
serializePayToMultisigWithTimeLockScript
serializePayToMultisigWithTimeLockScript,
getScriptType
} = require('../../src/wallet/utils')
const {
ScriptTypes
} = require('../../src/wallet/scripts')

const TESTNET_NETWORK_BYTE = '71'

Expand All @@ -26,7 +30,7 @@ const keyPairA = bitcoinjs.ECPair.fromPrivateKey(Buffer.from('3b187fd3a10960efe5
const keyPairB = bitcoinjs.ECPair.fromPrivateKey(Buffer.from('5cdc1bf38cd77f6a0f130d50e6e37b1d1e3eb59b78f3fde6c1572f44e7f709ed', 'hex'))

/*
pubkeyToAddress.js
pubkeyToAddress
*/
test('successfully convert public key to address', t => {
const pubkey = Buffer.from('04ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664b', 'hex')
Expand Down Expand Up @@ -71,3 +75,30 @@ test('successfully create pay to hash script', t => {

t.is(p2shScript, expectedp2shScript)
})

/*
getScriptType
*/
test('successfully detect pay to pubkey hash', t => {
const p2pkhScript = bitcoinjs.script.fromASM('OP_DUP OP_HASH160 0817fa995b26604c5ed08c160f0bc2141567ce72 OP_EQUALVERIFY OP_CHECKSIG')

const scriptType = getScriptType(p2pkhScript)

t.is(scriptType, ScriptTypes.PAY_TO_PUBKEY_HASH)
})

test('successfully detect pay to pubkey', t => {
const p2pkScript = bitcoinjs.script.fromASM('049464205950188c29d377eebca6535e0f3699ce4069ecd77ffebfbd0bcf95e3c134cb7d2742d800a12df41413a09ef87a80516353a2f0a280547bb5512dc03da8 OP_CHECKSIG')

const scriptType = getScriptType(p2pkScript)

t.is(scriptType, ScriptTypes.PAY_TO_PUBKEY)
})

test('successfully detect pay to hash script', t => {
const p2shScript = bitcoinjs.script.fromASM('OP_HASH160 0817fa995b26604c5ed08c160f0bc2141567ce72 OP_EQUAL')

const scriptType = getScriptType(p2shScript)

t.is(scriptType, ScriptTypes.PAY_TO_SCRIPT_HASH)
})
Loading