-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-scriptbuilder.js
More file actions
42 lines (34 loc) · 2.59 KB
/
Copy pathtest-scriptbuilder.js
File metadata and controls
42 lines (34 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// test-scriptbuilder.js — Use ScriptBuilder to construct proper sigScript
const wasm = require('kaspa-wasm');
const https = require('https');
async function post(u,b){return new Promise((res,rej)=>{const d=JSON.stringify(b);const q=https.request(u,{method:'POST',headers:{'Content-Type':'application/json','Content-Length':Buffer.byteLength(d)}},r=>{let dd='';r.on('data',c=>dd+=c);r.on('end',()=>{try{res(JSON.parse(dd))}catch(_){res(dd)}})});q.on('error',rej);q.write(d);q.end()})}
async function main(){
const tid='09c9071533bfa3aad1ddc178de70021a2c130448fb7e39961552906efde9da48';
const p2pk='20751cb1bf9e74209e4b40086af42bac943c16c0056902e812a3ab37557695e96dac';
// Use ScriptBuilder.addData to construct the proper push
const preimage=Buffer.from('covex:v1:Timelock:htp-lock-001','utf8');
console.log('preimage:', preimage.toString('hex'), 'len:', preimage.length);
// Try ScriptBuilder
const sb = new wasm.ScriptBuilder();
// addData should add the proper push opcode + data
try {
sb.addData(preimage);
const result = sb.drain();
const resultHex = Buffer.from(result).toString('hex');
console.log('ScriptBuilder result:', resultHex.substring(0,60)+'...');
console.log('Result length:', result.length, 'bytes');
const tx={version:0,inputs:[{previousOutpoint:{transactionId:tid,index:0},signatureScript:resultHex,sigOpCount:1,sequence:0}],outputs:[{amount:'49990000',scriptPublicKey:{version:0,scriptPublicKey:p2pk}}],lockTime:0,subnetworkId:'0000000000000000000000000000000000000000',gas:'0',payload:null};
const r=await post('https://api-tn12.kaspa.org/transactions',{transaction:tx});
console.log('ScriptBuilder:', r.transactionId ? 'ACCEPTED '+r.transactionId : JSON.stringify(r).substring(0,500));
} catch(e) {
console.log('ScriptBuilder error:', e.message);
// Try manual approach: OP_PUSHDATA1 (0x4c) + length + data
const push1 = Buffer.concat([Buffer.from([0x4c, preimage.length]), preimage]);
const push1Hex = push1.toString('hex');
console.log('\nManual 4c push:', push1Hex.substring(0,60)+'...');
const tx2={version:0,inputs:[{previousOutpoint:{transactionId:tid,index:0},signatureScript:push1Hex,sigOpCount:1,sequence:0}],outputs:[{amount:'49990000',scriptPublicKey:{version:0,scriptPublicKey:p2pk}}],lockTime:0,subnetworkId:'0000000000000000000000000000000000000000',gas:'0',payload:null};
const r2=await post('https://api-tn12.kaspa.org/transactions',{transaction:tx2});
console.log('Manual 4c:', r2.transactionId ? 'ACCEPTED '+r2.transactionId : JSON.stringify(r2).substring(0,500));
}
}
main().catch(e=>console.error(e));