@leofcoin/peernet 0.10.7 → 0.11.1

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 (76) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +49 -49
  3. package/coverage/lcov-report/block-navigation.js +8 -0
  4. package/coverage/lcov-report/codec-format-interface.js.html +224 -120
  5. package/coverage/lcov-report/dht-response.js.html +44 -39
  6. package/coverage/lcov-report/index.html +39 -64
  7. package/coverage/lcov-report/sorter.js +26 -0
  8. package/coverage/lcov.info +164 -424
  9. package/dist/browser/peernet.js +101788 -93018
  10. package/dist/commonjs/client-1a1f75e6.js +324 -0
  11. package/dist/commonjs/{codec-6367213c.js → codec-8c8c652f.js} +198 -188
  12. package/dist/commonjs/codec-format-interface.js +169 -152
  13. package/dist/commonjs/codec.js +4 -4
  14. package/dist/commonjs/dht-response.js +13 -13
  15. package/dist/commonjs/dht.js +24 -24
  16. package/dist/commonjs/hash.js +151 -141
  17. package/dist/commonjs/{http-a94c5a81.js → http-7bbac90a.js} +19 -15
  18. package/dist/commonjs/peernet-message.js +13 -13
  19. package/dist/commonjs/peernet.js +1901 -1794
  20. package/dist/commonjs/request.js +13 -13
  21. package/dist/commonjs/response.js +13 -13
  22. package/dist/module/peernet.js +2460 -2346
  23. package/index.html +4 -6
  24. package/package.json +22 -14
  25. package/rollup.config.js +33 -5
  26. package/rollup0.config.js +7 -0
  27. package/src/client.js +75 -75
  28. package/src/codec/codec-format-interface.js +172 -155
  29. package/src/codec/codec.js +124 -114
  30. package/src/codec/codecs.js +79 -79
  31. package/src/dht/dht.js +121 -121
  32. package/src/discovery/peer-discovery.js +75 -75
  33. package/src/handlers/message.js +52 -52
  34. package/src/hash/hash.js +155 -145
  35. package/src/http/client/http-client.js +44 -44
  36. package/src/messages/chat-message.js +14 -14
  37. package/src/messages/data-response.js +14 -14
  38. package/src/messages/data.js +18 -18
  39. package/src/messages/dht-response.js +14 -15
  40. package/src/messages/dht.js +25 -25
  41. package/src/messages/peer-response.js +14 -14
  42. package/src/messages/peer.js +14 -14
  43. package/src/messages/peernet-message.js +14 -14
  44. package/src/messages/ps.js +14 -14
  45. package/src/messages/request.js +14 -14
  46. package/src/messages/response.js +14 -14
  47. package/src/peer.js +67 -67
  48. package/src/peernet.js +612 -697
  49. package/src/proto/chat-message.proto.js +7 -7
  50. package/src/utils/utils.js +78 -78
  51. package/test/codec.js +3 -2
  52. package/test/messages.js +7 -4
  53. package/test.js +11 -4
  54. package/webpack.config.js +35 -0
  55. package/coverage/lcov-report/codec.js.html +0 -677
  56. package/coverage/lcov-report/hash.js.html +0 -551
  57. package/debug.log +0 -3
  58. package/dist/browser/peernet.js.tmp-browserify-14074318104595318069 +0 -0
  59. package/dist/browser/peernet.js.tmp-browserify-45407634493269122267 +0 -0
  60. package/dist/browser/peernet.js.tmp-browserify-53722389064799025427 +0 -0
  61. package/dist/browser/peernet.js.tmp-browserify-96323030449218949300 +0 -0
  62. package/dist/codec/codec-format-interface.js +0 -433
  63. package/dist/codec/codec.js +0 -199
  64. package/dist/commonjs/codec-73adfc0f.js +0 -205
  65. package/dist/commonjs/http-2c603501.js +0 -324
  66. package/dist/commonjs/http-42a6e555.js +0 -324
  67. package/dist/commonjs/http-43f4fafe.js +0 -324
  68. package/dist/commonjs/peernet-message-b6925673.js +0 -32
  69. package/dist/hash/hash.js +0 -340
  70. package/dist/messages/dht-response.js +0 -454
  71. package/dist/messages/dht.js +0 -453
  72. package/dist/messages/peernet.js +0 -456
  73. package/dist/module/http-273664bd.js +0 -317
  74. package/dist/module/http-8fe3c0d7.js +0 -317
  75. package/dist/module/http-c780c991.js +0 -317
  76. package/dist/module/http-f13e0d77.js +0 -317
