import { promisify } from "@0xproject/utils" ; import { BigNumber } from "bignumber.js" ; import * as crypto from "crypto" ; import * as Web3 from "web3" ; import * as solidity from "../../utils/solidity" ; import ethUtil = require ( "ethereumjs-util" ) ; import { Address , Bytes32 , UInt } from "../common" ; import { IssuanceCommitmentParams , Signatories } from "./schema" ; import { ECDSASignature , SignableMessage } from "./signable_message" ; const NULL_ADDRESS = "0x0000000000000000000000000000000000000000" ; const NULL_SIGNATURE = { r : "0x0" , s : "0x0" , v : 0 } ; export class IssuanceCommitment extends SignableMessage { public params : IssuanceCommitmentParams ; constructor ( params , salt ? ) { super ( ) ; this . params = params ; this . params . salt = salt || this . generateSalt ( ) ; } public getVersion ( ) { return this . params . version ; } public getDebtor ( ) { return this . params . debtor ; } public getUnderwriter ( ) { return this . params . underwriter ; } public getUnderwriterRiskRating ( ) { return this . params . underwriterRiskRating ; } public getTermsContract ( ) { return this . params . termsContract ; } public getTermsContractParameters ( ) { return this . params . termsContractParameters ; } public getSalt ( ) { return this . params . salt ; } public async getSignedIssuanceCommitment ( web3 , signatories = { } ) { const debtorSignature = signatories . debtor ? await this . getSignature ( web3 , signatories . debtor ) : NULL_SIGNATURE ; const underwriterSignature = signatories . underwriter ? await this . getSignature ( web3 , signatories . underwriter ) : NULL_SIGNATURE ; return new SignedIssuanceCommitment ( this , debtorSignature , underwriterSignature ) ; } public getHash ( ) { const hash = solidity . SHA3 ( [ this . getVersion ( ) , this . getDebtor ( ) , this . getUnderwriter ( ) , this . getUnderwriterRiskRating ( ) , this . getTermsContract ( ) , this . getTermsContractParameters ( ) , this . getSalt ( ) , ] ) ; const hashHex = ethUtil . bufferToHex ( hash ) ; return hashHex ; } public generateSalt ( ) { const saltBuffer = crypto . randomBytes ( 32 ) ; const saltBufferHex = ethUtil . bufferToHex ( saltBuffer ) ; return new BigNumber ( saltBufferHex ) ; } } export class SignedIssuanceCommitment extends IssuanceCommitment { public issuerSignature : ECDSASignature ; public underwriterSignature ? : ECDSASignature ; constructor ( issuanceCommitment , issuerSignature , underwriterSignature ? ) { super ( issuanceCommitment . params , issuanceCommitment . getSalt ( ) ) ; this . issuerSignature = issuerSignature ; this . underwriterSignature = underwriterSignature ; } public getIssuanceAddresses ( ) { return [ this . getVersion ( ) , this . getDebtor ( ) , this . getUnderwriter ( ) , this . getTermsContract ( ) , ] ; } public getIssuanceValues ( ) { return [ this . getUnderwriterRiskRating ( ) , this . getSalt ( ) ] ; } public getUnderwriterSignature ( ) { return this . underwriterSignature ; } public getSignaturesS ( ) { return [ this . issuerSignature . s , this . underwriterSignature . s ] ; } public getSignaturesV ( ) { return [ this . issuerSignature . v , this . underwriterSignature . v ] ; } }