Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createRefundableCollateralScript (borrowerPubKey, lenderPubKey, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration) {
let loanExpirationHex = scriptNumEncode(loanExpiration)
let biddingExpirationHex = scriptNumEncode(biddingExpiration)
const borrowerPubKeyHash = hash160(borrowerPubKey)
const borrowerPubKeyPushDataOpcode = padHexStart((borrowerPubKey.length / 2).toString(16))
const lenderPubKeyPushDataOpcode = padHexStart((lenderPubKey.length / 2).toString(16))
const loanExpirationPushDataOpcode = padHexStart(loanExpirationHex.length.toString(16))
const loanExpirationHexEncoded = loanExpirationHex.toString('hex')
const biddingExpirationPushDataOpcode = padHexStart(biddingExpirationHex.length.toString(16))
const biddingExpirationHexEncoded = biddingExpirationHex.toString('hex')
return [
'63', // OP_IF
'82', // OP_SIZE
'01', // OP_PUSHDATA(1)
createSeizableCollateralScript (borrowerPubKey, lenderPubKey, secretHashA1, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration, seizureExpiration) {
let loanExpirationHex = scriptNumEncode(loanExpiration)
let biddingExpirationHex = scriptNumEncode(biddingExpiration)
let seizureExpirationHex = scriptNumEncode(seizureExpiration)
const borrowerPubKeyHash = hash160(borrowerPubKey)
const borrowerPubKeyPushDataOpcode = padHexStart((borrowerPubKey.length / 2).toString(16))
const lenderPubKeyHash = hash160(lenderPubKey)
const lenderPubKeyPushDataOpcode = padHexStart((lenderPubKey.length / 2).toString(16))
const loanExpirationPushDataOpcode = padHexStart(loanExpirationHex.length.toString(16))
const loanExpirationHexEncoded = loanExpirationHex.toString('hex')
const biddingExpirationPushDataOpcode = padHexStart(biddingExpirationHex.length.toString(16))
const biddingExpirationHexEncoded = biddingExpirationHex.toString('hex')
const seizureExpirationPushDataOpcode = padHexStart(seizureExpirationHex.length.toString(16))
const seizureExpirationHexEncoded = seizureExpirationHex.toString('hex')
return [
'63', // OP_IF
requiresSecret = true
} else if (period === 'seizurePeriod' && seizable === true) {
secret = borrowerSecretParam
requiresSecret = true
}
if (period === 'loanPeriod') {
lockTime = 0
} else if (period === 'biddingPeriod') {
lockTime = loanExpiration + 100
} else if (period === 'seizurePeriod') {
lockTime = biddingExpiration + 100
} else {
lockTime = seizureExpiration + 100
}
const lockTimeHex = padHexStart(scriptNumEncode(lockTime).toString('hex'), 8)
const pubKey = (period === 'seizurePeriod' && requiresSecret) ? lenderPubKey : borrowerPubKey
const to = pubKeyToAddress(pubKey, this._network.name, 'pubKeyHash')
const scriptPubKey = padHexStart(script)
const p2shAddress = pubKeyToAddress(scriptPubKey, this._network.name, 'scriptHash')
const sendScript = this.getMethod('createScript')(p2shAddress)
const initiationTxRaw = await this.getMethod('getRawTransactionByHash')(initiationTxHash)
const initiationTx = await this.getMethod('splitTransaction')(initiationTxRaw, true)
const voutIndex = initiationTx.outputs.findIndex((output) => output.script.toString('hex') === sendScript)
const txHashLE = Buffer.from(initiationTxHash, 'hex').reverse().toString('hex') // TX HASH IN LITTLE ENDIAN
const newTxInput = this.generateSigTxInput(txHashLE, voutIndex, script)
const newTx = this.generateRawTx(initiationTx, voutIndex, to, newTxInput, lockTimeHex)
const splitNewTx = await this.getMethod('splitTransaction')(newTx, true)
async _multisigSignCollateral (initiationTxHash, script, loanExpiration, to, from) {
const lockTime = loanExpiration + 100
const lockTimeHex = padHexStart(scriptNumEncode(lockTime).toString('hex'), 8)
const scriptPubKey = padHexStart(script)
const p2shAddress = pubKeyToAddress(scriptPubKey, this._network.name, 'scriptHash')
const sendScript = this.getMethod('createScript')(p2shAddress)
const initiationTxRaw = await this.getMethod('getRawTransactionByHash')(initiationTxHash)
const initiationTx = await this.getMethod('splitTransaction')(initiationTxRaw, true)
const voutIndex = initiationTx.outputs.findIndex((output) => output.script.toString('hex') === sendScript)
const txHashLE = Buffer.from(initiationTxHash, 'hex').reverse().toString('hex') // TX HASH IN LITTLE ENDIAN
const newTxInput = this.generateSigTxInput(txHashLE, voutIndex, script)
const newTx = this.generateRawTx(initiationTx, voutIndex, to, newTxInput, lockTimeHex)
const splitNewTx = await this.getMethod('splitTransaction')(newTx, true)
const outputScriptObj = await this.getMethod('serializeTransactionOutputs')(splitNewTx)
const outputScript = outputScriptObj.toString('hex')
createRefundableCollateralScript (borrowerPubKey, lenderPubKey, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration) {
let loanExpirationHex = scriptNumEncode(loanExpiration)
let biddingExpirationHex = scriptNumEncode(biddingExpiration)
const borrowerPubKeyHash = hash160(borrowerPubKey)
const borrowerPubKeyPushDataOpcode = padHexStart((borrowerPubKey.length / 2).toString(16))
const lenderPubKeyPushDataOpcode = padHexStart((lenderPubKey.length / 2).toString(16))
const loanExpirationPushDataOpcode = padHexStart(loanExpirationHex.length.toString(16))
const loanExpirationHexEncoded = loanExpirationHex.toString('hex')
const biddingExpirationPushDataOpcode = padHexStart(biddingExpirationHex.length.toString(16))
const biddingExpirationHexEncoded = biddingExpirationHex.toString('hex')
return [
'63', // OP_IF
'82', // OP_SIZE
'01', // OP_PUSHDATA(1)
'20', // Hex 32
requiresSecret = true
}
if (period === 'loanPeriod') {
lockTime = 0
} else if (period === 'biddingPeriod') {
lockTime = loanExpiration + 100
} else if (period === 'seizurePeriod') {
lockTime = biddingExpiration + 100
} else {
lockTime = seizureExpiration + 100
}
const lockTimeHex = padHexStart(scriptNumEncode(lockTime).toString('hex'), 8)
const pubKey = (period === 'seizurePeriod' && requiresSecret) ? lenderPubKey : borrowerPubKey
const to = pubKeyToAddress(pubKey, this._network.name, 'pubKeyHash')
const scriptPubKey = padHexStart(script)
const p2shAddress = pubKeyToAddress(scriptPubKey, this._network.name, 'scriptHash')
const sendScript = this.getMethod('createScript')(p2shAddress)
const initiationTxRaw = await this.getMethod('getRawTransactionByHash')(initiationTxHash)
const initiationTx = await this.getMethod('splitTransaction')(initiationTxRaw, true)
const voutIndex = initiationTx.outputs.findIndex((output) => output.script.toString('hex') === sendScript)
const txHashLE = Buffer.from(initiationTxHash, 'hex').reverse().toString('hex') // TX HASH IN LITTLE ENDIAN
const newTxInput = this.generateSigTxInput(txHashLE, voutIndex, script)
const newTx = this.generateRawTx(initiationTx, voutIndex, to, newTxInput, lockTimeHex)
const splitNewTx = await this.getMethod('splitTransaction')(newTx, true)
const outputScriptObj = await this.getMethod('serializeTransactionOutputs')(splitNewTx)
const outputScript = outputScriptObj.toString('hex')
async _multisigSignCollateral (initiationTxHash, script, loanExpiration, to, from) {
const lockTime = loanExpiration + 100
const lockTimeHex = padHexStart(scriptNumEncode(lockTime).toString('hex'), 8)
const scriptPubKey = padHexStart(script)
const p2shAddress = pubKeyToAddress(scriptPubKey, this._network.name, 'scriptHash')
const sendScript = this.getMethod('createScript')(p2shAddress)
const initiationTxRaw = await this.getMethod('getRawTransactionByHash')(initiationTxHash)
const initiationTx = await this.getMethod('splitTransaction')(initiationTxRaw, true)
const voutIndex = initiationTx.outputs.findIndex((output) => output.script.toString('hex') === sendScript)
const txHashLE = Buffer.from(initiationTxHash, 'hex').reverse().toString('hex') // TX HASH IN LITTLE ENDIAN
const newTxInput = this.generateSigTxInput(txHashLE, voutIndex, script)
const newTx = this.generateRawTx(initiationTx, voutIndex, to, newTxInput, lockTimeHex)
const splitNewTx = await this.getMethod('splitTransaction')(newTx, true)
const outputScriptObj = await this.getMethod('serializeTransactionOutputs')(splitNewTx)
const outputScript = outputScriptObj.toString('hex')
const walletAddress = await this.getMethod('getWalletAddress')(from)
const signature = await this.getMethod('signP2SHTransaction')(
if (period === 'loanPeriod') {
lockTime = 0
} else if (period === 'biddingPeriod') {
lockTime = loanExpiration + 100
} else if (period === 'seizurePeriod') {
lockTime = biddingExpiration + 100
} else {
lockTime = seizureExpiration + 100
}
const lockTimeHex = padHexStart(scriptNumEncode(lockTime).toString('hex'), 8)
const pubKey = (period === 'seizurePeriod' && requiresSecret) ? lenderPubKey : borrowerPubKey
const to = pubKeyToAddress(pubKey, this._network.name, 'pubKeyHash')
const scriptPubKey = padHexStart(script)
const p2shAddress = pubKeyToAddress(scriptPubKey, this._network.name, 'scriptHash')
const sendScript = this.getMethod('createScript')(p2shAddress)
const initiationTxRaw = await this.getMethod('getRawTransactionByHash')(initiationTxHash)
const initiationTx = await this.getMethod('splitTransaction')(initiationTxRaw, true)
const voutIndex = initiationTx.outputs.findIndex((output) => output.script.toString('hex') === sendScript)
const txHashLE = Buffer.from(initiationTxHash, 'hex').reverse().toString('hex') // TX HASH IN LITTLE ENDIAN
const newTxInput = this.generateSigTxInput(txHashLE, voutIndex, script)
const newTx = this.generateRawTx(initiationTx, voutIndex, to, newTxInput, lockTimeHex)
const splitNewTx = await this.getMethod('splitTransaction')(newTx, true)
const outputScriptObj = await this.getMethod('serializeTransactionOutputs')(splitNewTx)
const outputScript = outputScriptObj.toString('hex')
const walletAddress = await this.getMethod('getWalletAddress')(to)
const signature = await this.getMethod('signP2SHTransaction')(
generateRawTx (initiationTx, voutIndex, address, input, locktime) {
const output = initiationTx.outputs[voutIndex]
const value = parseInt(reverseBuffer(output.amount).toString('hex'), 16)
const fee = this.getMethod('calculateFee')(1, 1, 3)
const amount = value - fee
const amountLE = Buffer.from(padHexStart(amount.toString(16), 16), 'hex').reverse().toString('hex') // amount in little endian
const pubKeyHash = addressToPubKeyHash(address)
return [
'01000000', // VERSION
input,
'01', // NUM OUTPUTS
amountLE,
'19', // data size to be pushed
'76', // OP_DUP
'a9', // OP_HASH160
'14', // data size to be pushed
pubKeyHash, //
'88', // OP_EQUALVERIFY
'ac', // OP_CHECKSIG
locktime // LOCKTIME
].join('')
}
createScript (address) {
address = addressToString(address)
const type = base58.decode(address).toString('hex').substring(0, 2).toUpperCase()
const pubKeyHash = addressToPubKeyHash(address)
if (type === this._network.pubKeyHash) {
return [
'76', // OP_DUP
'a9', // OP_HASH160
'14', // data size to be pushed
pubKeyHash, //
'88', // OP_EQUALVERIFY
'ac' // OP_CHECKSIG
].join('')
} else if (type === this._network.scriptHash) {
return [
'a9', // OP_HASH160
'14', // data size to be pushed
pubKeyHash, //
'87' // OP_EQUAL
].join('')