Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
}
}
async multisigSendCollateral (refundableTxHash, seizableTxHash, borrowerPubKey, lenderPubKey, secretHashA1, secretA2, secretHashB2, secretB3, loanExpiration, biddingExpiration, seizureExpiration, borrowerSignatures, lenderSignatures, to) {
const secretHashA2 = sha256(secretA2)
const secretHashB3 = sha256(secretB3)
const refundableScript = this.createRefundableCollateralScript(borrowerPubKey, lenderPubKey, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration)
const seizableScript = this.createSeizableCollateralScript(borrowerPubKey, lenderPubKey, secretHashA1, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration, seizureExpiration)
const refundableResult = await this._multisigSendCollateral(refundableTxHash, refundableScript, secretA2, secretB3, loanExpiration, borrowerSignatures.refundableSignature, lenderSignatures.refundableSignature, to)
const seizableResult = await this._multisigSendCollateral(seizableTxHash, seizableScript, secretA2, secretB3, loanExpiration, borrowerSignatures.seizableSignature, lenderSignatures.seizableSignature, to)
return { refundableResult, seizableResult }
}
async multisigSendCollateral (refundableTxHash, seizableTxHash, borrowerPubKey, lenderPubKey, secretHashA1, secretA2, secretHashB2, secretB3, loanExpiration, biddingExpiration, seizureExpiration, borrowerSignatures, lenderSignatures, to) {
const secretHashA2 = sha256(secretA2)
const secretHashB3 = sha256(secretB3)
const refundableScript = this.createRefundableCollateralScript(borrowerPubKey, lenderPubKey, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration)
const seizableScript = this.createSeizableCollateralScript(borrowerPubKey, lenderPubKey, secretHashA1, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration, seizureExpiration)
const refundableResult = await this._multisigSendCollateral(refundableTxHash, refundableScript, secretA2, secretB3, loanExpiration, borrowerSignatures.refundableSignature, lenderSignatures.refundableSignature, to)
const seizableResult = await this._multisigSendCollateral(seizableTxHash, seizableScript, secretA2, secretB3, loanExpiration, borrowerSignatures.seizableSignature, lenderSignatures.seizableSignature, to)
return { refundableResult, seizableResult }
}
async generateSecret (message) {
try {
return this.client.getMethod('generateSecret')(message)
} catch (e) {
if (!(e instanceof UnimplementedMethodError)) throw e
}
const address = (await this.client.getMethod('getAddresses')())[0]
const signedMessage = await this.client.getMethod('signMessage')(message, address)
const secret = sha256(signedMessage)
return secret
}
async seizeCollateral (seizableTxHash, borrowerPubKey, lenderPubKey, secretA1, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration, seizureExpiration) {
const secretHashA1 = sha256(secretA1)
const seizableScript = this.createSeizableCollateralScript(borrowerPubKey, lenderPubKey, secretHashA1, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration, seizureExpiration)
return this._refundCollateral(seizableTxHash, seizableScript, borrowerPubKey, lenderPubKey, secretA1, secretHashB2, loanExpiration, biddingExpiration, seizureExpiration, true, 'seizurePeriod')
}
async refundCollateral (refundableTxHash, seizableTxHash, borrowerPubKey, lenderPubKey, secretHashA1, secretHashA2, secretB2, secretHashB3, loanExpiration, biddingExpiration, seizureExpiration) {
const secretHashB2 = sha256(secretB2)
const refundableScript = this.createRefundableCollateralScript(borrowerPubKey, lenderPubKey, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration)
const seizableScript = this.createSeizableCollateralScript(borrowerPubKey, lenderPubKey, secretHashA1, secretHashA2, secretHashB2, secretHashB3, loanExpiration, biddingExpiration, seizureExpiration)
const refundableResult = await this._refundCollateral(refundableTxHash, refundableScript, borrowerPubKey, lenderPubKey, secretHashA1, secretB2, loanExpiration, biddingExpiration, seizureExpiration, false, 'loanPeriod')
const seizableResult = await this._refundCollateral(seizableTxHash, seizableScript, borrowerPubKey, lenderPubKey, secretHashA1, secretB2, loanExpiration, biddingExpiration, seizureExpiration, true, 'loanPeriod')
return { refundableResult, seizableResult }
}
async signMessage (message) {
const derivationPath = this._derivationPath + '0/0'
const hdKey = await this.hdKey(derivationPath)
const msgHash = sha256(message)
const hex = Buffer.from(msgHash, 'hex')
const { v, r, s } = ethUtil.ecsign(hex, hdKey._privateKey)
return { v, r: r.toString('hex'), s: s.toString('hex') }
}