package/index.html CHANGED
@@ -7,15 +7,13 @@
7
7
  <body>
8
8
  <script src="./dist/browser/peernet.js">
9
9
  </script>
10
-
10
+
11
11
  <script>
12
-
12
+
13
13
  (async () => {
14
14
  const peernet = await new Peernet()
15
- peernet.subscribe('socket-data', data => {
16
- console.log(data)
17
- })
15
+ peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: 100}))
18
16
  })()
19
17
  </script>
20
18
  </body>
21
- </html>
19
+ </html>
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.10.7",
3
+ "version": "0.11.1",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
+ "module": "dist/module/peernet.js",
7
+ "browser": "dist/browser/peernet.js",
6
8
  "scripts": {
9
+ "build": "webpack",
7
10
  "test": "node test/index.js",
8
11
  "server": "discovery-swarm-webrtc --port=4000",
9
12
  "demo": "jsproject --serve ./ --port 6868",
@@ -13,37 +16,36 @@
13
16
  "coveralls": "cat ./coverage/lcov.info | coveralls",
14
17
  "c": "rollup -c",
15
18
  "w": "rollup -c -w",
16
- "b": "browserify browser.js -o dist/browser/peernet.js --standalone Peernet && rm browser.js"
19
+ "b": "browserify dist/browser/peernet.js -o dist/browser/peernet.js --standalone Peernet && rm browser.js"
17
20
  },
18
21
  "keywords": [],
19
22
  "author": "",
20
23
  "license": "MIT",
24
+ "browserslist": "> 0.5%, last 2 versions, not dead",
21
25
  "dependencies": {
22
26
  "@leofcoin/generate-account": "^1.0.2",
23
- "@leofcoin/multi-wallet": "^2.0.6",
24
- "@leofcoin/storage": "^2.1.1",
27
+ "@leofcoin/multi-wallet": "^2.1.1",
28
+ "@leofcoin/peernet-swarm": "^0.1.9",
29
+ "@leofcoin/storage": "^2.1.2",
30
+ "@vandeurenglenn/base32": "^1.1.0",
31
+ "@vandeurenglenn/base58": "^1.1.0",
32
+ "@vandeurenglenn/is-hex": "^1.0.0",
25
33
  "@vandeurenglenn/little-pubsub": "^1.3.1",
26
- "async_hooks": "^1.0.0",
27
- "bs32": "^0.1.6",
28
- "bs58": "^4.0.1",
29
- "bs58check": "^2.1.2",
30
- "is-hex": "^1.1.3",
31
34
  "keccak": "^3.0.1",
32
35
  "koa": "^2.13.1",
33
36
  "node-fetch": "^2.6.1",
34
- "on-finished": "^2.4.1",
35
- "p2pt": "^1.5.0",
36
37
  "protons": "^2.0.1",
37
38
  "socket-request-client": "^1.5.0",
38
39
  "socket-request-server": "^1.5.0",
40
+ "stream-browserify": "^3.0.0",
39
41
  "varint": "^6.0.0"
40
42
  },
