import { PrivateKey , PublicKey , Signable , SignatureScheme } from '../crypto' ; import { hex2VarBytes , StringReader } from '../utils' ; import { getParamsFromProgram , getProgramInfo , programFromMultiPubKey , programFromParams , programFromPubKey } from './program' ; /**
 * Signature generated by signing transaction hash with Private Keys.
 */ export class TxSignature { param sr static deserialize ( sr ) { const sig = new TxSignature ( ) ; const invocationScript = sr . readNextBytes ( ) ; const verificationScript = sr . readNextBytes ( ) ; const sigData = getParamsFromProgram ( invocationScript ) ; const info = getProgramInfo ( verificationScript ) ; sig . M = info . M ; sig . pubKeys = info . pubKeys ; sig . sigData = sigData ; return sig ; } param hash param privateKey param scheme static create ( hash , privateKey , scheme ? ) { const signature = new TxSignature ( ) ; signature . M = 1 ; signature . pubKeys = [ privateKey . getPublicKey ( ) ] ; signature . sigData = [ privateKey . sign ( hash , scheme ) . serializeHex ( ) ] ; return signature ; } param hash param privateKey param scheme static async createAsync ( hash , privateKey , scheme ? ) { const signature = new TxSignature ( ) ; signature . M = 1 ; signature . pubKeys = [ privateKey . getPublicKey ( ) ] ; signature . sigData = [ ( await privateKey . signAsync ( hash , scheme ) ) . serializeHex ( ) ] ; return signature ; } /**
     * Public keys used to create this signature.
     */ pubKeys : PublicKey [ ] ; M : number ; sigData : string [ ] ; /**
     * Serializes signature to Hex representation.
     *
     */ serialize ( ) { let result = '' ; const invocationScript = programFromParams ( this . sigData ) ; let verificationScript = '' ; if ( this . pubKeys . length === 0 ) { throw new Error ( 'No pubkeys in sig' ) ; } else if ( this . pubKeys . length === 1 ) { verificationScript = programFromPubKey ( this . pubKeys [ 0 ] ) ; } else { verificationScript = programFromMultiPubKey ( this . pubKeys , this . M ) ; } result += hex2VarBytes ( invocationScript ) ; result += hex2VarBytes ( verificationScript ) ; return result ; } }