import { Address , Coin , Keyring , Output , TX , MTX , Script } from './index' ; const NETWORK_MINIMUM_OUTPUT_SATOSHIS = 546 ; /**
 * Create a BitAuth Authentication Transaction – a valid bitcoin transaction
 * (usually on-chain) which can serve as the basis for a BitAuth Identity.
 */ export class AuthenticationTransaction { private network : any ; private coins : any [ ] = [ ] ; private identityOutput : any ; private signingOutput : any ; private changeAddress : any ; private otherOutputs : any [ ] = [ ] ; private feeSatoshisPerKb : number ; private mtx : any ; addCoin ( coin ) { this . coins . push ( new Coin ( coin ) ) ; } addOutput ( output ) { this . otherOutputs . push ( new Output ( output ) ) ; } setFeeRate ( satoshisPerByte ) { this . feeSatoshisPerKb = satoshisPerByte * 1000 ; } setIdentityOutput ( address , satoshis = NETWORK_MINIMUM_OUTPUT_SATOSHIS ) { let addr = new Address ( address ) ; this . network = addr . network ; this . identityOutput = new Output ( { address : addr , value : satoshis } ) ; } setSigningOutput ( address , satoshis = NETWORK_MINIMUM_OUTPUT_SATOSHIS ) { this . signingOutput = new Output ( { address : new Address ( address ) , value : satoshis } ) ; } setChangeOutput ( address ) { this . changeAddress = new Address ( address ) ; } sign ( keyring ) { if ( typeof this . identityOutput === 'undefined' ) { throw new Error ( 'An identityOutput must be set before signing an AuthenticationTransaction.' ) ; } if ( typeof this . feeSatoshisPerKb === 'undefined' ) { throw new Error ( 'A fee rate must be set before signing an AuthenticationTransaction.' ) ; } if ( typeof this . changeAddress === 'undefined' ) { throw new Error ( 'A changeOutput must be set before signing an AuthenticationTransaction.' ) ; } let mtx = new MTX ( ) ; mtx . addOutput ( this . identityOutput ) ; if ( typeof this . signingOutput !== 'undefined' ) { mtx . addOutput ( this . signingOutput ) ; } this . otherOutputs . forEach ( ( output ) => { mtx . addOutput ( output ) ; } ) ; return mtx . fund ( this . coins , { rate : this . feeSatoshisPerKb , changeAddress : this . changeAddress } ) . then ( ( ) => { let ring = new Keyring ( keyring ) ; mtx . sign ( ring ) ; this . mtx = mtx ; } ) ; } isValid ( ) { return this . mtx . verify ( ) ; } toTX ( ) { return this . mtx . toTX ( ) ; } toJSON ( ) { return this . mtx . getJSON ( this . network ) ; } toRaw ( ) { return this . mtx . toTX ( ) . toRaw ( ) ; } }