Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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('')
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
'82', // OP_SIZE
'01', // OP_PUSHDATA(1)
'20', // Hex 32
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
'82', // OP_SIZE
'01', // OP_PUSHDATA(1)
'20', // Hex 32
'88', // OP_EQUALVERIFY
'a8', // OP_SHA256
'20', secretHashB2, // OP_PUSHDATA(32) {secretHashB2}
'88', // OP_EQUALVERIFY
'76', 'a9', // OP_DUP OP_HASH160
} else if (period === 'seizurePeriod' && !requiresSecret) {
ifBranch = ['00', '00']
} else if (period === 'refundPeriod') {
ifBranch = ['00', '00', '00']
}
const encodedSecret = requiresSecret
? [
padHexStart((secret.length / 2).toString(16)), // OP_PUSHDATA({secretLength})
secret
]
: [] // OP_0
const signatureEncoded = signature + '01'
const signaturePushDataOpcode = padHexStart((signatureEncoded.length / 2).toString(16))
const pubKeyPushDataOpcode = padHexStart((pubKey.length / 2).toString(16))
const bytecode = [
signaturePushDataOpcode,
signatureEncoded,
pubKeyPushDataOpcode,
pubKey,
...encodedSecret,
...ifBranch
]
return bytecode.join('')
}
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
'82', // OP_SIZE
'01', // OP_PUSHDATA(1)
'20', // Hex 32
'88', // OP_EQUALVERIFY
'a8', // OP_SHA256
'20', secretHashB2, // OP_PUSHDATA(32) {secretHashB2}
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
'88', // OP_EQUALVERIFY
'a8', // OP_SHA256
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
'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
'82', // OP_SIZE
'01', // OP_PUSHDATA(1)
'20', // Hex 32
'88', // OP_EQUALVERIFY
'a8', // OP_SHA256
async generateSecret (message) {
const secretAddressLabel = 'secretAddress'
let address
try {
const labelAddresses = await this._rpc.jsonrpc('getaddressesbylabel', secretAddressLabel)
address = Object.keys(labelAddresses)[0]
} catch (e) { // Label does not exist
address = await this.getNewAddress('legacy', secretAddressLabel) // Signing only possible with legacy addresses
}
const signedMessage = await this.signMessage(message, address)
const secret = sha256(signedMessage)
return secret
}
}
createScript (address) {
address = addressToString(address)
const type = base58.decode(address).toString('hex').substring(0, 2).toUpperCase()
const pubKeyHash = addressToPubKeyHash(address)
if (type === networks.bitcoin_testnet.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 === networks.bitcoin_testnet.scriptHash) {
return [
'a9', // OP_HASH160
'14', // data size to be pushed
pubKeyHash, //
'87' // OP_EQUAL