41
43
  "devDependencies": {
44
+ "@babel/plugin-proposal-private-methods": "^7.16.11",
42
45
  "@rollup/plugin-commonjs": "^19.0.1",
43
46
  "@rollup/plugin-eslint": "^8.0.1",
44
47
  "@rollup/plugin-json": "^4.1.0",
45
48
  "@rollup/plugin-node-resolve": "^13.0.4",
46
- "browserify": "^16.5.0",
47
49
  "coveralls": "^3.1.1",
48
50
  "esdoc": "^1.1.0",
49
51
  "esdoc-ecmascript-proposal-plugin": "^1.0.0",
@@ -51,9 +53,15 @@
51
53
  "eslint": "^7.31.0",
52
54
  "eslint-config-google": "^0.14.0",
53
55
  "nyc": "^15.1.0",
54
- "rollup": "^2.54.0",
56
+ "os-browserify": "^0.3.0",
57
+ "path-browserify": "^1.0.1",
58
+ "rollup": "^2.70.2",
55
59
  "rollup-plugin-modify": "^3.0.0",
60
+ "rollup-plugin-natives": "^0.7.5",
56
61
  "rollup-plugin-node-globals": "^1.4.0",
57
- "tape": "^5.2.2"
62
+ "rollup-plugin-node-polyfills": "^0.2.1",
63
+ "tape": "^5.2.2",
64
+ "webpack": "^5.72.0",
65
+ "webpack-cli": "^4.9.2"
58
66
  }
59
67
  }
package/rollup.config.js CHANGED
@@ -4,7 +4,14 @@ import resolve from '@rollup/plugin-node-resolve'
4
4
  import cjs from '@rollup/plugin-commonjs'
5
5
  import json from '@rollup/plugin-json'
6
6
  import modify from 'rollup-plugin-modify'
7
+ import nativePlugin from 'rollup-plugin-natives';
8
+ import polyfills from 'rollup-plugin-node-polyfills'
7
9
 
