Comments (1)
可以试试我的这个解决办法,通过打补丁的方式,我参考的是之前的uni-app源码的提交记录,但是他们新版又给去除了。只能支持crypto.getRandomValues
和crypto.randomBytes
方法,如果你的第三方库用到crypto其他方法,那只能自己去实现了。
web3 的这些sdk底层都要获取随机数,应该都用了这一套,所以其他币也是同样解决办法。
提交记录参考:feat(app-plus): add crypto
crypto.ts 文件内容:
const lookup = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 62, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0,
0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 0, 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51,
]
function base64Decode(source: string, target: Uint8Array) {
const sourceLength = source.length
const paddingLength =
source[sourceLength - 2] === '=' ? 2 : source[sourceLength - 1] === '=' ? 1 : 0
let tmp
let byteIndex = 0
const baseLength = (sourceLength - paddingLength) & 0xfffffffc
for (var i = 0; i < baseLength; i += 4) {
tmp =
(lookup[source.charCodeAt(i)] << 18) |
(lookup[source.charCodeAt(i + 1)] << 12) |
(lookup[source.charCodeAt(i + 2)] << 6) |
lookup[source.charCodeAt(i + 3)]
target[byteIndex++] = (tmp >> 16) & 0xff
target[byteIndex++] = (tmp >> 8) & 0xff
target[byteIndex++] = tmp & 0xff
}
if (paddingLength === 1) {
tmp =
(lookup[source.charCodeAt(i)] << 10) |
(lookup[source.charCodeAt(i + 1)] << 4) |
(lookup[source.charCodeAt(i + 2)] >> 2)
target[byteIndex++] = (tmp >> 8) & 0xff
target[byteIndex++] = tmp & 0xff
}
if (paddingLength === 2) {
tmp = (lookup[source.charCodeAt(i)] << 2) | (lookup[source.charCodeAt(i + 1)] >> 4)
target[byteIndex++] = tmp & 0xff
}
}
export default {
getRandomValues(arr: {
byteLength: number | undefined
buffer: ArrayBufferLike
byteOffset: number | undefined
}) {
if (
!(
arr instanceof Int8Array ||
arr instanceof Uint8Array ||
arr instanceof Int16Array ||
arr instanceof Uint16Array ||
arr instanceof Int32Array ||
arr instanceof Uint32Array ||
arr instanceof Uint8ClampedArray
)
) {
throw new Error('Expected an integer array')
}
if (arr.byteLength > 65536) {
throw new Error('Can only request a maximum of 65536 bytes')
}
var crypto = uni.requireNativePlugin('DCloud-Crypto')
base64Decode(
crypto.getRandomValues(arr.byteLength),
new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength),
)
return arr
},
randomBytes(arrSize: number) {
if (arrSize > 65536) {
throw new Error('Can only request a maximum of 65536 bytes')
}
var crypto = uni.requireNativePlugin('DCloud-Crypto')
var arr = new Uint8Array(arrSize)
base64Decode(
crypto.getRandomValues(arr.byteLength),
new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength),
)
return arr
},
}
index.ts 文件内容
import crypto from "./crypto";
/**
* 全局内置类型替换
*/
(function (scope) {
'use strict';
if(!(scope as any).crypto){
(scope as any).crypto = crypto
console.log('crypto不存在, 使用自定义crypto进行替换')
}
})(typeof globalThis !== 'undefined'? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this)
console.log('crypto polyfill loaded for App environment.')
from uni-app.
Related Issues (20)
- Vue-cli搭建的项目,开启了广告就报错!!!在线等啊,怎么解决 HOT 1
- Map组件circle的点击事件如何获取
- build命令
- uni file picker 预览不生效
- Vue-cli搭建的项目,开启了广告就报错!!!在线等啊,怎么解决 # HOT 4
- Vue-cli搭建的项目,开启了广告就报错!!!在线等啊,怎么解决 #111
- Vue-cli搭建的项目,开启了广告就报错!!!在线等啊,怎么解决 #
- Vue-cli搭建的项目,开启了广告就报错!!!在线等啊,怎么解决 #
- uniapp + tailwindcss 在H5环境下运行。为什么只设置了border-t上边框,但是显示有4个边框。小程序没问题,H5就不行,调试的css样式中还找不到其他三个边框的属性。大佬们有什么办法去改吗? HOT 6
- input安卓真机输入卡顿,有延迟 HOT 2
- uni vue2 classAPI 使用onShareAppMessage,支付宝中支付宝其他页面跳转过来后无法拉起分享
- 【bug】uni.pageScrollTo(selector)滚动定位的位置错误 HOT 2
- H5环境下,tabBar的高度问题
- App Ios getApp().globalData 挂在数据后,App升级导致数据丢失
- cli项目无法打包本地原生插件
- vulnerabilities high jpeg-js
- uniapp使用vant2
- vue3 模式下jsx渲染的子组件顺序有问题
- `3.0.0-4010520240507001` 版本使用 `vitest` 出现异常:`Error: Cannot find module 'vuex/package.json'` HOT 3
- 动态插槽拿不到作用域插槽的值
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uni-app.