zetrix-connect-wallet-sdk-test 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +792 -0
- package/lib/chainSDK.js +88 -0
- package/lib/cryptoStr.js +106 -0
- package/lib/h5WebView/apis/apis.js +15 -0
- package/lib/h5WebView/apis/native.js +154 -0
- package/lib/h5WebView/externalProvision/native/index.js +59 -0
- package/lib/h5WebView/h5WEbViewTool.js +90 -0
- package/lib/h5WebView/index.js +219 -0
- package/lib/h5WebView/poolName.js +14 -0
- package/lib/h5WebView/utils/base64.js +319 -0
- package/lib/h5WebView/utils/observerMode.js +15 -0
- package/lib/index.js +507 -0
- package/lib/linkTo.js +53 -0
- package/lib/mobileDetect.js +3 -0
- package/lib/qrcode.js +75 -0
- package/lib/result.js +59 -0
- package/lib/socket.js +139 -0
- package/lib/util.js +221 -0
- package/package.json +14 -0
package/lib/qrcode.js
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
import {qrcanvas} from 'qrcanvas'
|
2
|
+
import {resultInfo} from './result'
|
3
|
+
|
4
|
+
|
5
|
+
export const appendCode = (data, callBack) => {
|
6
|
+
const code = createQrcode(data)
|
7
|
+
let hasDom = document.getElementById('zetrixWalletConnectQr')
|
8
|
+
if (hasDom) {
|
9
|
+
document.body.removeChild(hasDom)
|
10
|
+
}
|
11
|
+
let divDom = document.createElement("div");
|
12
|
+
divDom.className = 'zetrix_wallet_connect_wrapper'
|
13
|
+
divDom.setAttribute('id', 'zetrixWalletConnectQr')
|
14
|
+
divDom.innerHTML = "<div class='zetrix_wallet_connect_box'>" +
|
15
|
+
"<h2>Scan with the Zetrix Wallet app</h2>" +
|
16
|
+
"<img src='"+code+"' width='100%' />" +
|
17
|
+
"</div>"
|
18
|
+
let bodyDom = document.body
|
19
|
+
bodyDom.insertBefore(divDom, bodyDom.lastChild);
|
20
|
+
|
21
|
+
divDom.onclick = function(){
|
22
|
+
let hasDom = document.getElementById('zetrixWalletConnectQr')
|
23
|
+
document.body.removeChild(hasDom)
|
24
|
+
if (callBack) {
|
25
|
+
callBack(resultInfo.closeNotice)
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
export const loadQrCss = () => {
|
31
|
+
let style = document.createElement('style');
|
32
|
+
style.type = 'text/css';
|
33
|
+
style.innerHTML=".zetrix_wallet_connect_wrapper{\n" +
|
34
|
+
" width: 100%;\n" +
|
35
|
+
" height:100%;\n" +
|
36
|
+
" background: rgba(0,0,0,.8);\n" +
|
37
|
+
" position: fixed;\n" +
|
38
|
+
" left:0;\n" +
|
39
|
+
" top:0;\n" +
|
40
|
+
" z-index: 9999999999;\n" +
|
41
|
+
" user-select: none;\n" +
|
42
|
+
" transition: .2s ease-in-out;\n" +
|
43
|
+
"}\n" +
|
44
|
+
".zetrix_wallet_connect_box{\n" +
|
45
|
+
" width: 400px;\n" +
|
46
|
+
" padding: 20px;\n" +
|
47
|
+
" border-radius: 20px;\n" +
|
48
|
+
" background-color: #FFF;\n" +
|
49
|
+
" position: absolute;\n" +
|
50
|
+
" left: 50%;\n" +
|
51
|
+
" top:50%;\n" +
|
52
|
+
" transform: translate(-50%, -50%);\n" +
|
53
|
+
"}\n" +
|
54
|
+
".zetrix_wallet_connect_box h2 {\n" +
|
55
|
+
" font-size: 18px;\n" +
|
56
|
+
" text-align: center;\n" +
|
57
|
+
" margin: 0 0 15px 0;\n" +
|
58
|
+
" color:#999;\n" +
|
59
|
+
"}";
|
60
|
+
document.getElementsByTagName('HEAD').item(0).appendChild(style);
|
61
|
+
}
|
62
|
+
|
63
|
+
export const closeQr = () => {
|
64
|
+
let hasDom = document.getElementById('zetrixWalletConnectQr')
|
65
|
+
if (hasDom) {
|
66
|
+
document.body.removeChild(hasDom)
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
const createQrcode = (data) => {
|
71
|
+
const canvas = qrcanvas({
|
72
|
+
data: data
|
73
|
+
})
|
74
|
+
return canvas.toDataURL("image/png")
|
75
|
+
}
|
package/lib/result.js
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
export const resultInfo = {
|
2
|
+
noUseSocket: {
|
3
|
+
code: 90001,
|
4
|
+
message: 'Your browser does not support the Websocket communication protocol'
|
5
|
+
},
|
6
|
+
noUseLocalStorage: {
|
7
|
+
code: 90002,
|
8
|
+
message: 'Your browser does not support window.localStorage'
|
9
|
+
},
|
10
|
+
nonceNotActivated: {
|
11
|
+
code: 10010,
|
12
|
+
message: 'Account not activated'
|
13
|
+
},
|
14
|
+
nonceNotAuth: {
|
15
|
+
code: 10011,
|
16
|
+
message: 'Unexecuted authorization'
|
17
|
+
},
|
18
|
+
closeNotice: {
|
19
|
+
code: 1,
|
20
|
+
message: 'Cancelled'
|
21
|
+
},
|
22
|
+
h5ParametersMastObject: {
|
23
|
+
code: 2000,
|
24
|
+
message: 'Parameter format error, parameter format is object'
|
25
|
+
},
|
26
|
+
h5RequiredParametersError: {
|
27
|
+
code: 2001,
|
28
|
+
message: 'Missing required parameters'
|
29
|
+
},
|
30
|
+
}
|
31
|
+
|
32
|
+
export const typeInfo = {
|
33
|
+
storageName: 'zetrixWalletConnect',
|
34
|
+
mobileSource: 'mobile'
|
35
|
+
}
|
36
|
+
|
37
|
+
export const hostParam = {
|
38
|
+
android: 'zetrixnew://zetrix.com/app/flutter',
|
39
|
+
ios: 'zetrixnew://zetrix.com/app/flutter',
|
40
|
+
androidpixa: 'pixa://pixa.com/app/flutter',
|
41
|
+
iospixa: 'pixa://pixa.com/app/flutter'
|
42
|
+
}
|
43
|
+
|
44
|
+
export const sdkType = {
|
45
|
+
bind: 'bind',
|
46
|
+
auth: 'auth',
|
47
|
+
signMessage: 'signMessage',
|
48
|
+
signBlob: 'signBlob',
|
49
|
+
sendTransaction: 'sendTransaction',
|
50
|
+
setQr: 'put',
|
51
|
+
bindAndSignMessage: 'bindAndSignMessage'
|
52
|
+
}
|
53
|
+
|
54
|
+
export const errorCode = -1
|
55
|
+
|
56
|
+
export const systemErrorInfo = {
|
57
|
+
abnormalOperation: 'Abnormal operation',
|
58
|
+
iosSupportError: 'Sorry, IOS is not currently supported'
|
59
|
+
}
|
package/lib/socket.js
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
'use strict'
|
2
|
+
import {compatibleSocket, compatibleStorage, getAddress, getSessionId} from './util'
|
3
|
+
import {sdkType} from './result'
|
4
|
+
class WalletSocket {
|
5
|
+
constructor (url) {
|
6
|
+
this.url = url
|
7
|
+
this._websocket = null
|
8
|
+
|
9
|
+
this.closeConfig = {
|
10
|
+
resolve: null,
|
11
|
+
closing: false
|
12
|
+
}
|
13
|
+
|
14
|
+
this.promisePool = {}
|
15
|
+
}
|
16
|
+
connect () {
|
17
|
+
return new Promise((resolve, reject) => {
|
18
|
+
|
19
|
+
let socketResult = compatibleSocket()
|
20
|
+
if (socketResult.code !== 0) {
|
21
|
+
reject(socketResult)
|
22
|
+
}
|
23
|
+
|
24
|
+
let locResult = compatibleStorage()
|
25
|
+
if (locResult.code !== 0) {
|
26
|
+
reject(locResult)
|
27
|
+
}
|
28
|
+
|
29
|
+
|
30
|
+
if (typeof(WebSocket) == "function") {
|
31
|
+
this._websocket = new WebSocket(this.url)
|
32
|
+
this._websocket.onopen = () => {
|
33
|
+
console.log('ws open successfully')
|
34
|
+
this.closeConfig.closing = false
|
35
|
+
|
36
|
+
let authResult = this.callbackAuthInfo()
|
37
|
+
resolve(authResult)
|
38
|
+
|
39
|
+
this.afterOpenToBind()
|
40
|
+
}
|
41
|
+
this._websocket.onerror = (e) => {
|
42
|
+
reject(e)
|
43
|
+
}
|
44
|
+
this._websocket.onclose = () => {
|
45
|
+
console.warn('Websocket closed')
|
46
|
+
if (!this.closeConfig.closing) {
|
47
|
+
console.warn('Websocket reconnect')
|
48
|
+
this.connect().then()
|
49
|
+
}
|
50
|
+
|
51
|
+
}
|
52
|
+
this._websocket.onmessage = (evt) => {
|
53
|
+
|
54
|
+
try {
|
55
|
+
let resp = JSON.parse(evt.data)
|
56
|
+
if (!resp.type) {
|
57
|
+
return false
|
58
|
+
}
|
59
|
+
|
60
|
+
let type = resp.type.split('_')[1]
|
61
|
+
if (type === 'auth') {
|
62
|
+
type = 'bind'
|
63
|
+
}
|
64
|
+
type = 'H5_' + type
|
65
|
+
|
66
|
+
let req = this.promisePool[type]
|
67
|
+
|
68
|
+
if (!req) {
|
69
|
+
return
|
70
|
+
}
|
71
|
+
|
72
|
+
req.resolve(resp)
|
73
|
+
|
74
|
+
} catch (e) {
|
75
|
+
console.error(e)
|
76
|
+
}
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
})
|
81
|
+
}
|
82
|
+
// changeCallBackData (resp) {
|
83
|
+
// if (resp.data.isAuth === 1) {
|
84
|
+
// resp.type = 'H5_' + sdkType.bind
|
85
|
+
// return resp
|
86
|
+
// } else {
|
87
|
+
// return resp
|
88
|
+
// }
|
89
|
+
// }
|
90
|
+
send (method, data) {
|
91
|
+
return new Promise((resolve, reject) => {
|
92
|
+
this.promisePool[method] = {
|
93
|
+
resolve,
|
94
|
+
reject
|
95
|
+
}
|
96
|
+
data.type = method
|
97
|
+
let req = JSON.stringify(data)
|
98
|
+
this._websocket.send(req)
|
99
|
+
})
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
disconnect () {
|
105
|
+
this.closeConfig.closing = true
|
106
|
+
this._websocket.close()
|
107
|
+
}
|
108
|
+
h5Bind (data) {
|
109
|
+
return this.send('H5_'+ sdkType.bind, data)
|
110
|
+
}
|
111
|
+
afterOpenToBind () {
|
112
|
+
if (getSessionId()) {
|
113
|
+
let sendBind = {
|
114
|
+
type: 'H5_bind',
|
115
|
+
sessionId: getSessionId()
|
116
|
+
}
|
117
|
+
this._websocket.send(JSON.stringify(sendBind))
|
118
|
+
}
|
119
|
+
}
|
120
|
+
callbackAuthInfo () {
|
121
|
+
let addressStr = getAddress()
|
122
|
+
let sessionIdStr = getSessionId()
|
123
|
+
if (addressStr && sessionIdStr){
|
124
|
+
return {
|
125
|
+
code: 0,
|
126
|
+
data: {
|
127
|
+
address: addressStr
|
128
|
+
}
|
129
|
+
}
|
130
|
+
} else {
|
131
|
+
return {
|
132
|
+
code: 0,
|
133
|
+
data: {}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
export default WalletSocket
|
package/lib/util.js
ADDED
@@ -0,0 +1,221 @@
|
|
1
|
+
import {resultInfo, typeInfo} from './result'
|
2
|
+
import MobileDetect from './mobileDetect'
|
3
|
+
|
4
|
+
export const createSessionId = () => {
|
5
|
+
let s = [];
|
6
|
+
let hexDigits = "0123456789abcdef";
|
7
|
+
for (let i = 0; i < 36; i++) {
|
8
|
+
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
|
9
|
+
}
|
10
|
+
s[14] = "4";
|
11
|
+
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
|
12
|
+
s[8] = s[13] = s[18] = s[23] = "-";
|
13
|
+
|
14
|
+
let uuid = s.join("");
|
15
|
+
return uuid;
|
16
|
+
}
|
17
|
+
|
18
|
+
export const compatibleSocket = () => {
|
19
|
+
if( typeof(WebSocket) != "function" ) {
|
20
|
+
console.error(resultInfo.noUseSocket.message)
|
21
|
+
return {
|
22
|
+
code: resultInfo.noUseSocket.code,
|
23
|
+
message: resultInfo.noUseSocket.message
|
24
|
+
}
|
25
|
+
} else {
|
26
|
+
return {
|
27
|
+
code: 0
|
28
|
+
}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
export const compatibleStorage = () => {
|
33
|
+
if(typeof window.localStorage == 'undefined') {
|
34
|
+
console.error(resultInfo.noUseLocalStorage.message)
|
35
|
+
return {
|
36
|
+
code: resultInfo.noUseLocalStorage.code,
|
37
|
+
message: resultInfo.noUseLocalStorage.message
|
38
|
+
}
|
39
|
+
} else {
|
40
|
+
return {
|
41
|
+
code: 0
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
export const getWindowUrl = () => {
|
47
|
+
return window.location.protocol + '//' + window.location.host
|
48
|
+
}
|
49
|
+
|
50
|
+
export const getWindowICO = () => {
|
51
|
+
let ico = ''
|
52
|
+
try {
|
53
|
+
ico = document.querySelectorAll("link[rel*='icon']")[0].href || ''
|
54
|
+
} catch (e) {
|
55
|
+
ico = ''
|
56
|
+
}
|
57
|
+
return ico
|
58
|
+
}
|
59
|
+
|
60
|
+
export const useDevice = () => {
|
61
|
+
let u = navigator.userAgent;
|
62
|
+
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
|
63
|
+
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
|
64
|
+
|
65
|
+
if(isAndroid) {
|
66
|
+
return 'android'
|
67
|
+
} else if(isiOS) {
|
68
|
+
return 'ios'
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
export const useClientName = () => {
|
73
|
+
const md = new MobileDetect(window.navigator.userAgent);
|
74
|
+
const client = md.userAgent()
|
75
|
+
return client || ''
|
76
|
+
}
|
77
|
+
|
78
|
+
export const warnIncorrectLog = (name) => {
|
79
|
+
return `The '${name}' argument is incorrect`
|
80
|
+
}
|
81
|
+
|
82
|
+
export const getSessionId = () => {
|
83
|
+
let storageData = window.localStorage.getItem(typeInfo.storageName)
|
84
|
+
if (!storageData) {
|
85
|
+
return false
|
86
|
+
}
|
87
|
+
storageData = JSON.parse(storageData)
|
88
|
+
if (storageData.sessionId) {
|
89
|
+
return storageData.sessionId
|
90
|
+
} else {
|
91
|
+
return false
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
export const getAddress = () => {
|
96
|
+
let storageData = window.localStorage.getItem(typeInfo.storageName)
|
97
|
+
if (!storageData) {
|
98
|
+
return false
|
99
|
+
}
|
100
|
+
storageData = JSON.parse(storageData)
|
101
|
+
if (storageData.address) {
|
102
|
+
return storageData.address
|
103
|
+
} else {
|
104
|
+
return false
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
export const setAuthData = (sessionId, address) => {
|
109
|
+
let storageData = window.localStorage.getItem(typeInfo.storageName)
|
110
|
+
if (!storageData) {
|
111
|
+
let setData = { sessionId: sessionId, address: address }
|
112
|
+
window.localStorage.setItem(typeInfo.storageName, JSON.stringify(setData))
|
113
|
+
} else {
|
114
|
+
storageData = JSON.parse(storageData)
|
115
|
+
storageData.sessionId = sessionId
|
116
|
+
storageData.address = address
|
117
|
+
window.localStorage.setItem(typeInfo.storageName, JSON.stringify(storageData))
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
export const disconnectRemoveStorage = () => {
|
122
|
+
window.localStorage.removeItem(typeInfo.storageName)
|
123
|
+
}
|
124
|
+
|
125
|
+
export const isMobile = () => {
|
126
|
+
if ('ontouchstart' in window) {
|
127
|
+
return true
|
128
|
+
} else {
|
129
|
+
return false
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
export const setLocalStorage = (name, value) => {
|
134
|
+
window.localStorage.setItem(name, value)
|
135
|
+
}
|
136
|
+
|
137
|
+
export const getLocalStorage = (name) => {
|
138
|
+
let data = window.localStorage.getItem(name)
|
139
|
+
if (!data) {
|
140
|
+
return ''
|
141
|
+
}
|
142
|
+
return data
|
143
|
+
}
|
144
|
+
|
145
|
+
export const isJSON = (str) => {
|
146
|
+
if (typeof str == 'string') {
|
147
|
+
try {
|
148
|
+
var obj=JSON.parse(str);
|
149
|
+
if(typeof obj == 'object' && obj ){
|
150
|
+
return true;
|
151
|
+
}else{
|
152
|
+
return false;
|
153
|
+
}
|
154
|
+
} catch(e) {
|
155
|
+
console.log('error:'+str+'!!!'+e);
|
156
|
+
return false;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
export const openInWebview = () => {
|
162
|
+
var useragent = navigator.userAgent;
|
163
|
+
var rules = ['WebView','(iPhone|iPod|iPad)(?!.*Safari/)','Android.*(wv|.0.0.0)'];
|
164
|
+
var regex = new RegExp(`(${rules.join('|')})`, 'ig');
|
165
|
+
return Boolean(useragent.match(regex));
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
export const fixedRequestParameters = (oParams={}) => {
|
170
|
+
let mustParameters = {
|
171
|
+
data: {
|
172
|
+
source: {
|
173
|
+
isBackData: true,
|
174
|
+
sourceType: 'triggerApp'
|
175
|
+
},
|
176
|
+
payload: {
|
177
|
+
...oParams
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}
|
181
|
+
return JSON.stringify(mustParameters)
|
182
|
+
}
|
183
|
+
|
184
|
+
export const resReturnedData = (res) => {
|
185
|
+
let parseRes = res
|
186
|
+
console.log(parseRes, 'parseRes1');
|
187
|
+
let oParams = {
|
188
|
+
code: parseRes.code,
|
189
|
+
data: parseRes.data.payload,
|
190
|
+
message: parseRes.message
|
191
|
+
}
|
192
|
+
if (parseRes.data.source.chainId) {
|
193
|
+
oParams.data.chainId = parseRes.data.source.chainId
|
194
|
+
}
|
195
|
+
return oParams
|
196
|
+
}
|
197
|
+
|
198
|
+
export const isAnObject = (obj) => {
|
199
|
+
if (obj instanceof Object) {
|
200
|
+
return true
|
201
|
+
} else {
|
202
|
+
return false
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
export const getUrlParameter = (loca, type) => {
|
207
|
+
let search = () => (location.href.indexOf('?') === -1 ? '' : location.href.slice(location.href.indexOf('?')));
|
208
|
+
let url = loca || location.search || search();
|
209
|
+
if (type === 'string') {
|
210
|
+
return url;
|
211
|
+
}
|
212
|
+
let theRequest = new Object();
|
213
|
+
if (url.indexOf('?') != -1) {
|
214
|
+
let str = url.split('?')[1];
|
215
|
+
let strs = str.split('&');
|
216
|
+
for (var i = 0; i < strs.length; i++) {
|
217
|
+
theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
|
218
|
+
}
|
219
|
+
}
|
220
|
+
return theRequest;
|
221
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"name": "zetrix-connect-wallet-sdk-test",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "A library for mobile wallet and non-Chrome browsers to connect with the Zetrix blockchain.",
|
5
|
+
"main": "lib/index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"author": "",
|
10
|
+
"license": "ISC",
|
11
|
+
"dependencies": {
|
12
|
+
"qrcanvas": "^3.1.2"
|
13
|
+
}
|
14
|
+
}
|