Created
May 2, 2017 23:18
-
-
Save dmdeklerk/1956ee75b3a975166f568edb185648e2 to your computer and use it in GitHub Desktop.
Gateway microservice without comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module microservice.gateway { | |
interface GatewayServiceSettings { | |
heat_secret_phrase: string; | |
btc_private_key: string; | |
btc_token: string; | |
} | |
@MicroService('gateway.service') | |
class GatewayService extends AbstractMicroService<GatewayServiceSettings> { | |
private subscriber = subscriber.create('gateway.service.btc').assetTransfer(); | |
constructor(config: Config) { | |
super(config); | |
var heat_account = Account.getId(Crypto2.getPublicKey(this.settings.heat_secret_phrase)); | |
this.subscriber.recipient(heat_account).confirmations(10).onConfirmed((event) => { | |
this.onAssetTransfer(event.transaction)}).subscribe(); | |
} | |
private onAssetTransfer(transaction: Java.com.heatledger.Transaction) { | |
var btc_address = util.decryptEncryptedMessage(transaction, this.settings.heat_secret_phrase); | |
this.subscriber.put(transaction.id, subscriber.COMPLETE, subscriber.TRUE); | |
var quantity = attachment.quantity - (attachment.quantity * .0025); | |
var attachment: Java.com.heatledger.Attachment.ColoredCoinsAssetTransfer = <any> transaction.attachment; | |
var response = this.sendBTC(btc_address, quantity); | |
this.subscriber.put(transaction.id, 'BTC.TRANSACTION.ID', response.transactionId); | |
this.subscriber.put(transaction.id, 'BTC.TRANSACTION.TIMESTAMP', response.transactionTimestamp); | |
} | |
private sendBTC(btc_recipient: string, btc_amount: number) { | |
var microtx = { from_private: this.settings.btc_private_key,to_address: btc_recipient,value_satoshis: btc_amount }; | |
var url = 'https://api.blockcypher.com/v1/bcy/test/txs/micro?token='+this.settings.btc_token; | |
return JSON.parse(heat.createHTTPClient().post(url, JSON.stringify(microtx))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment