scrypttest
Advanced tools
Comparing version 0.1.20 to 0.1.21
@@ -1,2 +0,2 @@ | ||
export { buildContractClass, bsv } from './local'; | ||
export { lockScriptTx, unlockScriptTx, getSighashPreimage, getSignature, showError } from './remote'; | ||
export { buildContractClass, bsv, literal2Asm, int2Asm } from './local'; | ||
export { lockScriptTx, unlockScriptTx, unlockFundedScriptTx, getSighashPreimage, getFundedSighashPreimage, getSignature, showError } from './remote'; |
@@ -6,7 +6,11 @@ "use strict"; | ||
exports.bsv = local_1.bsv; | ||
exports.literal2Asm = local_1.literal2Asm; | ||
exports.int2Asm = local_1.int2Asm; | ||
var remote_1 = require("./remote"); | ||
exports.lockScriptTx = remote_1.lockScriptTx; | ||
exports.unlockScriptTx = remote_1.unlockScriptTx; | ||
exports.unlockFundedScriptTx = remote_1.unlockFundedScriptTx; | ||
exports.getSighashPreimage = remote_1.getSighashPreimage; | ||
exports.getFundedSighashPreimage = remote_1.getFundedSighashPreimage; | ||
exports.getSignature = remote_1.getSignature; | ||
exports.showError = remote_1.showError; |
import bsv = require('bsv'); | ||
declare function int2Asm(num: number | BigInt): string; | ||
declare function literal2Asm(l: boolean | string | number | BigInt): string; | ||
/** | ||
@@ -11,2 +13,2 @@ * construct a class reflecting sCrypt contract | ||
declare function buildContractClass(sourcePath: any, tx?: any, nin?: number, inputSatoshis?: number): any; | ||
export { buildContractClass, bsv }; | ||
export { buildContractClass, bsv, int2Asm, literal2Asm }; |
@@ -17,3 +17,4 @@ 'use strict'; | ||
const AST_SUFFIX = '_ast.json'; | ||
function int2sm(num) { | ||
// convert int literals to script ASM format | ||
function int2Asm(num) { | ||
if (num === -1) { | ||
@@ -25,6 +26,7 @@ return 'OP_1NEGATE'; | ||
} | ||
const n = BN.fromNumber(num); | ||
const n = typeof num === 'number' ? BN.fromNumber(num) : new BN(num.toString()); | ||
const m = n.toSM({ endian: 'little' }); | ||
return m.toString('hex'); | ||
} | ||
exports.int2Asm = int2Asm; | ||
// TODO: error handling | ||
@@ -40,13 +42,10 @@ // convert literals to script ASM format | ||
} | ||
// hex int/bytes | ||
// bytes | ||
if (typeof l === 'string') { | ||
if (l.startsWith('0x')) { | ||
return l.slice(2); | ||
} | ||
return l; | ||
} | ||
// decimal int | ||
if (typeof l === 'number') { | ||
return int2sm(l); | ||
} | ||
// int | ||
return int2Asm(l); | ||
} | ||
exports.literal2Asm = literal2Asm; | ||
/** | ||
@@ -157,2 +156,4 @@ * construct a class reflecting sCrypt contract | ||
bsv, | ||
int2Asm, | ||
literal2Asm, | ||
}; |
declare const lockScriptTx: (scriptPubKeyStr: any, key: any, amount: any) => Promise<any>; | ||
declare const unlockScriptTx: (scriptSigStr: any, lockingTxid: any, scriptPubKeyStr: any, inputAmount: any, newScriptPubKeyStr: any, outputAmount: any) => Promise<any>; | ||
declare const unlockFundedScriptTx: (key: any, scriptSigStr: any, lockingTxid: any, scriptPubKeyStr: any, inputAmount: any, newScriptPubKeyStr: any, outputAmount: any) => Promise<any>; | ||
declare const getSighashPreimage: (lockingTxid: any, scriptPubKeyStr: any, inputAmount: any, newScriptPubKeyStr: any, outputAmount: any) => any; | ||
declare const getSignature: (lockingTxid: any, privateKey: any, scriptPubKeyStr: any, inputAmount: any, newScriptPubKeyStr: any, outputAmount: any) => any; | ||
declare const getFundedSighashPreimage: (key: any, lockingTxid: any, scriptPubKeyStr: any, inputAmount: any, newScriptPubKeyStr: any, outputAmount: any) => Promise<{ | ||
preimage: any; | ||
change: any; | ||
fee: number; | ||
}>; | ||
declare const showError: (error: any) => void; | ||
export { lockScriptTx, unlockScriptTx, getSighashPreimage, getSignature, showError }; | ||
export { lockScriptTx, unlockScriptTx, unlockFundedScriptTx, getSighashPreimage, getFundedSighashPreimage, getSignature, showError }; |
@@ -24,2 +24,3 @@ 'use strict'; | ||
const SIGHASH_TYPE = bsv.crypto.Signature.SIGHASH_ALL | bsv.crypto.Signature.SIGHASH_FORKID; | ||
const SIGHASH_ALLANY = SIGHASH_TYPE | bsv.crypto.Signature.SIGHASH_ANYONECANPAY; | ||
const fetchUtxos = (address) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -62,2 +63,34 @@ let { data: utxos } = yield axios_1.default.get(`${API_PREFIX}/address/${address}/unspent`); | ||
}; | ||
// unlock funds from a script, but, add an extra input (funding) and output (change) | ||
const buildFundedScriptUnlockTx = (utxos, privateKey, prevTxId, scriptPubKey, inputAmount, newScriptPubKey, outputAmount) => { | ||
// derive PKH from Private Key - to sign P2PKH funding | ||
const fundingPubKey = bsv.PublicKey.fromPrivateKey(privateKey).toBuffer(); | ||
const fundingPKH = bsv.crypto.Hash.sha256ripemd160(fundingPubKey); | ||
const fundingPKHstr = fundingPKH.toString('hex'); | ||
const fundingScriptPubKey = bsv.Script.fromASM('OP_DUP OP_HASH160 ' + fundingPKHstr + ' OP_EQUALVERIFY OP_CHECKSIG'); | ||
const fundingAmount = utxos[0].satoshis; | ||
const fundingTxid = utxos[0].txId; | ||
const fundingOutputIndex = utxos[0].outputIndex; | ||
const tx = new bsv.Transaction(); | ||
tx.addInput(new bsv.Transaction.Input({ | ||
prevTxId, | ||
outputIndex: 0, | ||
script: new bsv.Script(), | ||
}), scriptPubKey, inputAmount); | ||
tx.addInput(new bsv.Transaction.Input({ | ||
prevTxId: fundingTxid, | ||
outputIndex: fundingOutputIndex, | ||
script: new bsv.Script(), | ||
}), fundingScriptPubKey, fundingAmount); | ||
tx.addOutput(new bsv.Transaction.Output({ | ||
script: newScriptPubKey, | ||
satoshis: outputAmount, | ||
})); | ||
tx.change(privateKey.toAddress()); | ||
if (tx.getFee() < MIN_FEE) { | ||
tx.fee(MIN_FEE); | ||
} | ||
// We'll sign it later | ||
return tx; | ||
}; | ||
const sendTx = (txhex) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -93,2 +126,29 @@ const { data: txid } = yield axios_1.default.post(`${API_PREFIX}/tx/raw`, { | ||
exports.unlockScriptTx = unlockScriptTx; | ||
// send tx to fund and unlock previously locked funds, but, add extra input (funding) and output (change) | ||
const unlockFundedScriptTx = (key, scriptSigStr, lockingTxid, scriptPubKeyStr, inputAmount, newScriptPubKeyStr, outputAmount) => __awaiter(void 0, void 0, void 0, function* () { | ||
const scriptSig = bsv.Script.fromASM(scriptSigStr); | ||
const scriptPubKey = bsv.Script.fromASM(scriptPubKeyStr); | ||
const newScriptPubKey = bsv.Script.fromASM(newScriptPubKeyStr); | ||
// step 1: fetch utxos | ||
const privateKey = new bsv.PrivateKey(key); | ||
const utxos = yield fetchUtxos(privateKey.toAddress()); | ||
// step 2: build the unlocking tx | ||
const unlockingTx = buildFundedScriptUnlockTx(utxos, privateKey, lockingTxid, scriptPubKey, inputAmount, newScriptPubKey, outputAmount); | ||
// step 3: set the scriptSig on the zeroth input | ||
unlockingTx.inputs[INPUT_IDX].setScript(scriptSig); | ||
// TODO: simplify as this is just unlocking p2pkh | ||
// step 4: sign the external funding input | ||
const txSig1 = new bsv.Transaction.Signature({ | ||
publicKey: bsv.PublicKey.fromPrivateKey(privateKey), | ||
prevTxId: unlockingTx.inputs[INPUT_IDX + 1].prevTxId, | ||
outputIndex: unlockingTx.inputs[INPUT_IDX + 1].outputIndex, | ||
inputIndex: INPUT_IDX + 1, | ||
signature: bsv.Transaction.Sighash.sign(unlockingTx, privateKey, SIGHASH_TYPE, INPUT_IDX + 1, unlockingTx.inputs[INPUT_IDX + 1].output.script, unlockingTx.inputs[INPUT_IDX + 1].output.satoshisBN), | ||
sigtype: SIGHASH_TYPE, | ||
}); | ||
unlockingTx.inputs[INPUT_IDX + 1].setScript(bsv.Script.buildPublicKeyHashIn(txSig1.publicKey, txSig1.signature.toDER(), txSig1.sigtype)); | ||
// step 5: serialize and send it. | ||
return yield sendTx(unlockingTx.serialize()); | ||
}); | ||
exports.unlockFundedScriptTx = unlockFundedScriptTx; | ||
// helper function to get sighash preimage | ||
@@ -112,2 +172,20 @@ const getSighashPreimage = (lockingTxid, scriptPubKeyStr, inputAmount, newScriptPubKeyStr, outputAmount) => { | ||
exports.getSignature = getSignature; | ||
// helper function to get sighash preimage, but, add extra input (funding) and output (change) | ||
// Use alternate build: buildFundedScriptUnlockTx() | ||
const getFundedSighashPreimage = (key, lockingTxid, scriptPubKeyStr, inputAmount, newScriptPubKeyStr, outputAmount) => __awaiter(void 0, void 0, void 0, function* () { | ||
const scriptPubKey = bsv.Script.fromASM(scriptPubKeyStr); | ||
const newScriptPubKey = bsv.Script.fromASM(newScriptPubKeyStr); | ||
// step 1: fetch utxos | ||
const privateKey = new bsv.PrivateKey(key); | ||
const utxos = yield fetchUtxos(privateKey.toAddress()); | ||
const unlockingTx = buildFundedScriptUnlockTx(utxos, privateKey, lockingTxid, scriptPubKey, inputAmount, newScriptPubKey, outputAmount); | ||
const preimage = bsv.Transaction.sighash.sighashPreimage(unlockingTx, SIGHASH_ALLANY, INPUT_IDX, scriptPubKey, new bsv.crypto.BN(inputAmount), FLAGS); | ||
const fee1 = unlockingTx._inputAmount - unlockingTx._outputAmount; | ||
const satsChange = unlockingTx.outputs[1]._satoshis; | ||
return { preimage: preimage.toString('hex'), | ||
change: satsChange, | ||
fee: fee1, | ||
}; | ||
}); | ||
exports.getFundedSighashPreimage = getFundedSighashPreimage; | ||
// print out error | ||
@@ -139,5 +217,7 @@ const showError = (error) => { | ||
unlockScriptTx, | ||
unlockFundedScriptTx, | ||
getSighashPreimage, | ||
getFundedSighashPreimage, | ||
getSignature, | ||
showError, | ||
}; |
{ | ||
"name": "scrypttest", | ||
"version": "0.1.20", | ||
"version": "0.1.21", | ||
"description": "testing library for sCrypt projects", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22166
468