-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (81 loc) · 3.38 KB
/
index.js
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import QueryString from 'query-string'
import crypto from 'crypto-js'
const DEFAULT_PASSWORD = 'CHENDINH'
const encryptBackupFileContent = (value, pass = DEFAULT_PASSWORD) => {
try {
return crypto.AES.encrypt(value.toString(), pass.toString()).toString()
} catch (error) {
console.log('error encryptBackupFileContent', error)
return false
}
}
const decryptBackupFileContent = (value, pass = DEFAULT_PASSWORD) => {
try {
return crypto.AES.decrypt(value.toString(), pass.toString()).toString(crypto.enc.Utf8)
} catch (error) {
return false
}
}
export const encryptSessionObject = (objectSession, addressUser, chainId, peerId, password = DEFAULT_PASSWORD) => {
// objectSession is from connector.session by wallet connect v1
// chainId's type is Number (ex: 1)
// peerId's type is String (this is clientId of dapp or wallet. if you encode from wallet, your peerId must be clientId of dapp)
// addressUser's type is String (0x...123)
try {
const objSession = objectSession
objSession.connected = true
objSession.address = addressUser
objSession.chainId = chainId
objSession.peerId = peerId
const encodeStringSession = encryptBackupFileContent(QueryString.stringify(objSession), password)
return encodeStringSession
} catch (e) {
return false
}
}
export const decryptSessionObject = (encodeStringSession, clientMeta, peerMeta, password = DEFAULT_PASSWORD) => {
// encodeStringSession is encoded
// clientMeta's type is Object (this is clientMeta of dapp or wallet. if you encode from wallet, your clientMeta must be peerMeta of dapp)
// peerMeta's type is Object (this is peerMeta of dapp or wallet. if you encode from wallet, your peerMeta must be clientMeta of dapp)
const urlObject = encodeStringSession.split('?')
const queryKey = urlObject[1].split('=')
const queryKeyString = queryKey[1]
if (!queryKeyString) {
return false
} else {
const decodeURIString = decodeURI(queryKeyString)
const decodeAutoWCKey = decryptBackupFileContent(decodeURIString, password)
const objSessionTemp = QueryString.parseUrl(`?${decodeAutoWCKey}`).query
return {
accounts: [objSessionTemp.address],
bridge: objSessionTemp.bridge,
chainId: Number(objSessionTemp.chainId),
clientId: objSessionTemp.clientId,
clientMeta: clientMeta || {
name: 'AppNAME clientMeta',
url: 'https://yourdomaindapp.com',
icons: ['https://ipfs.appicon.png'],
description: 'good morning'
},
connected: true,
handshakeId: objSessionTemp.handshakeId,
handshakeTopic: objSessionTemp.handshakeTopic,
key: objSessionTemp.key,
peerId: objSessionTemp.peerId,
peerMeta: peerMeta || {
name: 'AppNAME peerMeta',
url: 'https://yourdomaindapp.com',
icons: ['https://ipfs.appicon.png'],
description: 'good morning'
}
}
}
}
export const urlWithEncodeSession = (urlDapp, encodeStringSession) => {
if (!encodeStringSession || !urlDapp || (urlDapp && urlDapp.length === 0) || (encodeStringSession && encodeStringSession.length === 0)) {
return false
} else {
const customUrl = encodeURI(`${urlDapp}?autoWCKey=${encodeStringSession}`)
return customUrl
}
}