rollDice.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Import modules. */
  2. import { broadcast } from '@nexajs/provider'
  3. import { buildCoins } from '@nexajs/purse'
  4. import {
  5. encodeDataPush,
  6. encodeNullData,
  7. OP,
  8. } from '@nexajs/script'
  9. import {
  10. hexToBin,
  11. utf8ToBin,
  12. } from '@nexajs/utils'
  13. /* Import (local) modules. */
  14. import { useWalletStore } from '@/stores/wallet'
  15. /* Initialize constants. */
  16. const TIMETOCASHOUT_HEX = '6c6c6c6c5479009c63557a7cad6d6d67547a519d537ab275030051147b7e00cd8800cc55ea537a94a2697568'
  17. export default async function () {
  18. /* Initialize locals. */
  19. let coins
  20. let lockingScript
  21. let nullData
  22. let receivers
  23. let response
  24. let scriptPubkey
  25. let unlockingScript
  26. let userData
  27. /* Initialize wallet store. */
  28. const Wallet = useWalletStore()
  29. /* Validate play address. */
  30. if (!this.address) {
  31. throw new Error('Missing play address.')
  32. }
  33. /* Validate authorization hash. */
  34. if (!this.authHash) {
  35. throw new Error('Missing authorization hash.')
  36. }
  37. /* Set script public key. */
  38. scriptPubkey = encodeDataPush(Wallet.publicKey)
  39. /* Set locking script. */
  40. lockingScript = hexToBin(TIMETOCASHOUT_HEX)
  41. // console.info('\nCONTRACT TEMPLATE', binToHex(lockingScript))
  42. /* Set unlocking script. */
  43. // NOTE: Index of (executed) contract method.
  44. unlockingScript = new Uint8Array([
  45. ...encodeDataPush(scriptPubkey),
  46. ...utf8ToBin('{{SIGNATURE}}'), // placeholder for signature
  47. OP.ZERO, // contract function index
  48. ])
  49. /* Handle coin locks. */
  50. coins = Wallet.coins.map(_coin => {
  51. return {
  52. ..._coin,
  53. locking: lockingScript,
  54. unlocking: unlockingScript,
  55. }
  56. })
  57. // console.log('COINS-2', coins)
  58. /* Initialize receivers. */
  59. receivers = []
  60. /* Initialize user data. */
  61. userData = [
  62. 'NEXA.games',
  63. this.authHash,
  64. ]
  65. // console.log('AUTH HASH', this.authHash)
  66. /* Initialize hex data. */
  67. nullData = encodeNullData(userData)
  68. // console.log('HEX DATA', nullData)
  69. /* Validate user data. */
  70. if (userData) {
  71. console.log('USER DATA', userData)
  72. // TODO Validate data length is less than OP_RETURN max (220).
  73. /* Add OP_RETURN data. */
  74. receivers.push({
  75. data: nullData,
  76. })
  77. }
  78. /* Add value output. */
  79. receivers.push({
  80. address: this.address,
  81. satoshis: BigInt(this.satoshis),
  82. })
  83. receivers.push({
  84. address: Wallet.address,
  85. // satoshis: changeVal,
  86. })
  87. console.log('RECEIVERS', receivers)
  88. /* Build transaction. */
  89. response = await buildCoins(coins, receivers)
  90. .catch(err => console.error(err))
  91. console.log('BUILD TRANSACTION', response.raw)
  92. /* Broadcast transaction. */
  93. response = await broadcast(response.raw)
  94. .catch(err => console.error(err))
  95. console.log('SEND TRANSACTION', response)
  96. try {
  97. const txResult = JSON.parse(response)
  98. console.log('TX RESULT', txResult)
  99. if (txResult?.result) {
  100. // TEMP FOR DEV PURPOSES ONLY
  101. window.open('https://nexa.games/fairplay/' + this.playid)
  102. }
  103. if (txResult?.error) {
  104. if (txResult?.error.message.includes('txn-mempool-conflict')) {
  105. return alert(`Oops! Please wait for pending transactions to confirm before sending.`)
  106. }
  107. if (txResult?.error.message.includes('bad-txns-in-belowout')) {
  108. return alert(`Oops! You don't have enough NEXA to send that amount.`)
  109. }
  110. alert(txResult?.error.message)
  111. }
  112. /* Reset gameplay. */
  113. this.resetGameplay()
  114. } catch (err) {
  115. console.error(err)
  116. }
  117. }