@com-chain/jsc3l 2.0.1-rc.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.
Files changed (84) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +45 -0
  3. package/build/bcRead.d.ts +15 -0
  4. package/build/bcRead.js +123 -0
  5. package/build/bcRead.js.map +1 -0
  6. package/build/bcTransaction.d.ts +10 -0
  7. package/build/bcTransaction.js +135 -0
  8. package/build/bcTransaction.js.map +1 -0
  9. package/build/blockies.d.ts +1 -0
  10. package/build/blockies.js +91 -0
  11. package/build/blockies.js.map +1 -0
  12. package/build/config.d.ts +6 -0
  13. package/build/config.js +25 -0
  14. package/build/config.js.map +1 -0
  15. package/build/connection.d.ts +58 -0
  16. package/build/connection.js +204 -0
  17. package/build/connection.js.map +1 -0
  18. package/build/customization.d.ts +37 -0
  19. package/build/customization.js +129 -0
  20. package/build/customization.js.map +1 -0
  21. package/build/ethereum/cipher.d.ts +3 -0
  22. package/build/ethereum/cipher.js +94 -0
  23. package/build/ethereum/cipher.js.map +1 -0
  24. package/build/ethereum/ethFuncs.d.ts +12 -0
  25. package/build/ethereum/ethFuncs.js +70 -0
  26. package/build/ethereum/ethFuncs.js.map +1 -0
  27. package/build/ethereum/etherUnits.d.ts +5 -0
  28. package/build/ethereum/etherUnits.js +60 -0
  29. package/build/ethereum/etherUnits.js.map +1 -0
  30. package/build/ethereum/myetherwallet.d.ts +47 -0
  31. package/build/ethereum/myetherwallet.js +294 -0
  32. package/build/ethereum/myetherwallet.js.map +1 -0
  33. package/build/ethereum/uiFuncs.d.ts +3 -0
  34. package/build/ethereum/uiFuncs.js +51 -0
  35. package/build/ethereum/uiFuncs.js.map +1 -0
  36. package/build/index.d.ts +111 -0
  37. package/build/index.js +310 -0
  38. package/build/index.js.map +1 -0
  39. package/build/memo.d.ts +6 -0
  40. package/build/memo.js +24 -0
  41. package/build/memo.js.map +1 -0
  42. package/build/qr.d.ts +8 -0
  43. package/build/qr.js +35 -0
  44. package/build/qr.js.map +1 -0
  45. package/build/rest/ajaxReq.d.ts +31 -0
  46. package/build/rest/ajaxReq.js +127 -0
  47. package/build/rest/ajaxReq.js.map +1 -0
  48. package/build/rest/endpoint.d.ts +18 -0
  49. package/build/rest/endpoint.js +26 -0
  50. package/build/rest/endpoint.js.map +1 -0
  51. package/build/rest/http.d.ts +10 -0
  52. package/build/rest/http.js +60 -0
  53. package/build/rest/http.js.map +1 -0
  54. package/build/rest/serializer.d.ts +1 -0
  55. package/build/rest/serializer.js +94 -0
  56. package/build/rest/serializer.js.map +1 -0
  57. package/build/type.d.ts +24 -0
  58. package/build/type.js +9 -0
  59. package/build/type.js.map +1 -0
  60. package/build/wallet.d.ts +35 -0
  61. package/build/wallet.js +162 -0
  62. package/build/wallet.js.map +1 -0
  63. package/package.json +41 -0
  64. package/src/bcRead.ts +184 -0
  65. package/src/bcTransaction.ts +157 -0
  66. package/src/blockies.ts +113 -0
  67. package/src/config.ts +33 -0
  68. package/src/connection.ts +243 -0
  69. package/src/customization.ts +156 -0
  70. package/src/ethereum/cipher.ts +118 -0
  71. package/src/ethereum/ethFuncs.ts +73 -0
  72. package/src/ethereum/etherUnits.ts +66 -0
  73. package/src/ethereum/myetherwallet.ts +354 -0
  74. package/src/ethereum/uiFuncs.ts +55 -0
  75. package/src/index.ts +366 -0
  76. package/src/memo.ts +37 -0
  77. package/src/qr.ts +34 -0
  78. package/src/rest/ajaxReq.ts +160 -0
  79. package/src/rest/endpoint.ts +31 -0
  80. package/src/rest/http.ts +69 -0
  81. package/src/rest/serializer.ts +99 -0
  82. package/src/type.ts +37 -0
  83. package/src/wallet.ts +200 -0
  84. package/tsconfig.json +31 -0
