@leofcoin/peernet 1.1.79 → 1.1.81
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/.esdoc.json +10 -10
- package/.gitattributes +2 -2
- package/.prettierrc +7 -7
- package/.travis.yml +27 -27
- package/BREAKING_CHANGES.md +34 -34
- package/LICENSE +21 -21
- package/README.md +72 -72
- package/deploy.js +8 -8
- package/exports/browser/{browser-WUe24rfW.js → browser-DQJ6xf_F.js} +3 -3
- package/exports/browser/browser-store.js +168 -22
- package/exports/browser/{client-I9x7CFr1.js → client-C0VVXIWm.js} +66 -32
- package/exports/browser/{peernet-DULgegxE.js → identity-CQ_ieRiz.js} +2232 -13425
- package/exports/browser/identity.d.ts +1 -1
- package/exports/browser/identity.js +1 -0
- package/exports/browser/{index-sw14JvKD.js → index-BeqbCwUk.js} +1 -2
- package/exports/browser/{index-In1Jzp-v.js → index-CEwkDK9g.js} +10 -489
- package/exports/browser/{messages-lzTD4EMU.js → messages-BdevLRCA.js} +167 -166
- package/exports/browser/peernet-DEIKLS2i.js +13220 -0
- package/exports/browser/peernet.d.ts +7 -7
- package/exports/browser/peernet.js +3 -2
- package/exports/identity.js +92 -0
- package/exports/{messages-T3M-Ff1E.js → messages-BmpgEM4y.js} +163 -163
- package/exports/peernet.js +189 -273
- package/exports/src/prompts/password.js +3 -3
- package/exports/store.js +9 -2
- package/exports/types/identity.d.ts +1 -1
- package/exports/types/peernet.d.ts +7 -7
- package/index.html +19 -19
- package/package.json +71 -62
- package/rollup.config.js +63 -63
- package/src/dht/dht.ts +147 -147
- package/src/discovery/peer-discovery.js +75 -75
- package/src/errors/errors.js +12 -12
- package/src/handlers/data.js +15 -15
- package/src/handlers/message.js +34 -34
- package/src/identity.ts +104 -104
- package/src/messages/chat.js +13 -13
- package/src/messages/data-response.js +13 -13
- package/src/messages/data.js +17 -17
- package/src/messages/dht-response.js +13 -13
- package/src/messages/dht.js +21 -21
- package/src/messages/file-link.js +17 -17
- package/src/messages/file.js +17 -17
- package/src/messages/peer-response.js +13 -13
- package/src/messages/peer.js +13 -13
- package/src/messages/peernet.js +13 -13
- package/src/messages/ps.js +13 -13
- package/src/messages/request.js +13 -13
- package/src/messages/response.js +13 -13
- package/src/messages.js +13 -13
- package/src/peer-info.js +9 -9
- package/src/peernet.ts +817 -817
- package/src/prompts/password/browser.js +1 -1
- package/src/prompts/password/node.js +6 -6
- package/src/proto/chat-message.proto.js +6 -6
- package/src/proto/data-response.proto.js +4 -4
- package/src/proto/data.proto.js +4 -4
- package/src/proto/dht-response.proto.js +4 -4
- package/src/proto/dht.proto.js +4 -4
- package/src/proto/file-link.proto.js +5 -5
- package/src/proto/file.proto.js +5 -5
- package/src/proto/peer-response.proto.js +3 -3
- package/src/proto/peer.proto.js +3 -3
- package/src/proto/peernet.proto.js +7 -7
- package/src/proto/ps.proto.js +4 -4
- package/src/proto/request.proto.js +4 -4
- package/src/proto/response.proto.js +3 -3
- package/src/types.ts +25 -25
- package/src/utils/utils.js +77 -77
- package/test/client.js +14 -14
- package/test/codec.js +56 -56
- package/test/hash.js +13 -13
- package/test/index.js +3 -3
- package/test/lastBlock.js +7 -7
- package/test/messages.js +26 -26
- package/test/peernet.js +17 -17
- package/test/peernet.test.js +159 -0
- package/test.js +62 -62
- package/test2.js +13 -13
- package/test3.js +15 -15
- package/test4.js +7 -7
- package/tsconfig.json +11 -13
- /package/exports/browser/{browser-AyxSBUXj.js → browser-pguCHlVu.js} +0 -0
- /package/exports/browser/{qr-scanner-worker.min-RaSiJc_R.js → qr-scanner-worker.min-Dy0qkKA4.js} +0 -0
- /package/exports/browser/{value-wzPYMxsX.js → value-C3vAp-wb.js} +0 -0
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { protoFor } from './../utils/utils.js'
|
|
2
|
-
|
|
3
|
-
export default class PeerDiscovery {
|
|
4
|
-
constructor(id) {
|
|
5
|
-
this.id = id
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
_getPeerId(id) {
|
|
9
|
-
if (!peernet.peerMap || (peernet.peerMap && peernet.peerMap.size === 0)) return false
|
|
10
|
-
|
|
11
|
-
for (const entry of [...peernet.peerMap.entries()]) {
|
|
12
|
-
for (const _id of entry[1]) {
|
|
13
|
-
if (_id === id) return entry[0]
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async discover(peer) {
|
|
19
|
-
let id = this._getPeerId(peer.id)
|
|
20
|
-
if (id) return id
|
|
21
|
-
const data = await new peernet.protos['peernet-peer']({ id: this.id })
|
|
22
|
-
const node = await peernet.prepareMessage(peer.id, data.encoded)
|
|
23
|
-
|
|
24
|
-
let response = await peer.request(node.encoded)
|
|
25
|
-
response = await protoFor(response)
|
|
26
|
-
response = await new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
27
|
-
|
|
28
|
-
id = response.decoded.id
|
|
29
|
-
if (id === this.id) return
|
|
30
|
-
|
|
31
|
-
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id])
|
|
32
|
-
else {
|
|
33
|
-
const connections = peernet.peerMap.get(id)
|
|
34
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
35
|
-
connections.push(peer.id)
|
|
36
|
-
peernet.peerMap.set(peer.id, connections)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return id
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async discoverHandler(message, peer) {
|
|
43
|
-
const { id, proto } = message
|
|
44
|
-
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
45
|
-
if (proto.name === 'peernet-peer') {
|
|
46
|
-
const from = proto.decoded.id
|
|
47
|
-
if (from === this.id) return
|
|
48
|
-
|
|
49
|
-
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
50
|
-
else {
|
|
51
|
-
const connections = peernet.peerMap.get(from)
|
|
52
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
53
|
-
connections.push(peer.id)
|
|
54
|
-
peernet.peerMap.set(from, connections)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const data = await new peernet.protos['peernet-peer-response']({ id: this.id })
|
|
58
|
-
const node = await peernet.prepareMessage(from, data.encoded)
|
|
59
|
-
|
|
60
|
-
peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })))
|
|
61
|
-
} else if (proto.name === 'peernet-peer-response') {
|
|
62
|
-
const from = proto.decoded.id
|
|
63
|
-
if (from === this.id) return
|
|
64
|
-
|
|
65
|
-
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
66
|
-
else {
|
|
67
|
-
const connections = peernet.peerMap.get(from)
|
|
68
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
69
|
-
connections.push(peer.id)
|
|
70
|
-
peernet.peerMap.set(from, connections)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
1
|
+
import { protoFor } from './../utils/utils.js'
|
|
2
|
+
|
|
3
|
+
export default class PeerDiscovery {
|
|
4
|
+
constructor(id) {
|
|
5
|
+
this.id = id
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
_getPeerId(id) {
|
|
9
|
+
if (!peernet.peerMap || (peernet.peerMap && peernet.peerMap.size === 0)) return false
|
|
10
|
+
|
|
11
|
+
for (const entry of [...peernet.peerMap.entries()]) {
|
|
12
|
+
for (const _id of entry[1]) {
|
|
13
|
+
if (_id === id) return entry[0]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async discover(peer) {
|
|
19
|
+
let id = this._getPeerId(peer.id)
|
|
20
|
+
if (id) return id
|
|
21
|
+
const data = await new peernet.protos['peernet-peer']({ id: this.id })
|
|
22
|
+
const node = await peernet.prepareMessage(peer.id, data.encoded)
|
|
23
|
+
|
|
24
|
+
let response = await peer.request(node.encoded)
|
|
25
|
+
response = await protoFor(response)
|
|
26
|
+
response = await new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
27
|
+
|
|
28
|
+
id = response.decoded.id
|
|
29
|
+
if (id === this.id) return
|
|
30
|
+
|
|
31
|
+
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id])
|
|
32
|
+
else {
|
|
33
|
+
const connections = peernet.peerMap.get(id)
|
|
34
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
35
|
+
connections.push(peer.id)
|
|
36
|
+
peernet.peerMap.set(peer.id, connections)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return id
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async discoverHandler(message, peer) {
|
|
43
|
+
const { id, proto } = message
|
|
44
|
+
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
45
|
+
if (proto.name === 'peernet-peer') {
|
|
46
|
+
const from = proto.decoded.id
|
|
47
|
+
if (from === this.id) return
|
|
48
|
+
|
|
49
|
+
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
50
|
+
else {
|
|
51
|
+
const connections = peernet.peerMap.get(from)
|
|
52
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
53
|
+
connections.push(peer.id)
|
|
54
|
+
peernet.peerMap.set(from, connections)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const data = await new peernet.protos['peernet-peer-response']({ id: this.id })
|
|
58
|
+
const node = await peernet.prepareMessage(from, data.encoded)
|
|
59
|
+
|
|
60
|
+
peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })))
|
|
61
|
+
} else if (proto.name === 'peernet-peer-response') {
|
|
62
|
+
const from = proto.decoded.id
|
|
63
|
+
if (from === this.id) return
|
|
64
|
+
|
|
65
|
+
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
66
|
+
else {
|
|
67
|
+
const connections = peernet.peerMap.get(from)
|
|
68
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
69
|
+
connections.push(peer.id)
|
|
70
|
+
peernet.peerMap.set(from, connections)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/errors/errors.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export const encapsulatedError = () => {
|
|
2
|
-
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export const dhtError = (proto) => {
|
|
6
|
-
const text = `Received proto ${proto.name} expected peernet-dht-response`
|
|
7
|
-
return new Error(`Routing error: ${text}`)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const nothingFoundError = (hash) => {
|
|
11
|
-
return new Error(`nothing found for ${hash}`)
|
|
12
|
-
}
|
|
1
|
+
export const encapsulatedError = () => {
|
|
2
|
+
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export const dhtError = (proto) => {
|
|
6
|
+
const text = `Received proto ${proto.name} expected peernet-dht-response`
|
|
7
|
+
return new Error(`Routing error: ${text}`)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const nothingFoundError = (hash) => {
|
|
11
|
+
return new Error(`nothing found for ${hash}`)
|
|
12
|
+
}
|
package/src/handlers/data.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { protoFor } from './../utils/utils.js'
|
|
2
|
-
|
|
3
|
-
const dataHandler = async (message) => {
|
|
4
|
-
if (!message) return
|
|
5
|
-
|
|
6
|
-
try {
|
|
7
|
-
const { data, id, from, peer } = message
|
|
8
|
-
const proto = await protoFor(data)
|
|
9
|
-
peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from)
|
|
10
|
-
} catch (error) {
|
|
11
|
-
console.error(error)
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default dataHandler
|
|
1
|
+
import { protoFor } from './../utils/utils.js'
|
|
2
|
+
|
|
3
|
+
const dataHandler = async (message) => {
|
|
4
|
+
if (!message) return
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const { data, id, from, peer } = message
|
|
8
|
+
const proto = await protoFor(data)
|
|
9
|
+
peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from)
|
|
10
|
+
} catch (error) {
|
|
11
|
+
console.error(error)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default dataHandler
|
package/src/handlers/message.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
export default class MessageHandler {
|
|
2
|
-
constructor(network) {
|
|
3
|
-
this.network = network
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* hash and sign message
|
|
7
|
-
*
|
|
8
|
-
* @param {object} message
|
|
9
|
-
* @param {Buffer} message.from peer id
|
|
10
|
-
* @param {Buffer} message.to peer id
|
|
11
|
-
* @param {string} message.data Peernet message
|
|
12
|
-
* (PeernetMessage excluded) encoded as a string
|
|
13
|
-
* @return message
|
|
14
|
-
*/
|
|
15
|
-
async hashAndSignMessage(message) {
|
|
16
|
-
const hash = await message.peernetHash
|
|
17
|
-
message.decoded.signature = globalThis.identity.sign(hash.buffer)
|
|
18
|
-
return message
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param {String} from - peer id
|
|
23
|
-
* @param {String} to - peer id
|
|
24
|
-
* @param {String|PeernetMessage} data - data encoded message string
|
|
25
|
-
* or the messageNode itself
|
|
26
|
-
*/
|
|
27
|
-
async prepareMessage(message) {
|
|
28
|
-
if (message.keys.includes('signature')) {
|
|
29
|
-
message = await this.hashAndSignMessage(message)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return message
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
export default class MessageHandler {
|
|
2
|
+
constructor(network) {
|
|
3
|
+
this.network = network
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* hash and sign message
|
|
7
|
+
*
|
|
8
|
+
* @param {object} message
|
|
9
|
+
* @param {Buffer} message.from peer id
|
|
10
|
+
* @param {Buffer} message.to peer id
|
|
11
|
+
* @param {string} message.data Peernet message
|
|
12
|
+
* (PeernetMessage excluded) encoded as a string
|
|
13
|
+
* @return message
|
|
14
|
+
*/
|
|
15
|
+
async hashAndSignMessage(message) {
|
|
16
|
+
const hash = await message.peernetHash
|
|
17
|
+
message.decoded.signature = globalThis.identity.sign(hash.buffer)
|
|
18
|
+
return message
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {String} from - peer id
|
|
23
|
+
* @param {String} to - peer id
|
|
24
|
+
* @param {String|PeernetMessage} data - data encoded message string
|
|
25
|
+
* or the messageNode itself
|
|
26
|
+
*/
|
|
27
|
+
async prepareMessage(message) {
|
|
28
|
+
if (message.keys.includes('signature')) {
|
|
29
|
+
message = await this.hashAndSignMessage(message)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return message
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/identity.ts
CHANGED
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
import MultiWallet from '@leofcoin/multi-wallet'
|
|
2
|
-
import base58 from '@vandeurenglenn/base58'
|
|
3
|
-
import type { base58String } from '@vandeurenglenn/base58'
|
|
4
|
-
import { encrypt, decrypt } from '@leofcoin/identity-utils'
|
|
5
|
-
import QrScanner from 'qr-scanner'
|
|
6
|
-
import qrcode from 'qrcode'
|
|
7
|
-
|
|
8
|
-
export default class Identity {
|
|
9
|
-
#wallet: MultiWallet
|
|
10
|
-
network
|
|
11
|
-
id: string
|
|
12
|
-
selectedAccount: string
|
|
13
|
-
|
|
14
|
-
constructor(network: string) {
|
|
15
|
-
this.network = network
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]> {
|
|
19
|
-
return this.getAccounts()
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async getAccounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]> {
|
|
23
|
-
let accounts = await globalThis.walletStore.get('accounts')
|
|
24
|
-
accounts = new TextDecoder().decode(accounts)
|
|
25
|
-
return JSON.parse(accounts)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async load(password?: string): Promise<void> {
|
|
29
|
-
if (password && password.includes('.txt')) {
|
|
30
|
-
const { readFile } = await import('fs/promises')
|
|
31
|
-
try {
|
|
32
|
-
password = (await readFile(password)).toString()
|
|
33
|
-
} catch (error) {
|
|
34
|
-
console.error(error)
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
if (!password) {
|
|
38
|
-
// @ts-ignore
|
|
39
|
-
const importee: { default: () => Promise<string> } = await import('./prompts/password.js')
|
|
40
|
-
password = await importee.default()
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const accountExists = await globalThis.accountStore.has('public')
|
|
44
|
-
if (accountExists) {
|
|
45
|
-
const pub = await globalThis.accountStore.get('public')
|
|
46
|
-
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
|
|
47
|
-
const selected = await globalThis.walletStore.get('selected-account')
|
|
48
|
-
this.selectedAccount = new TextDecoder().decode(selected)
|
|
49
|
-
} else {
|
|
50
|
-
const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
|
|
51
|
-
const { identity, accounts } = await importee.default(password, this.network)
|
|
52
|
-
await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }))
|
|
53
|
-
|
|
54
|
-
await globalThis.walletStore.put('version', String(1))
|
|
55
|
-
await globalThis.walletStore.put('accounts', JSON.stringify(accounts))
|
|
56
|
-
await globalThis.walletStore.put('selected-account', accounts[0][1])
|
|
57
|
-
await globalThis.walletStore.put('identity', JSON.stringify(identity))
|
|
58
|
-
|
|
59
|
-
this.selectedAccount = accounts[0][1]
|
|
60
|
-
this.id = identity.walletId
|
|
61
|
-
}
|
|
62
|
-
const identity = JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('identity')))
|
|
63
|
-
this.#wallet = new MultiWallet(this.network)
|
|
64
|
-
const multiWIF = await decrypt(password, base58.decode(identity.multiWIF))
|
|
65
|
-
await this.#wallet.fromMultiWif(multiWIF)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
selectAccount(account: string) {
|
|
69
|
-
this.selectedAccount = account
|
|
70
|
-
return walletStore.put('selected-account', account)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
sign(hash: Uint8Array) {
|
|
74
|
-
return this.#wallet.sign(hash.subarray(0, 32))
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
lock(password: string) {
|
|
78
|
-
this.#wallet.lock(password)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
unlock(password: string) {
|
|
82
|
-
this.#wallet.unlock(password)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async export(password: string) {
|
|
86
|
-
return this.#wallet.export(password)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async import(password, encrypted: base58String) {
|
|
90
|
-
await this.#wallet.import(password, encrypted)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async exportQR(password: string) {
|
|
94
|
-
const exported = await this.export(password)
|
|
95
|
-
return globalThis.navigator
|
|
96
|
-
? await qrcode.toDataURL(exported)
|
|
97
|
-
: await qrcode.toString(exported, { type: 'terminal' })
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async importQR(image: File | Blob, password: string) {
|
|
101
|
-
const multiWIF = await QrScanner.default.scanImage(image)
|
|
102
|
-
return this.import(password, multiWIF)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
1
|
+
import MultiWallet from '@leofcoin/multi-wallet'
|
|
2
|
+
import base58 from '@vandeurenglenn/base58'
|
|
3
|
+
import type { base58String } from '@vandeurenglenn/base58'
|
|
4
|
+
import { encrypt, decrypt } from '@leofcoin/identity-utils'
|
|
5
|
+
import QrScanner from 'qr-scanner'
|
|
6
|
+
import qrcode from 'qrcode'
|
|
7
|
+
|
|
8
|
+
export default class Identity {
|
|
9
|
+
#wallet: MultiWallet
|
|
10
|
+
network
|
|
11
|
+
id: string
|
|
12
|
+
selectedAccount: string
|
|
13
|
+
|
|
14
|
+
constructor(network: string) {
|
|
15
|
+
this.network = network
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]> {
|
|
19
|
+
return this.getAccounts()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async getAccounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]> {
|
|
23
|
+
let accounts = await globalThis.walletStore.get('accounts')
|
|
24
|
+
accounts = new TextDecoder().decode(accounts)
|
|
25
|
+
return JSON.parse(accounts)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async load(password?: string): Promise<void> {
|
|
29
|
+
if (password && password.includes('.txt')) {
|
|
30
|
+
const { readFile } = await import('fs/promises')
|
|
31
|
+
try {
|
|
32
|
+
password = (await readFile(password)).toString()
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error(error)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (!password) {
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
const importee: { default: () => Promise<string> } = await import('./prompts/password.js')
|
|
40
|
+
password = await importee.default()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const accountExists = await globalThis.accountStore.has('public')
|
|
44
|
+
if (accountExists) {
|
|
45
|
+
const pub = await globalThis.accountStore.get('public')
|
|
46
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
|
|
47
|
+
const selected = await globalThis.walletStore.get('selected-account')
|
|
48
|
+
this.selectedAccount = new TextDecoder().decode(selected)
|
|
49
|
+
} else {
|
|
50
|
+
const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
|
|
51
|
+
const { identity, accounts } = await importee.default(password, this.network)
|
|
52
|
+
await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }))
|
|
53
|
+
|
|
54
|
+
await globalThis.walletStore.put('version', String(1))
|
|
55
|
+
await globalThis.walletStore.put('accounts', JSON.stringify(accounts))
|
|
56
|
+
await globalThis.walletStore.put('selected-account', accounts[0][1])
|
|
57
|
+
await globalThis.walletStore.put('identity', JSON.stringify(identity))
|
|
58
|
+
|
|
59
|
+
this.selectedAccount = accounts[0][1]
|
|
60
|
+
this.id = identity.walletId
|
|
61
|
+
}
|
|
62
|
+
const identity = JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('identity')))
|
|
63
|
+
this.#wallet = new MultiWallet(this.network)
|
|
64
|
+
const multiWIF = await decrypt(password, base58.decode(identity.multiWIF))
|
|
65
|
+
await this.#wallet.fromMultiWif(multiWIF)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
selectAccount(account: string) {
|
|
69
|
+
this.selectedAccount = account
|
|
70
|
+
return walletStore.put('selected-account', account)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
sign(hash: Uint8Array) {
|
|
74
|
+
return this.#wallet.sign(hash.subarray(0, 32))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
lock(password: string) {
|
|
78
|
+
this.#wallet.lock(password)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
unlock(password: string) {
|
|
82
|
+
this.#wallet.unlock(password)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async export(password: string) {
|
|
86
|
+
return this.#wallet.export(password)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async import(password, encrypted: base58String) {
|
|
90
|
+
await this.#wallet.import(password, encrypted)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async exportQR(password: string) {
|
|
94
|
+
const exported = await this.export(password)
|
|
95
|
+
return globalThis.navigator
|
|
96
|
+
? await qrcode.toDataURL(exported)
|
|
97
|
+
: await qrcode.toString(exported, { type: 'terminal' })
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async importQR(image: File | Blob, password: string) {
|
|
101
|
+
const multiWIF = await QrScanner.default.scanImage(image)
|
|
102
|
+
return this.import(password, multiWIF)
|
|
103
|
+
}
|
|
104
|
+
}
|
package/src/messages/chat.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import proto from './../proto/chat-message.proto.js'
|
|
2
|
-
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
-
|
|
4
|
-
export default class ChatMessage extends FormatInterface {
|
|
5
|
-
get messageName() {
|
|
6
|
-
return 'ChatMessage'
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
constructor(buffer) {
|
|
10
|
-
const name = 'chat-message'
|
|
11
|
-
super(buffer, proto, { name })
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
import proto from './../proto/chat-message.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
export default class ChatMessage extends FormatInterface {
|
|
5
|
+
get messageName() {
|
|
6
|
+
return 'ChatMessage'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
constructor(buffer) {
|
|
10
|
+
const name = 'chat-message'
|
|
11
|
+
super(buffer, proto, { name })
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import proto from './../proto/data-response.proto.js'
|
|
2
|
-
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
-
|
|
4
|
-
export default class DataMessageResponse extends FormatInterface {
|
|
5
|
-
get messageName() {
|
|
6
|
-
return 'PeernetDataMessageResponse'
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
constructor(data) {
|
|
10
|
-
const name = 'peernet-data-response'
|
|
11
|
-
super(data, proto, { name })
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
import proto from './../proto/data-response.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
export default class DataMessageResponse extends FormatInterface {
|
|
5
|
+
get messageName() {
|
|
6
|
+
return 'PeernetDataMessageResponse'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
constructor(data) {
|
|
10
|
+
const name = 'peernet-data-response'
|
|
11
|
+
super(data, proto, { name })
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/messages/data.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import proto from './../proto/data.proto.js'
|
|
2
|
-
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @extends {CodecFormat}
|
|
6
|
-
*/
|
|
7
|
-
export default class DataMessage extends FormatInterface {
|
|
8
|
-
get messageName() {
|
|
9
|
-
return 'PeernetDataMessage'
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
13
|
-
*/
|
|
14
|
-
constructor(data) {
|
|
15
|
-
super(data, proto, { name: 'peernet-data' })
|
|
16
|
-
}
|
|
17
|
-
}
|
|
1
|
+
import proto from './../proto/data.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @extends {CodecFormat}
|
|
6
|
+
*/
|
|
7
|
+
export default class DataMessage extends FormatInterface {
|
|
8
|
+
get messageName() {
|
|
9
|
+
return 'PeernetDataMessage'
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
13
|
+
*/
|
|
14
|
+
constructor(data) {
|
|
15
|
+
super(data, proto, { name: 'peernet-data' })
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import proto from './../proto/dht-response.proto.js'
|
|
2
|
-
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
-
|
|
4
|
-
export default class DHTMessageResponse extends FormatInterface {
|
|
5
|
-
get messageName() {
|
|
6
|
-
return 'PeernetDHTMessageResponse'
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
constructor(data) {
|
|
10
|
-
const name = 'peernet-dht-response'
|
|
11
|
-
super(data, proto, { name })
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
import proto from './../proto/dht-response.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
export default class DHTMessageResponse extends FormatInterface {
|
|
5
|
+
get messageName() {
|
|
6
|
+
return 'PeernetDHTMessageResponse'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
constructor(data) {
|
|
10
|
+
const name = 'peernet-dht-response'
|
|
11
|
+
super(data, proto, { name })
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/messages/dht.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import proto from './../proto/dht.proto.js'
|
|
2
|
-
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @example `
|
|
6
|
-
new DHTMessage(hash, store)
|
|
7
|
-
// store = optional if not set, peernet checks every store
|
|
8
|
-
let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
|
|
9
|
-
message = new DHTMessage('hashmvbs124xcfd...', 'block')
|
|
10
|
-
`
|
|
11
|
-
*/
|
|
12
|
-
export default class DHTMessage extends FormatInterface {
|
|
13
|
-
get messageName() {
|
|
14
|
-
return 'PeernetDHTMessage'
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
constructor(data) {
|
|
18
|
-
const name = 'peernet-dht'
|
|
19
|
-
super(data, proto, { name })
|
|
20
|
-
}
|
|
21
|
-
}
|
|
1
|
+
import proto from './../proto/dht.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @example `
|
|
6
|
+
new DHTMessage(hash, store)
|
|
7
|
+
// store = optional if not set, peernet checks every store
|
|
8
|
+
let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
|
|
9
|
+
message = new DHTMessage('hashmvbs124xcfd...', 'block')
|
|
10
|
+
`
|
|
11
|
+
*/
|
|
12
|
+
export default class DHTMessage extends FormatInterface {
|
|
13
|
+
get messageName() {
|
|
14
|
+
return 'PeernetDHTMessage'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
constructor(data) {
|
|
18
|
+
const name = 'peernet-dht'
|
|
19
|
+
super(data, proto, { name })
|
|
20
|
+
}
|
|
21
|
+
}
|