10
+ try {
11
+ execSync('rm dist -r')
12
+ } catch (e) {
13
+
14
+ }
8
15
  export default [{
9
16
  input: ['src/peernet.js', 'src/codec/codec.js', 'src/hash/hash.js', 'src/messages/dht.js',
10
17
  'src/messages/peernet-message.js', 'src/messages/dht-response.js', 'src/messages/request.js',
@@ -14,21 +21,42 @@ export default [{
14
21
  format: 'cjs'
15
22
  },
16
23
  plugins: [
17
- json()
24
+ json(),
25
+ modify({
26
+ HTTP_IMPORT: `if (this.hasDaemon) {
27
+ const httpClient = await import('./http/client/client.js')
28
+ globalThis.peernet.client = await httpClient.default({
29
+ protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
30
+ })
31
+ } else {
32
+ const http = await import('./http/http.js')
33
+ if (environment !== 'browser') http.default(options)
34
+ }`
35
+ })
18
36
  ]
19
37
  }, {
20
38
  input: 'src/peernet.js',
21
39
  output: {
22
- file: 'browser.js',
40
+ dir: 'dist/browser/',
23
41
  format: 'cjs'
24
42
  },
25
43
  plugins: [
26
44
  json(),
27
45
  modify({
28
- "import fetch from 'node-fetch'": ''
46
+ "import fetch from 'node-fetch'": '',
47
+ HTTP_IMPORT: ``
29
48
  }),
30
- cjs(),
31
- // resolve(),
49
+ // nativePlugin(),
50
+ // polyfills(),
51
+ // resolve({
52
+ // preferBuiltins: true,
53
+ // mainFields: ["browser", "module", "main"],
54
+ // extensions: ['.mjs', '.js', '.json']
55
+ // }),
56
+ // cjs({
57
+ // exclude: ['*.node'],
58
+ // extensions: ['.js']
59
+ // }),
32
60
  // lint({
33
61
  // fix: true,
34
62
  // exclude: ['package.json', "package-lock.json"]
@@ -0,0 +1,7 @@
1
+ export default [{
2
+ input: ['./src/base-x.js'],
3
+ output: [{
4
+ dir: './dist',
5
+ format: 'cjs'
6
+ }]
7
+ }]
package/src/client.js CHANGED
@@ -1,75 +1,75 @@
1
- import Pubsub from '@vandeurenglenn/little-pubsub'
2
- import PeernetPeer from './peer.js'
3
- import sha256 from 'crypto-js/sha256'
4
-
5
- import P2P from 'p2pt'
6
-
7
-
8
- /**
9
- * Array of peers
10
- * @type {Array}
11
- * @property {PeernetPeer} peer Instance of Peer
12
- */
13
- globalThis.connections = new Map()
14
- globalThis.recentConnections = new Map()
15
- globalThis.pubsub = globalThis.pubsub || new Pubsub({verbose: false})
16
-
17
- export default class PeernetClient {
18
- constructor(options = {}) {
19
- if (!options.id) options.id = Buffer.from('00000000000000000000000000000000')
20
- if (!options.networkVersion) options.networkVersion = 'v0.1.0'
21
- if (!options.networkName) options.networkName = 'peernet'
22
- this.id = options.id
23
-
24
- this.topic = Buffer.from(sha256(`${options.networkName}-${options.networkVersion}`).toString())
25
-
26
- const trackers = [
27
- 'wss://star.leofcoin.org',
28
- 'wss://tracker.openwebtorrent.com',
29
- // 'wss://tracker.sloppyta.co:443/announce',
30
- ]
31
- this.p2p = new P2P(trackers, this.topic.slice(0, 20))
32
- this.p2p.on('peerconnect', (peer) => {
33
- peer = new PeernetPeer(peer.id, peer)
34
- connections.set(peer.id, peer)
35
- pubsub.publish('peer:discovered', peer)
36
- })
37
-
38
- this.p2p.on('peerclose', (peer) => {
39
- // TODO: close peernetPeer
40
- const peernetPeer = connections.get(peer.id)
41
- if (peernetPeer) {
42
- peernetPeer.close()
43
- }
44
- connections.delete(peer.id)
45
- pubsub.publish('peer:disconnected', peer)
46
- })
47
-
48
- this.p2p.start()
49
-
50
- if (globalThis.process) {
51
- process.on('SIGINT', async () => {
52
- console.log('Caught interrupt signal')
53
- this.close()
54
- setTimeout(async () => {
55
- process.exit();
56
- }, 100);
57
- })
58
- } else {
59
- globalThis.onbeforeunload = () => {
60
- this.close()
61
- }
62
- }
63
- //
64
- // this.sw.on('close', () => {
65
- // })
66
- }
67
-
68
- close() {
69
- return this.p2p.destroy()
70
- }
71
-
72
- _peers() {
73
- return this.p2p.getPeers()
74
- }
75
- }
1
+ import Pubsub from '@vandeurenglenn/little-pubsub'
2
+ import PeernetPeer from './peer.js'
3
+ import sha256 from 'crypto-js/sha256'
4
+
5
+ import P2P from 'p2pt'
6
+
7
+
8
+ /**
9
+ * Array of peers
10
+ * @type {Array}
11
+ * @property {PeernetPeer} peer Instance of Peer
12
+ */
13
+ globalThis.connections = new Map()
14
+ globalThis.recentConnections = new Map()
15
+ globalThis.pubsub = globalThis.pubsub || new Pubsub({verbose: false})
16
+
17
+ export default class PeernetClient {
18
+ constructor(options = {}) {
19
+ if (!options.id) options.id = Buffer.from('00000000000000000000000000000000')
20
+ if (!options.networkVersion) options.networkVersion = 'v0.1.0'
21
+ if (!options.networkName) options.networkName = 'peernet'
22
+ this.id = options.id
23
+
24
+ this.topic = Buffer.from(sha256(`${options.networkName}-${options.networkVersion}`).toString())
25
+
26
+ const trackers = [
27
+ 'wss://star.leofcoin.org',
28
+ 'wss://tracker.openwebtorrent.com',
29
+ // 'wss://tracker.sloppyta.co:443/announce',
30
+ ]
31
+ this.p2p = new P2P(trackers, this.topic.slice(0, 20))
32
+ this.p2p.on('peerconnect', (peer) => {
33
+ peer = new PeernetPeer(peer.id, peer)
34
+ connections.set(peer.id, peer)
35
+ pubsub.publish('peer:discovered', peer)
36
+ })
37
+
38
+ this.p2p.on('peerclose', (peer) => {
39
+ // TODO: close peernetPeer
40
+ const peernetPeer = connections.get(peer.id)
41
+ if (peernetPeer) {
42
+ peernetPeer.close()
43
+ }
44
+ connections.delete(peer.id)
45
+ pubsub.publish('peer:disconnected', peer)
46
+ })
47
+
48
+ this.p2p.start()
49
+
50
+ if (globalThis.process) {
51
+ process.on('SIGINT', async () => {
52
+ console.log('Caught interrupt signal')
53
+ this.close()
54
+ setTimeout(async () => {
55
+ process.exit();
56
+ }, 100);
57
+ })
58
+ } else {
59
+ globalThis.onbeforeunload = () => {
60
+ this.close()
61
+ }
62
+ }
63
+ //
64
+ // this.sw.on('close', () => {
65
+ // })
66
+ }
67
+
68
+ close() {
69
+ return this.p2p.destroy()
70
+ }
71
+
72
+ _peers() {
73
+ return this.p2p.getPeers()
74
+ }
75
+ }
@@ -1,155 +1,172 @@
1
- import bs32 from 'bs32';
2
- import bs58 from 'bs58';
3
- import isHex from 'is-hex';
4
- import Codec from './codec';
5
- import Hash from './../hash/hash'
6
-
7
- export default class FormatInterface {
8
- /**
9
- * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
10
- * @param {Object} proto - {encode, decode}
11
- * @param {Object} options - {hashFormat, name}
12
- */
13
- constructor(buffer, proto, options = {}) {
14
- this.protoEncode = proto.encode
15
- this.protoDecode = proto.decode
16
- if (options.name) this.name = options.name
17
- this.hashFormat = options.hashFormat || 'bs32'
18
- if (buffer.name === options.name) {
19
- return buffer
20
- } else if (Buffer.isBuffer(buffer)) {
21
- const codec = new Codec(buffer)
22
- if (codec.name) {
23
- this.fromEncoded(buffer)
24
- } else {
25
- this.create(buffer)
26
- }
27
- } else {
28
- if (typeof buffer === 'string') {
29
- if (isHex(buffer)) this.fromHex(buffer)
30
- else if (bs32.test(buffer)) this.fromBs32(buffer)
31
- else this.fromBs58(buffer)
32
- } else if (typeof buffer === 'object' && !Array.isArray(buffer)) {
33
- this.create(buffer)
34
- }
35
- }
36
- }
37
-
38
- /**
39
- * @return {PeernetHash}
40
- */
41
- get peernetHash() {
42
- return new Hash(this.decoded, {name: this.name})
43
- }
44
-
45
- /**
46
- * @return {peernetHash}
47
- */
48
- get hash() {
49
- const upper = this.hashFormat.charAt(0).toUpperCase()
50
- const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`
51
- return this.peernetHash[`to${format}`]()
52
- }
53
-
54
- /**
55
- * @return {Object}
56
- */
57
- decode() {
58
- let encoded = this.encoded;
59
- const discoCodec = new Codec(this.encoded.toString('hex'))
60
- encoded = encoded.slice(discoCodec.codecBuffer.length)
61
- this.name = discoCodec.name
62
- this.decoded = this.protoDecode(encoded)
63
- return this.decoded
64
- }
65
-
66
- /**
67
- * @return {Buffer}
68
- */
69
- encode(decoded) {
70
- if (!decoded) decoded = this.decoded;
71
- const codec = new Codec(this.name)
72
- this.encoded = Buffer.concat([codec.codecBuffer, this.protoEncode(decoded)])
73
- return this.encoded
74
- }
75
-
76
- /**
77
- * @param {Buffer} encoded
78
- */
79
- fromEncoded(encoded) {
80
- const codec = new Codec(encoded)
81
- this.name = codec.name
82
- this.encoded = encoded
83
- this.decode()
84
- }
85
-
86
- /**
87
- * @param {String} encoded
88
- */
89
- fromHex(encoded) {
90
- this.encoded = Buffer.from(encoded, 'hex')
91
- this.decode()
92
- }
93
-
94
- /**
95
- * @param {String} encoded
96
- */
97
- fromBs32(encoded) {
98
- this.encoded = bs32.decode(encoded)
99
- this.decode()
100
- }
101
-
102
- /**
103
- * @param {String} encoded
104
- */
105
- fromBs58(encoded) {
106
- this.encoded = bs58.decode(encoded)
107
- this.decode()
108
- }
109
-
110
- /**
111
- * @return {String} encoded
112
- */
113
- toHex() {
114
- if (!this.encoded) this.encode()
115
- return this.encoded.toString('hex')
116
- }
117
-
118
- /**
119
- * @return {String} encoded
120
- */
121
- toBs32() {
122
- if (!this.encoded) this.encode()
123
- return bs32.encode(this.encoded)
124
- }
125
-
126
- /**
127
- * @return {String} encoded
128
- */
129
- toBs58() {
130
- if (!this.encoded) this.encode()
131
- return bs58.encode(this.encoded)
132
- }
133
-
134
- /**
135
- * @param {Object} data
136
- */
137
- create(data) {
138
- const decoded = {}
139
- if (this.keys?.length > 0) {
140
- for (const key of this.keys) {
141
- Object.defineProperties(decoded, {
142
- [key]: {
143
- enumerable: true,
144
- configurable: true,
145
- set: (val) => value = data[key],
146
- get: () => data[key]
147
- }
148
- })
149
- }
150
-
151
- this.decoded = decoded
152
- this.encode()
153
- }
154
- }
155
- }
1
+ import bs32 from '@vandeurenglenn/base32';
2
+ import bs58 from '@vandeurenglenn/base58';
3
+ import isHex from '@vandeurenglenn/is-hex';
4
+ import Codec from './codec';
5
+ import Hash from './../hash/hash'
6
+
7
+ export default class FormatInterface {
8
+ /**
9
+ * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
10
+ * @param {Object} proto - {encode, decode}
11
+ * @param {Object} options - {hashFormat, name}
12
+ */
13
+ constructor(buffer, proto, options = {}) {
14
+ this.protoEncode = proto.encode
15
+ this.protoDecode = proto.decode
16
+ this.hashFormat = options.hashFormat || 'bs32'
17
+ if (options.name) this.name = options.name
18
+ if (buffer instanceof Uint8Array) return this.fromUint8Array(buffer)
19
+ else if (buffer instanceof ArrayBuffer) return this.fromArrayBuffer(buffer)
20
+ else if (buffer.name === options.name) return buffer
21
+ else if (typeof buffer === 'string') {
22
+ if (isHex(buffer)) this.fromHex(buffer)
23
+ else if (bs32.isBase32(buffer)) this.fromBs32(buffer)
24
+ else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
25
+ else throw new Error(`unsupported string ${buffer}`)
26
+ } else {
27
+ this.create(buffer)
28
+ }
29
+ }
30
+
31
+ /**
32
+ * @return {PeernetHash}
33
+ */
34
+ get peernetHash() {
35
+ return new Hash(this.decoded, {name: this.name})
36
+ }
37
+
38
+ /**
39
+ * @return {peernetHash}
40
+ */
41
+ get hash() {
42
+ const upper = this.hashFormat.charAt(0).toUpperCase()
43
+ const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`
44
+ return this.peernetHash[`to${format}`]()
45
+ }
46
+
47
+ /**
48
+ * @return {Object}
49
+ */
50
+ decode() {
51
+ let encoded = this.encoded;
52
+ const discoCodec = new Codec(this.encoded)
53
+ encoded = encoded.slice(discoCodec.codecBuffer.length)
54
+ this.name = discoCodec.name
55
+ this.decoded = this.protoDecode(encoded)
56
+ return this.decoded
57
+ }
58
+
59
+ /**
60
+ * @return {Buffer}
61
+ */
62
+ encode(decoded) {
63
+ if (!decoded) decoded = this.decoded;
64
+ const codec = new Codec(this.name)
65
+ const encoded = this.protoEncode(decoded)
66
+ const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
67
+ uint8Array.set(codec.codecBuffer)
68
+ uint8Array.set(encoded, codec.codecBuffer.length)
69
+ this.encoded = uint8Array
70
+ return this.encoded
71
+ }
72
+
73
+ hasCodec() {
74
+ if (!this.encoded) return false
75
+ const codec = new Codec(this.encoded)
76
+ if (codec.name) return true
77
+ }
78
+
79
+ fromUint8Array(buffer) {
80
+ this.encoded = buffer
81
+ if (!this.hasCodec()) this.create(
82
+ JSON.parse(new TextDecoder().decode(this.encoded))
83
+ )
84
+ else this.decode()
85
+ }
86
+
87
+ fromArrayBuffer(buffer) {
88
+ this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
89
+ if (!this.hasCodec()) this.create(
90
+ JSON.parse(new TextDecoder().decode(this.encoded))
91
+ )
92
+ else this.decode()
93
+ }
94
+
95
+ /**
96
+ * @param {Buffer} encoded
97
+ */
98
+ fromEncoded(encoded) {
99
+ this.encoded = encoded
100
+ this.decode()
101
+ }
102
+
103
+ /**
104
+ * @param {String} encoded
105
+ */
106
+ fromHex(encoded) {
107
+ this.encoded = Buffer.from(encoded, 'hex')
108
+ this.decode()
109
+ }
110
+
111
+ /**
112
+ * @param {String} encoded
113
+ */
114
+ fromBs32(encoded) {
115
+ this.encoded = bs32.decode(encoded)
116
+ this.decode()
117
+ }
118
+
119
+ /**
120
+ * @param {String} encoded
121
+ */
122
+ fromBs58(encoded) {
123
+ this.encoded = bs58.decode(encoded)
124
+ this.decode()
125
+ }
126
+
127
+ /**
128
+ * @return {String} encoded
129
+ */
130
+ toHex() {
131
+ if (!this.encoded) this.encode()
132
+ return this.encoded.toString('hex')
133
+ }
134
+
135
+ /**
136
+ * @return {String} encoded
137
+ */
138
+ toBs32() {
139
+ if (!this.encoded) this.encode()
140
+ return bs32.encode(this.encoded)
141
+ }
142
+
143
+ /**
144
+ * @return {String} encoded
145
+ */
146
+ toBs58() {
147
+ if (!this.encoded) this.encode()
148
+ return bs58.encode(this.encoded)
149
+ }
150
+
151
+ /**
152
+ * @param {Object} data
153
+ */
154
+ create(data) {
155
+ const decoded = {}
156
+ if (this.keys?.length > 0) {
157
+ for (const key of this.keys) {
158
+ Object.defineProperties(decoded, {
159
+ [key]: {
160
+ enumerable: true,
161
+ configurable: true,
162
+ set: (val) => value = data[key],
163
+ get: () => data[key]
164
+ }
165
+ })
166
+ }
167
+
168
+ this.decoded = decoded
169
+ this.encode()
170
+ }
171
+ }
172
+ }