@@ -0,0 +1,99 @@
1
+
2
+ function isScope (obj) {
3
+ return obj && obj.$evalAsync && obj.$watch
4
+ }
5
+
6
+ function isWindow (obj) {
7
+ return obj && obj.window === obj
8
+ }
9
+
10
+ function toJsonReplacer (key, value) {
11
+ let val = value
12
+ if (typeof key === 'string' &&
13
+ key.charAt(0) === '$' &&
14
+ key.charAt(1) === '$') {
15
+ val = undefined
16
+ } else if (isWindow(value)) {
17
+ val = '$WINDOW'
18
+ } else if (value && window.document === value) {
19
+ val = '$DOCUMENT'
20
+ } else if (isScope(value)) {
21
+ val = '$SCOPE'
22
+ }
23
+ return val
24
+ }
25
+
26
+ function isNumber (arg) {
27
+ return typeof arg === 'number'
28
+ }
29
+
30
+ function toJson (obj, pretty?) {
31
+ if (isUndefined(obj)) { return undefined }
32
+ if (!isNumber(pretty)) {
33
+ pretty = pretty ? 2 : null
34
+ }
35
+ return JSON.stringify(obj, toJsonReplacer, pretty)
36
+ }
37
+
38
+ function isDate (value) {
39
+ return toString.call(value) === '[object Date]'
40
+ }
41
+
42
+ function isObject (value) {
43
+ return value !== null && typeof value === 'object'
44
+ }
45
+
46
+ function serializeValue (v) {
47
+ if (isObject(v)) {
48
+ return isDate(v) ? v.toISOString() : toJson(v)
49
+ }
50
+ return v
51
+ }
52
+
53
+ function encodeUriQuery (val, pctEncodeSpaces?) {
54
+ return encodeURIComponent(val)
55
+ .replace(/%40/gi, '@')
56
+ .replace(/%3A/gi, ':')
57
+ .replace(/%24/g, '$')
58
+ .replace(/%2C/gi, ',')
59
+ .replace(/%3B/gi, ';')
60
+ .replace(/%20/g, pctEncodeSpaces ? '%20' : '+')
61
+ }
62
+
63
+ function isUndefined (value) {
64
+ return typeof value === 'undefined'
65
+ }
66
+
67
+ function forEachSorted (obj, iterator, context?) {
68
+ const keys = Object.keys(obj).sort()
69
+ for (let i = 0; i < keys.length; i++) {
70
+ iterator.call(context, obj[keys[i]], keys[i])
71
+ }
72
+ return keys
73
+ }
74
+
75
+ export default function postSerializer (params) {
76
+ if (!params) return ''
77
+
78
+ const parts = []
79
+ serialize(params, '', true)
80
+ return parts.join('&')
81
+
82
+ function serialize (toSerialize, prefix, topLevel?) {
83
+ if (toSerialize === null || isUndefined(toSerialize)) { return }
84
+ if (Array.isArray(toSerialize)) {
85
+ toSerialize.forEach(function (value, index) {
86
+ serialize(value, prefix + '[' + (isObject(value) ? index : '') + ']')
87
+ })
88
+ } else if (isObject(toSerialize) && !isDate(toSerialize)) {
89
+ forEachSorted(toSerialize, function (value, key) {
90
+ serialize(value,
91
+ prefix + (topLevel ? '' : '[') + key + (topLevel ? '' : ']'))
92
+ })
93
+ } else {
94
+ parts.push(
95
+ encodeUriQuery(prefix) + '=' +
96
+ encodeUriQuery(serializeValue(toSerialize)))
97
+ }
98
+ }
99
+ }
package/src/type.ts ADDED
@@ -0,0 +1,37 @@
1
+ export const httpMethods = [
2
+ 'GET',
3
+ 'POST',
4
+ 'PUT',
5
+ 'DELETE',
6
+ 'PATCH',
7
+ 'HEAD'
8
+ ] as const
9
+
10
+ export type httpMethod = typeof httpMethods[number]
11
+
12
+ export type coreHttpOpts = {
13
+ protocol: string
14
+ host: string
15
+ path: string
16
+ method: httpMethod
17
+ headers?: {}
18
+ port?: number
19
+ data?: any // TODO: inconsistency with lokapi here
20
+ timeout?: number
21
+ }
22
+
23
+ export type HttpRequest = (opts: coreHttpOpts) => Object
24
+
25
+ export interface IPersistentStore {
26
+ get(key: string, defaultValue?: string): string
27
+ set(key: string, value: string): void
28
+ del(key: string): void
29
+ }
30
+
31
+ export type UrlParts = {
32
+ protocol: string
33
+ host: string
34
+ port: number
35
+ path: string
36
+ }
37
+
package/src/wallet.ts ADDED
@@ -0,0 +1,200 @@
1
+ import ethUtil from 'ethereumjs-util'
2
+
3
+ import AjaxReq from './rest/ajaxReq'
4
+ import { shortenAddress, cipherMsg, decipherMsg } from './ethereum/cipher'
5
+ import Wallet from './ethereum/myetherwallet'
6
+
7
+
8
+ export default abstract class MessagingWalletAbstract extends Wallet {
9
+
10
+ abstract ajaxReq: AjaxReq // need to be provided
11
+ abstract currencyName: string
12
+ abstract unlockUrl: string
13
+
14
+ message_key: null | {pub: string, priv: string} = null
15
+
16
+ public static async createWallet (this: { new(): MessagingWalletAbstract }) {
17
+ const wallet = new this()
18
+ return wallet.ensureWalletMessageKey()
19
+ }
20
+
21
+ private publishMessageKey () {
22
+ const dataStr = JSON.stringify({
23
+ address: this.getAddressString(),
24
+ public_message_key: this.message_key.pub,
25
+ private_message_key: this.message_key.priv,
26
+ })
27
+
28
+ return this.ajaxReq.publishMessageKey(dataStr, this.signMessage(dataStr))
29
+ }
30
+
31
+ public async publishReqMessages (addTo, message) {
32
+ const addFrom = this.getAddressString()
33
+ const pubKey = async (add) =>
34
+ (await this.ajaxReq.getMessageKey(add, false)).public_message_key
35
+
36
+ const fromMsgKey = await pubKey(addFrom)
37
+ const toMsgKey = await pubKey(addTo)
38
+
39
+ const dataStr = JSON.stringify({
40
+ add_req: addFrom,
41
+ add_cli: addTo,
42
+ ref_req: fromMsgKey ? cipherMsg(fromMsgKey, message) : '',
43
+ ref_cli: toMsgKey ? cipherMsg(toMsgKey, message) : '',
44
+ })
45
+ return this.ajaxReq.publishReqMessages(dataStr, this.signMessage(dataStr))
46
+ }
47
+
48
+ async ensureWalletMessageKey () {
49
+ const remoteKey = await this.ajaxReq.getMessageKey(
50
+ this.getAddressString(), true)
51
+ const walletMessageKey = this?.message_key
52
+ if (remoteKey.public_message_key !== undefined) {
53
+ this.message_key = {
54
+ pub: remoteKey.public_message_key,
55
+ priv: remoteKey.private_message_key,
56
+ }
57
+ if (walletMessageKey?.pub !== remoteKey.public_message_key) {
58
+ return 'REPLACED_WITH_REMOTE'
59
+ }
60
+ return
61
+ }
62
+
63
+ if (!walletMessageKey?.pub || !walletMessageKey?.priv) {
64
+ this.message_key = this.newMessageKey()
65
+ this.publishMessageKey()
66
+ return 'CREATED_NEW'
67
+ }
68
+ }
69
+
70
+ public async getReqMessage (otherAdd, myMessageKey, didISentThisMsg) {
71
+ const myAdd = this.getAddressString()
72
+ const addFrom = didISentThisMsg ? myAdd : otherAdd
73
+ const addTo = !didISentThisMsg ? myAdd : otherAdd
74
+ const data = await this.ajaxReq.getReqMessages(addFrom, addTo)
75
+ if (!data) return ''
76
+
77
+ let crypted = ''
78
+ if (didISentThisMsg && data.ref_from) {
79
+ crypted = data.ref_from
80
+ } else if (!didISentThisMsg && data.ref_to) {
81
+ crypted = data.ref_to
82
+ } else {
83
+ return ''
84
+ }
85
+
86
+ try {
87
+ return decipherMsg(myMessageKey, crypted)
88
+ } catch (e) {
89
+ return ''
90
+ }
91
+ }
92
+
93
+ private newMessageKey () {
94
+ const newKey = Wallet.generate(false)
95
+ const mPub = newKey.getPublicKeyString()
96
+ const mPriv = newKey.getPrivateKeyString()
97
+ return { pub: mPub, priv: cipherMsg(this.getPublicKey(), mPriv) }
98
+ }
99
+
100
+ public messageKeysFromCrypted (cipheredKey) {
101
+ // XXXvlab: here we are converting to hex and de-converting in decipherMsg...
102
+ return shortenAddress(decipherMsg(this.getPrivateKeyString(), cipheredKey))
103
+ }
104
+
105
+ public messageKeysFromWallet () {
106
+ return this.messageKeysFromCrypted(this.message_key.priv)
107
+ }
108
+
109
+
110
+ //
111
+ // Using currencyName
112
+ //
113
+
114
+ public encryptWallet (password) {
115
+ return this.toV3(password, {
116
+ kdf: 'scrypt',
117
+ n: 1024,
118
+ server_name: this.currencyName,
119
+ message_key: this.message_key
120
+ })
121
+ }
122
+
123
+ public enrollAddress (codeId, token) {
124
+ return this.ajaxReq.enrollAddress(
125
+ codeId, this.getAddressString(),
126
+ this.currencyName, token)
127
+ }
128
+
129
+ public requestUnlock () {
130
+ return this.ajaxReq.requestUnlock(
131
+ this.getAddressString(),
132
+ this.unlockUrl)
133
+ }
134
+
135
+ public static getWalletFromPrivKeyFile (jsonStr, password) {
136
+ const jsonArr = JSON.parse(jsonStr)
137
+ if (jsonArr.encseed != null) return this.fromEthSale(jsonStr, password)
138
+ else if (jsonArr.Crypto != null ||
139
+ jsonArr.crypto != null) {
140
+ return this.fromV3(jsonStr, password, true)
141
+ } else if (jsonArr.hash != null) {
142
+ return this.fromMyEtherWallet(jsonStr, password)
143
+ } else if (jsonArr.publisher === 'MyEtherWallet') {
144
+ return this.fromMyEtherWalletV2(jsonStr)
145
+ } else {
146
+ throw new Error("Sorry! We don't recognize this type of wallet file.")
147
+ }
148
+ }
149
+
150
+
151
+ //
152
+ // QR Codes requires EthUtils
153
+ //
154
+
155
+ public makeSignedQRWithPubKey (objContent, pubKey) {
156
+ objContent.message_key = cipherMsg(
157
+ pubKey, this.messageKeysFromWallet()
158
+ )
159
+ objContent.address = this.getAddressString()
160
+ return this.makeSignedQR(objContent)
161
+ }
162
+
163
+ public makeSignedQR (obj) {
164
+
165
+ // Values expected:
166
+ const {
167
+ server, destinary, begin, end,
168
+ viewbalance, viewoldtran, pub_key
169
+ } = obj
170
+ const formatDate = (date) =>
171
+ `${begin.getFullYear()}/${begin.getMonth()}/${begin.getDate()}`
172
+ const objContent = Object.assign(obj, {
173
+ address: this.getAddressString(),
174
+ begin: formatDate(begin),
175
+ end: formatDate(end)
176
+ })
177
+
178
+ const hash = ethUtil.sha3(JSON.stringify(objContent))
179
+ const signature = ethUtil.ecsign(hash, this.privKey)
180
+ return {
181
+ signature,
182
+ qrContent: JSON.stringify({
183
+ data: objContent,
184
+ signature: {
185
+ v: signature.v,
186
+ r: '0x' + signature.r.toString('hex'),
187
+ s: '0x' + signature.s.toString('hex')
188
+ }
189
+ })
190
+ }
191
+ }
192
+
193
+ }
194
+
195
+ // // TODO: What to do with this validateEnrollment
196
+ // public static validateEnrollment (codeId, signature) {
197
+ // return this.ajaxReq.validateEnrollmentLetter(
198
+ // codeId, this.currencyName, signature)
199
+ // }
200
+
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2017",
4
+ "module": "esnext",
5
+ "moduleResolution": "node",
6
+ "declaration": true,
7
+ "removeComments": false,
8
+ "emitDecoratorMetadata": true,
9
+ "experimentalDecorators": true,
10
+ "sourceMap": true,
11
+ "skipLibCheck": true,
12
+ "allowUnreachableCode": false,
13
+ "allowUnusedLabels": false,
14
+ "noEmitHelpers": true,
15
+ "noEmitOnError": false,
16
+ "noImplicitAny": false,
17
+ "noImplicitReturns": false,
18
+ "noImplicitUseStrict": false,
19
+ "noFallthroughCasesInSwitch": true,
20
+ "allowSyntheticDefaultImports": true,
21
+ "esModuleInterop": true,
22
+ "lib": ["esnext", "dom"],
23
+ "baseUrl": ".",
24
+ "outDir": "build",
25
+ "paths": {
26
+ "*": ["src/types/*"]
27
+ }
28
+ },
29
+ "include": ["src/**/*", "references.d.ts"],
30
+ "exclude": ["node_modules", "platforms", "**/angular"]
31
+ }