import { BigNumber } from "bignumber.js" ; import ethUtil = require ( "ethereumjs-util" ) ; import * as Web3 from "web3" ; import { IssuanceCommitment } from "./issuance_commitment" ; import { OrderParams } from "./schema" ; import { ECDSASignature , SignableMessage } from "./signable_message" ; import { Address , Bytes32 } from "../common" ; import * as solidity from "../../utils/solidity" ; const NULL_ADDRESS = "0x0000000000000000000000000000000000000000" ; const NULL_SIGNATURE = { r : "0x0" , s : "0x0" , v : 0 } ; export interface DebtOrderSignatories { creditor ? : Address ; debtor ? : Address ; underwriter ? : Address ; } export class DebtOrder extends SignableMessage { public params : OrderParams ; constructor ( params ) { super ( ) ; this . params = params ; } public getVersion ( ) { return this . params . version ; } public getIssuanceCommitment ( ) { return this . params . debtIssuanceCommitment ; } public getDebtor ( ) { return this . params . debtor ; } public getDebtKernelContract ( ) { return this . params . debtKernelContract ; } public getDebtTokenContract ( ) { return this . params . debtTokenContract ; } public getPrincipalAmount ( ) { return this . params . principalAmount ; } public getPrincipalTokenAddress ( ) { return this . params . principalTokenAddress ; } public getRelayer ( ) { return this . params . relayer ; } public getDebtorFee ( ) { return this . params . debtorFee ; } public getCreditorFee ( ) { return this . params . creditorFee ; } public getUnderwriterFee ( ) { return this . params . underwriterFee ; } public getExpiration ( ) { return this . params . expirationTimestampInSec ; } public getRelayerFee ( ) { return this . params . relayerFee ; } public getDebtOrderHash ( ) { const hash = solidity . SHA3 ( [ this . getVersion ( ) , this . getIssuanceCommitment ( ) . getHash ( ) , this . getUnderwriterFee ( ) , this . getPrincipalAmount ( ) , this . getPrincipalTokenAddress ( ) , this . getDebtorFee ( ) , this . getCreditorFee ( ) , this . getRelayer ( ) , this . getRelayerFee ( ) , this . getExpiration ( ) , ] ) ; const hashHex = ethUtil . bufferToHex ( hash ) ; return hashHex ; } public getUnderwriterCommitmentHash ( ) { const hash = solidity . SHA3 ( [ this . getVersion ( ) , this . getIssuanceCommitment ( ) . getHash ( ) , this . getUnderwriterFee ( ) , this . getPrincipalAmount ( ) , this . getPrincipalTokenAddress ( ) , this . getExpiration ( ) , ] ) ; const hashHex = ethUtil . bufferToHex ( hash ) ; return hashHex ; } public getHash ( ) { return this . getDebtOrderHash ( ) ; } public async getSignedDebtOrder ( web3 , creditor , signatories ) { const creditorSignature = signatories . creditor ? await this . getSignature ( web3 , signatories . creditor , this . getDebtOrderHash ( ) ) : NULL_SIGNATURE ; const debtorSignature = signatories . debtor ? await this . getSignature ( web3 , signatories . debtor , this . getDebtOrderHash ( ) ) : NULL_SIGNATURE ; const underwriterSignature = signatories . underwriter ? await this . getSignature ( web3 , signatories . underwriter , this . getUnderwriterCommitmentHash ( ) ) : NULL_SIGNATURE ; return new SignedDebtOrder ( this , creditor , creditorSignature , debtorSignature , underwriterSignature ) ; } } export class SignedDebtOrder extends DebtOrder { private creditor : Address ; private creditorSignature : ECDSASignature ; private debtorSignature : ECDSASignature ; private underwriterSignature : ECDSASignature ; constructor ( debtOrder , creditor , creditorSignature , debtorSignature , underwriterSignature ) { super ( debtOrder . params ) ; this . creditor = creditor ; this . creditorSignature = creditorSignature ; this . debtorSignature = debtorSignature ; this . underwriterSignature = underwriterSignature ; } public getCreditor ( ) { return this . creditor ; } public getDebtorSignature ( ) { return this . debtorSignature ; } public getCreditorSignature ( ) { return this . creditorSignature ; } public getUnderwriterSignature ( ) { return this . underwriterSignature ; } public getOrderAddresses ( ) { return [ this . getIssuanceCommitment ( ) . getVersion ( ) , this . getDebtor ( ) , this . getIssuanceCommitment ( ) . getUnderwriter ( ) , this . getIssuanceCommitment ( ) . getTermsContract ( ) , this . getPrincipalTokenAddress ( ) , this . getRelayer ( ) , ] ; } public getOrderValues ( ) { return [ this . getIssuanceCommitment ( ) . getUnderwriterRiskRating ( ) , this . getIssuanceCommitment ( ) . getSalt ( ) , this . getPrincipalAmount ( ) , this . getUnderwriterFee ( ) , this . getRelayerFee ( ) , this . getCreditorFee ( ) , this . getDebtorFee ( ) , this . getExpiration ( ) , ] ; } public getOrderBytes32 ( ) { return [ this . getIssuanceCommitment ( ) . getTermsContractParameters ( ) ] ; } public getSignaturesR ( ) { return [ this . debtorSignature . r , this . creditorSignature . r , this . underwriterSignature . r ] ; } public getSignaturesS ( ) { return [ this . debtorSignature . s , this . creditorSignature . s , this . underwriterSignature . s ] ; } public getSignaturesV ( ) { return [ this . debtorSignature . v , this . creditorSignature . v , this . underwriterSignature . v ] ; } }