Created
March 30, 2018 19:06
-
-
Save danrobinson/eeff2c765bcc328d92bf48ba8037c5f9 to your computer and use it in GitHub Desktop.
HLTC on Stellar
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
const HtlcSequenceNumber = Ratchet1SequenceNumber.plus(3) | |
const htlcAmount = 50 | |
const aliceOldBalance = 250 | |
const bobOldBalance = 750 | |
const HtlcTime = moment().unix() | |
const HtlcTimeout = HtlcTime + TIMEOUT_CLAIM | |
const challenge = | |
'7196633b1ce5bf5fa369169ea81fc03a8238b38a888ef226a1e1bb42ab186bcb' | |
const HtlcCancelTransaction = new TransactionBuilder( | |
new Account(RatchetAccountId, HtlcSequenceNumber.plus(1).toString()), | |
{ timebounds: { minTime: HtlcTimeout } } | |
) | |
.addOperation( | |
Operation.payment({ | |
destination: Bob.accountId(), | |
amount: (bobOldBalance + htlcAmount).toString(), | |
asset: Asset.native(), | |
}) | |
) | |
.addOperation( | |
Operation.payment({ | |
destination: Alice.accountId(), | |
amount: (aliceOldBalance - htlcAmount).toString(), | |
asset: Asset.native(), | |
}) | |
) | |
.build() | |
const HtlcFulfillTransaction = new TransactionBuilder( | |
new Account(RatchetAccountId, HtlcSequenceNumber.plus(1).toString()) | |
) | |
.addOperation( | |
Operation.setOptions({ | |
signer: { | |
sha256Hash: challenge, | |
weight: 2, | |
}, | |
}) | |
) | |
.addOperation( | |
Operation.payment({ | |
destination: Alice.accountId(), | |
amount: aliceOldBalance.toString(), | |
asset: Asset.native(), | |
}) | |
) | |
.addOperation( | |
Operation.payment({ | |
destination: Bob.accountId(), | |
amount: bobOldBalance.toString(), | |
asset: Asset.native(), | |
}) | |
) | |
.addOperation( | |
Operation.setOptions({ | |
signer: { | |
sha256Hash: challenge, | |
weight: 0, | |
}, | |
}) | |
) | |
.addOperation( | |
Operation.setOptions({ | |
signer: { | |
preAuthTx: HtlcCancelTransaction.hash(), | |
weight: 0, | |
}, | |
}) | |
) | |
.build() | |
const HtlcTransaction = new TransactionBuilder( | |
new Account(RatchetAccountId, HtlcSequenceNumber.toString()) | |
) | |
.addOperation( | |
Operation.setOptions({ | |
signer: { | |
preAuthTx: HtlcCancelTransaction.hash(), | |
weight: 2, | |
}, | |
}) | |
) | |
.addOperation( | |
Operation.setOptions({ | |
signer: { | |
preAuthTx: HtlcFulfillTransaction.hash(), | |
weight: 2, | |
}, | |
}) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment