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}
_spendMultisigCollateral (secretA2, secretB3, borrowerSignature, lenderSignature) {
const ifBranch = ['51', '00']
const secretA2PushDataOpcode = padHexStart((secretA2.length / 2).toString(16))
const secretB3PushDataOpcode = padHexStart((secretB3.length / 2).toString(16))
const borrowerSignatureEncoded = borrowerSignature + '01'
const borrowerSignaturePushDataOpcode = padHexStart((borrowerSignatureEncoded.length / 2).toString(16))
const lenderSignatureEncoded = lenderSignature + '01'
const lenderSignaturePushDataOpcode = padHexStart((lenderSignatureEncoded.length / 2).toString(16))
const bytecode = [
'00',
borrowerSignaturePushDataOpcode,
borrowerSignatureEncoded,
lenderSignaturePushDataOpcode,
lenderSignatureEncoded,
secretB3PushDataOpcode,
secretB3,
secretA2PushDataOpcode,
secretA2,
...ifBranch
]
return bytecode.join('')
}
async withdrawLoanFund (initiationTxHash, amount) {
const functionSignature = '0x2e1a7d4d'
const initiationTransaction = await this.getMethod('getTransactionReceipt')(initiationTxHash)
const encodedAmount = padHexStart(new BigNumber(amount).toString(16), 64)
return this.getMethod('sendTransaction')(initiationTransaction.contractAddress, 0, functionSignature + encodedAmount)
}
bidLoan (contractAddress, secretHashC, bidValue, aCoinAddress) {
const encodedBidValue = padHexStart(new BigNumber(bidValue).toString(16), 64)
const encodedACoinAddress = padHexStart(aCoinAddress, 64)
return this.getMethod('sendTransaction')(contractAddress, 0, '0x92b0c7ab' + secretHashC + encodedBidValue + encodedACoinAddress)
}
generateErc20Transfer (to, value) {
value = BigNumber(value).toString(16)
const encodedAddress = padHexStart(remove0x(to), 64)
const encodedValue = padHexStart(value, 64)
return [
remove0x(SOL_TRANSFER_FUNCTION),
encodedAddress,
encodedValue
].join('').toLowerCase()
}