wallet.get.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Import modules. */
  2. import BCHJS from '@psf/bch-js'
  3. import { binToHex } from '@nexajs/utils'
  4. // REST API servers.
  5. const BCHN_MAINNET = 'https://bchn.fullstack.cash/v5/'
  6. const runtimeConfig = useRuntimeConfig()
  7. // const jwtAuthToken = runtimeConfig.public.PSF_JWT_AUTH_TOKEN
  8. const jwtAuthToken = runtimeConfig.PSF_JWT_AUTH_TOKEN
  9. // console.log('jwtAuthToken', jwtAuthToken)
  10. // Instantiate bch-js based on the network.
  11. // FIXME https://github.com/Permissionless-Software-Foundation/jwt-bch-demo/blob/master/lib/fullstack-jwt.js
  12. const bchjs = new BCHJS({
  13. restURL: BCHN_MAINNET,
  14. apiToken: jwtAuthToken,
  15. })
  16. // console.log('bchjs', bchjs)
  17. const lang = 'english' // Set the language of the wallet.
  18. const aliceObj = {}
  19. const bobObj = {}
  20. const samObj = {}
  21. async function createWallets () {
  22. try {
  23. // create 256 bit BIP39 mnemonic
  24. const aliceMnemonic = bchjs.Mnemonic.generate(
  25. 128,
  26. bchjs.Mnemonic.wordLists()[lang]
  27. )
  28. console.log('aliceMnemonic', aliceMnemonic)
  29. const bobMnemonic = bchjs.Mnemonic.generate(
  30. 128,
  31. bchjs.Mnemonic.wordLists()[lang]
  32. )
  33. console.log('bobMnemonic', bobMnemonic)
  34. const samMnemonic = bchjs.Mnemonic.generate(
  35. 128,
  36. bchjs.Mnemonic.wordLists()[lang]
  37. )
  38. // root seed buffer
  39. const aliceRootSeed = await bchjs.Mnemonic.toSeed(aliceMnemonic)
  40. console.log('aliceRootSeed', aliceRootSeed)
  41. const bobRootSeed = await bchjs.Mnemonic.toSeed(bobMnemonic)
  42. const samRootSeed = await bchjs.Mnemonic.toSeed(samMnemonic)
  43. // master HDNode
  44. const aliceMasterHDNode = bchjs.HDNode.fromSeed(aliceRootSeed)
  45. const bobMasterHDNode = bchjs.HDNode.fromSeed(bobRootSeed)
  46. const samMasterHDNode = bchjs.HDNode.fromSeed(samRootSeed)
  47. // Use 245 derivation path, which is the BIP44 standard for SLP tokens.
  48. const aliceChildNode = aliceMasterHDNode.derivePath("m/44'/145'/0'/0/0")
  49. const bobChildNode = bobMasterHDNode.derivePath("m/44'/145'/0'/0/0")
  50. const samChildNode = samMasterHDNode.derivePath("m/44'/145'/0'/0/0")
  51. aliceObj.mnemonic = aliceMnemonic
  52. aliceObj.cashAddress = bchjs.HDNode.toCashAddress(aliceChildNode)
  53. aliceObj.legacyAddress = bchjs.HDNode.toLegacyAddress(aliceChildNode)
  54. aliceObj.slpAddress = bchjs.SLP.HDNode.toSLPAddress(aliceChildNode)
  55. aliceObj.WIF = bchjs.HDNode.toWIF(aliceChildNode)
  56. console.log('aliceObj', aliceObj)
  57. bobObj.mnemonic = bobMnemonic
  58. bobObj.cashAddress = bchjs.HDNode.toCashAddress(bobChildNode)
  59. bobObj.legacyAddress = bchjs.HDNode.toLegacyAddress(bobChildNode)
  60. bobObj.slpAddress = bchjs.SLP.HDNode.toSLPAddress(bobChildNode)
  61. bobObj.WIF = bchjs.HDNode.toWIF(bobChildNode)
  62. samObj.mnemonic = samMnemonic
  63. samObj.cashAddress = bchjs.HDNode.toCashAddress(samChildNode)
  64. samObj.legacyAddress = bchjs.HDNode.toLegacyAddress(samChildNode)
  65. samObj.slpAddress = bchjs.SLP.HDNode.toSLPAddress(samChildNode)
  66. samObj.WIF = bchjs.HDNode.toWIF(samChildNode)
  67. // Write out the basic information into a json file for other example apps to use.
  68. // fs.writeFileSync('alice-wallet.json', JSON.stringify(aliceObj, null, 2))
  69. // fs.writeFileSync('bob-wallet.json', JSON.stringify(bobObj, null, 2))
  70. // fs.writeFileSync('sam-wallet.json', JSON.stringify(samObj, null, 2))
  71. console.log(`
  72. Wallets created. To continue the example, you need to fund these wallets.
  73. Send 3000 sats to the Alice's address:
  74. ${aliceObj.cashAddress}
  75. Send 3000 sats to the Bob's address:
  76. ${bobObj.cashAddress}
  77. Send 3000 sats to the Sam's address:
  78. ${samObj.cashAddress}
  79. `)
  80. } catch (err) {
  81. console.error('Error in createWallets(): ', err)
  82. }
  83. }
  84. export default defineEventHandler((event) => {
  85. /* Set database. */
  86. const Db = event.context.Db
  87. // console.log('DB', Db)
  88. /* Set database. */
  89. const Wallet = event.context.Wallet
  90. // console.log('WALLET', Wallet)
  91. const walletPkg = {
  92. address: Wallet.address,
  93. publicKey: binToHex(Wallet.publicKey),
  94. assets: JSON.stringify(Wallet.assets, (key, value) =>
  95. typeof value === 'bigint' ? value.toString() + 'n' : value
  96. ),
  97. coins: JSON.stringify(Wallet.coins, (key, value) =>
  98. typeof value === 'bigint' ? value.toString() + 'n' : value
  99. ),
  100. mnemonic: Wallet.mnemonic,
  101. tokens: JSON.stringify(Wallet.tokens, (key, value) =>
  102. typeof value === 'bigint' ? value.toString() + 'n' : value
  103. ),
  104. aliceObj,
  105. bobObj,
  106. samObj,
  107. }
  108. /* Sanitize private details. */
  109. delete walletPkg.mnemonic
  110. /* Return wallet details. */
  111. return walletPkg
  112. })