@leofcoin/chain 1.4.22 → 1.4.23
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/exports/chain.js +1071 -0
- package/exports/node.js +23 -0
- package/exports/typings/chain.d.ts +83 -0
- package/exports/typings/config/config.d.ts +1 -0
- package/exports/typings/config/main.d.ts +5 -0
- package/exports/typings/config/protocol.d.ts +6 -0
- package/exports/typings/contract.d.ts +29 -0
- package/exports/typings/fee/config.d.ts +4 -0
- package/exports/typings/machine.d.ts +26 -0
- package/exports/typings/node.d.ts +9 -0
- package/exports/typings/protocol.d.ts +4 -0
- package/exports/typings/state.d.ts +5 -0
- package/exports/typings/transaction.d.ts +47 -0
- package/exports/typings/typer.d.ts +6 -0
- package/package.json +12 -6
- package/CHANGELOG.md +0 -14
- package/demo/index.html +0 -25
- package/examples/contracts/token.js +0 -7
- package/plugins/bundle.js +0 -18
- package/src/chain.js +0 -716
- package/src/config/config.js +0 -14
- package/src/config/main.js +0 -4
- package/src/config/protocol.js +0 -5
- package/src/contract.js +0 -52
- package/src/fee/config.js +0 -3
- package/src/machine.js +0 -215
- package/src/node.js +0 -24
- package/src/protocol.js +0 -4
- package/src/state.js +0 -31
- package/src/transaction.js +0 -234
- package/src/type.index.d.ts +0 -21
- package/src/typer.js +0 -19
- package/test/chain.js +0 -120
- package/test/contracts/token.js +0 -40
- package/test/create-genesis.js +0 -66
- package/test/index.js +0 -1
- package/tsconfig.js +0 -15
- package/workers/block-worker.js +0 -40
- package/workers/machine-worker.js +0 -219
- package/workers/pool-worker.js +0 -28
- package/workers/transaction-worker.js +0 -20
- package/workers/workers.js +0 -9
package/src/config/config.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import main from './main'
|
|
2
|
-
import protocol from './protocol'
|
|
3
|
-
|
|
4
|
-
// export default async () => {
|
|
5
|
-
// let config = { ...main, protocol }
|
|
6
|
-
// try {
|
|
7
|
-
// let data = await read(main.configPath)
|
|
8
|
-
// data = JSON.parse(data.toString())
|
|
9
|
-
// config = { ...config, ...data }
|
|
10
|
-
// } catch (e) {
|
|
11
|
-
// await write(main.configPath, JSON.stringify(config, null, '\t'))
|
|
12
|
-
// }
|
|
13
|
-
// return config
|
|
14
|
-
// }
|
package/src/config/main.js
DELETED
package/src/config/protocol.js
DELETED
package/src/contract.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import Transaction from "./transaction.js";
|
|
2
|
-
import { createContractMessage } from '@leofcoin/lib'
|
|
3
|
-
import addresses from "@leofcoin/addresses"
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @extends {Transaction}
|
|
7
|
-
*/
|
|
8
|
-
export default class Contract extends Transaction {
|
|
9
|
-
constructor() {
|
|
10
|
-
super()
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* @param {Address} creator
|
|
16
|
-
* @param {String} contract
|
|
17
|
-
* @param {Array} constructorParameters
|
|
18
|
-
* @returns lib.createContractMessage
|
|
19
|
-
*/
|
|
20
|
-
async createContractMessage(creator, contract, constructorParameters = []) {
|
|
21
|
-
return createContractMessage(creator, contract, constructorParameters)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
* @param {Address} creator
|
|
27
|
-
* @param {String} contract
|
|
28
|
-
* @param {Array} constructorParameters
|
|
29
|
-
* @returns {Address}
|
|
30
|
-
*/
|
|
31
|
-
async createContractAddress(creator, contract, constructorParameters = []) {
|
|
32
|
-
contract = await this.createContractMessage(creator, contract, constructorParameters)
|
|
33
|
-
return contract.hash()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
*
|
|
38
|
-
* @param {String} contract
|
|
39
|
-
* @param {Array} parameters
|
|
40
|
-
* @returns
|
|
41
|
-
*/
|
|
42
|
-
async deployContract(contract, constructorParameters = []) {
|
|
43
|
-
const message = await createContractMessage(peernet.selectedAccount, contract, constructorParameters)
|
|
44
|
-
try {
|
|
45
|
-
await contractStore.put(await message.hash(), message.encoded)
|
|
46
|
-
} catch (error) {
|
|
47
|
-
throw error
|
|
48
|
-
}
|
|
49
|
-
return this.createTransactionFrom(peernet.selectedAccount, addresses.contractFactory, 'registerContract', [await message.hash()])
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
}
|
package/src/fee/config.js
DELETED
package/src/machine.js
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { contractFactory, nativeToken, validators, nameService } from '@leofcoin/addresses'
|
|
2
|
-
import { randombytes } from '@leofcoin/crypto'
|
|
3
|
-
import EasyWorker from '@vandeurenglenn/easy-worker'
|
|
4
|
-
import { ContractMessage } from '@leofcoin/messages'
|
|
5
|
-
// import State from './state'
|
|
6
|
-
|
|
7
|
-
export default class Machine {
|
|
8
|
-
#contracts = {}
|
|
9
|
-
#nonces = {}
|
|
10
|
-
lastBlock = {index: 0, hash: '0x0', previousHash: '0x0'}
|
|
11
|
-
|
|
12
|
-
constructor(blocks) {
|
|
13
|
-
return this.#init(blocks)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
#createMessage(sender = peernet.selectedAccount) {
|
|
17
|
-
return {
|
|
18
|
-
sender,
|
|
19
|
-
call: this.execute,
|
|
20
|
-
staticCall: this.get.bind(this)
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async #onmessage(data) {
|
|
25
|
-
switch (data.type) {
|
|
26
|
-
case 'contractError': {
|
|
27
|
-
console.warn(`removing contract ${await data.hash()}`);
|
|
28
|
-
await contractStore.delete(await data.hash())
|
|
29
|
-
break
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
case 'initError': {
|
|
33
|
-
console.error(`init error: ${data.message}`);
|
|
34
|
-
break
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
case 'executionError': {
|
|
38
|
-
// console.warn(`error executing transaction ${data.message}`);
|
|
39
|
-
pubsub.publish(data.id, {error: data.message})
|
|
40
|
-
break
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
case 'debug': {
|
|
44
|
-
for (const message of data.messages) debug(message)
|
|
45
|
-
break
|
|
46
|
-
}
|
|
47
|
-
case 'machine-ready': {
|
|
48
|
-
this.lastBlock = data.lastBlock
|
|
49
|
-
pubsub.publish('machine.ready', true)
|
|
50
|
-
break
|
|
51
|
-
}
|
|
52
|
-
case 'response': {
|
|
53
|
-
pubsub.publish(data.id, data.value || false)
|
|
54
|
-
break
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async #init(blocks) {
|
|
61
|
-
return new Promise(async (resolve) => {
|
|
62
|
-
const machineReady = () => {
|
|
63
|
-
pubsub.unsubscribe('machine.ready', machineReady)
|
|
64
|
-
resolve(this)
|
|
65
|
-
}
|
|
66
|
-
pubsub.subscribe('machine.ready', machineReady)
|
|
67
|
-
|
|
68
|
-
this.worker = await new EasyWorker('node_modules/@leofcoin/workers/src/machine-worker.js', {serialization: 'advanced', type:'module'})
|
|
69
|
-
this.worker.onmessage(this.#onmessage.bind(this))
|
|
70
|
-
|
|
71
|
-
// const blocks = await blockStore.values()
|
|
72
|
-
const contracts = await Promise.all([
|
|
73
|
-
contractStore.get(contractFactory),
|
|
74
|
-
contractStore.get(nativeToken),
|
|
75
|
-
contractStore.get(validators),
|
|
76
|
-
contractStore.get(nameService)
|
|
77
|
-
])
|
|
78
|
-
|
|
79
|
-
const message = {
|
|
80
|
-
type: 'init',
|
|
81
|
-
input: {
|
|
82
|
-
contracts,
|
|
83
|
-
blocks,
|
|
84
|
-
peerid: peernet.peerId
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
this.worker.postMessage(message)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async #runContract(contractMessage) {
|
|
93
|
-
const hash = await contractMessage.hash()
|
|
94
|
-
return new Promise((resolve, reject) => {
|
|
95
|
-
const id = randombytes(20).toString('hex')
|
|
96
|
-
const onmessage = message => {
|
|
97
|
-
pubsub.unsubscribe(id, onmessage)
|
|
98
|
-
if (message?.error) reject(message.error)
|
|
99
|
-
else resolve(message)
|
|
100
|
-
}
|
|
101
|
-
pubsub.subscribe(id, onmessage)
|
|
102
|
-
this.worker.postMessage({
|
|
103
|
-
type: 'run',
|
|
104
|
-
id,
|
|
105
|
-
input: {
|
|
106
|
-
decoded: contractMessage.decoded,
|
|
107
|
-
encoded: contractMessage.encoded,
|
|
108
|
-
hash
|
|
109
|
-
}
|
|
110
|
-
})
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
*
|
|
117
|
-
* @param {Address} contract
|
|
118
|
-
* @param {String} method
|
|
119
|
-
* @param {Array} parameters
|
|
120
|
-
* @returns Promise<message>
|
|
121
|
-
*/
|
|
122
|
-
async execute(contract, method, parameters) {
|
|
123
|
-
try {
|
|
124
|
-
if (contract === contractFactory && method === 'registerContract') {
|
|
125
|
-
if (await this.has(parameters[0])) throw new Error(`duplicate contract @${parameters[0]}`)
|
|
126
|
-
let message;
|
|
127
|
-
if (!await contractStore.has(parameters[0])) {
|
|
128
|
-
message = await peernet.get(parameters[0], 'contract')
|
|
129
|
-
message = await new ContractMessage(message)
|
|
130
|
-
await contractStore.put(await message.hash(), message.encoded)
|
|
131
|
-
}
|
|
132
|
-
if (!message) {
|
|
133
|
-
message = await contractStore.get(parameters[0])
|
|
134
|
-
message = await new ContractMessage(message)
|
|
135
|
-
}
|
|
136
|
-
if (!await this.has(await message.hash())) await this.#runContract(message)
|
|
137
|
-
}
|
|
138
|
-
} catch (error) {
|
|
139
|
-
throw new Error(`contract deployment failed for ${parameters[0]}\n${error.message}`)
|
|
140
|
-
}
|
|
141
|
-
return new Promise((resolve, reject) => {
|
|
142
|
-
const id = randombytes(20).toString('hex')
|
|
143
|
-
const onmessage = message => {
|
|
144
|
-
pubsub.unsubscribe(id, onmessage)
|
|
145
|
-
if (message?.error) reject(message.error)
|
|
146
|
-
else resolve(message)
|
|
147
|
-
}
|
|
148
|
-
pubsub.subscribe(id, onmessage)
|
|
149
|
-
this.worker.postMessage({
|
|
150
|
-
type: 'execute',
|
|
151
|
-
id,
|
|
152
|
-
input: {
|
|
153
|
-
contract,
|
|
154
|
-
method,
|
|
155
|
-
params: parameters
|
|
156
|
-
}
|
|
157
|
-
})
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
get(contract, method, parameters) {
|
|
163
|
-
return new Promise((resolve, reject) => {
|
|
164
|
-
const id = randombytes(20).toString()
|
|
165
|
-
const onmessage = message => {
|
|
166
|
-
pubsub.unsubscribe(id, onmessage)
|
|
167
|
-
resolve(message)
|
|
168
|
-
}
|
|
169
|
-
pubsub.subscribe(id, onmessage)
|
|
170
|
-
this.worker.postMessage({
|
|
171
|
-
type: 'get',
|
|
172
|
-
id,
|
|
173
|
-
input: {
|
|
174
|
-
contract,
|
|
175
|
-
method,
|
|
176
|
-
params: parameters
|
|
177
|
-
}
|
|
178
|
-
})
|
|
179
|
-
})
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
async has(address) {
|
|
184
|
-
return new Promise((resolve, reject) => {
|
|
185
|
-
const id = randombytes(20).toString('hex')
|
|
186
|
-
const onmessage = message => {
|
|
187
|
-
pubsub.unsubscribe(id, onmessage)
|
|
188
|
-
if (message?.error) reject(message.error)
|
|
189
|
-
else resolve(message)
|
|
190
|
-
}
|
|
191
|
-
pubsub.subscribe(id, onmessage)
|
|
192
|
-
this.worker.postMessage({
|
|
193
|
-
type: 'has',
|
|
194
|
-
id,
|
|
195
|
-
input: {
|
|
196
|
-
address
|
|
197
|
-
}
|
|
198
|
-
})
|
|
199
|
-
})
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async delete(hash) {
|
|
203
|
-
return contractStore.delete(hash)
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
*
|
|
208
|
-
* @returns Promise
|
|
209
|
-
*/
|
|
210
|
-
async deleteAll() {
|
|
211
|
-
let hashes = await contractStore.get()
|
|
212
|
-
hashes = Object.keys(hashes).map(hash => this.delete(hash))
|
|
213
|
-
return Promise.all(hashes)
|
|
214
|
-
}
|
|
215
|
-
}
|
package/src/node.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// import config from './config/config'
|
|
2
|
-
import Peernet from '@leofcoin/peernet'
|
|
3
|
-
import nodeConfig from '@leofcoin/lib/node-config';
|
|
4
|
-
import networks from '@leofcoin/networks';
|
|
5
|
-
|
|
6
|
-
export default class Node {
|
|
7
|
-
constructor() {
|
|
8
|
-
return this._init()
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async _init(config = {
|
|
12
|
-
network: 'leofcoin:peach',
|
|
13
|
-
networkName: 'leofcoin:peach',
|
|
14
|
-
networkVersion: 'peach',
|
|
15
|
-
stars: networks.leofcoin.peach.stars
|
|
16
|
-
}) {
|
|
17
|
-
globalThis.Peernet ? await new globalThis.Peernet(config) : await new Peernet(config)
|
|
18
|
-
await nodeConfig(config)
|
|
19
|
-
|
|
20
|
-
return this
|
|
21
|
-
// this.config = await config()
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
}
|
package/src/protocol.js
DELETED
package/src/state.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import pako from 'pako'
|
|
2
|
-
|
|
3
|
-
export default class State {
|
|
4
|
-
constructor() {
|
|
5
|
-
// return this.#init()
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
// async #init() {
|
|
9
|
-
// const state = await stateStore.get()
|
|
10
|
-
// for (const [key, value] of Object.entries(state)) {
|
|
11
|
-
//
|
|
12
|
-
// }
|
|
13
|
-
//
|
|
14
|
-
// return this
|
|
15
|
-
// }
|
|
16
|
-
|
|
17
|
-
async put(key, value, isCompressed = true) {
|
|
18
|
-
value = isCompressed ? value : await pako.deflate(value)
|
|
19
|
-
await stateStore.put(key, value)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async get(key, isCompressed = true) {
|
|
23
|
-
const value = await stateStore.get(key)
|
|
24
|
-
return isCompressed = true ? pako.inflate(value) : value
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
updateState(block) {
|
|
28
|
-
// block.decoded.index
|
|
29
|
-
// this.#isUpdateNeeded()
|
|
30
|
-
}
|
|
31
|
-
}
|
package/src/transaction.js
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import Protocol from "./protocol.js"
|
|
2
|
-
import MultiWallet from '@leofcoin/multi-wallet'
|
|
3
|
-
import {CodecHash} from '@leofcoin/codec-format-interface'
|
|
4
|
-
import bs32 from '@vandeurenglenn/base32'
|
|
5
|
-
import { TransactionMessage, BlockMessage } from "@leofcoin/messages"
|
|
6
|
-
import { calculateFee } from '@leofcoin/lib'
|
|
7
|
-
import { formatBytes } from '@leofcoin/utils'
|
|
8
|
-
|
|
9
|
-
export default class Transaction extends Protocol {
|
|
10
|
-
constructor() {
|
|
11
|
-
super()
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
* @param {Address[]} transactions
|
|
17
|
-
* @returns transactions to include
|
|
18
|
-
*/
|
|
19
|
-
async getTransactions (transactions) {
|
|
20
|
-
return new Promise(async (resolve, reject) => {
|
|
21
|
-
let size = 0
|
|
22
|
-
const _transactions = []
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const promises = await Promise.all(transactions
|
|
26
|
-
.map(async tx => {
|
|
27
|
-
tx = await new TransactionMessage(tx)
|
|
28
|
-
size += tx.encoded.length
|
|
29
|
-
if (!formatBytes(size).includes('MB') || formatBytes(size).includes('MB') && Number(formatBytes(size).split(' MB')[0]) <= 0.75) _transactions.push({...tx.decoded, hash: await tx.hash()})
|
|
30
|
-
else resolve(_transactions)
|
|
31
|
-
}))
|
|
32
|
-
|
|
33
|
-
return resolve(_transactions)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
*
|
|
40
|
-
* @param {Transaction[]} transactions An array containing Transactions
|
|
41
|
-
* @returns {TransactionMessage}
|
|
42
|
-
*/
|
|
43
|
-
async promiseTransactions(transactions) {
|
|
44
|
-
transactions = await Promise.all(transactions.map(tx => new TransactionMessage(tx)))
|
|
45
|
-
return transactions
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
*
|
|
50
|
-
* @param {Transaction[]} transactions An array containing Transactions
|
|
51
|
-
* @returns {Object} {transaction.decoded, transaction.hash}
|
|
52
|
-
*/
|
|
53
|
-
async promiseTransactionsContent(transactions) {
|
|
54
|
-
transactions = await Promise.all(transactions.map(tx => new Promise(async (resolve, reject) => {
|
|
55
|
-
resolve({ ...tx.decoded, hash: await tx.hash() })
|
|
56
|
-
})))
|
|
57
|
-
|
|
58
|
-
return transactions
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* When a nonce isn't found for an address fallback to just checking the transactionnPoolStore
|
|
63
|
-
* @param {Address} address
|
|
64
|
-
* @returns {Number} nonce
|
|
65
|
-
*/
|
|
66
|
-
async #getNonceFallback(address) {
|
|
67
|
-
let transactions = await transactionPoolStore.values(this.transactionLimit)
|
|
68
|
-
transactions = await this.promiseTransactions(transactions)
|
|
69
|
-
transactions = transactions.filter(tx => tx.decoded.from === address)
|
|
70
|
-
transactions = await this.promiseTransactionsContent(transactions)
|
|
71
|
-
|
|
72
|
-
if (this.lastBlock?.hash && transactions.length === 0 && this.lastBlock.hash !== '0x0') {
|
|
73
|
-
|
|
74
|
-
let block = await peernet.get(this.lastBlock.hash)
|
|
75
|
-
block = await new BlockMessage(block)
|
|
76
|
-
|
|
77
|
-
// for (let tx of block.decoded?.transactions) {
|
|
78
|
-
// tx = await peernet.get(tx, 'transaction')
|
|
79
|
-
// transactions.push(new TransactionMessage(tx))
|
|
80
|
-
// }
|
|
81
|
-
transactions = transactions.filter(tx => tx.from === address)
|
|
82
|
-
while (transactions.length === 0 && block.decoded.index !== 0 && block.decoded.previousHash !== '0x0') {
|
|
83
|
-
block = await blockStore.get(block.decoded.previousHash)
|
|
84
|
-
block = await new BlockMessage(block)
|
|
85
|
-
transactions = block.decoded.transactions.filter(tx => tx.from === address)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
if (transactions.length === 0) return 0
|
|
90
|
-
|
|
91
|
-
transactions = transactions.sort((a, b) => a.timestamp - b.timestamp)
|
|
92
|
-
return transactions[transactions.length - 1].nonce
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Get amount of transactions by address
|
|
97
|
-
* @param {Address} address The address to get the nonce for
|
|
98
|
-
* @returns {Number} nonce
|
|
99
|
-
*/
|
|
100
|
-
async getNonce(address) {
|
|
101
|
-
if (!await accountsStore.has(address)) {
|
|
102
|
-
const nonce = await this.#getNonceFallback(address)
|
|
103
|
-
await accountsStore.put(address, new TextEncoder().encode(String(nonce)))
|
|
104
|
-
}
|
|
105
|
-
// todo: are those in the pool in cluded also ? they need to be included!!!
|
|
106
|
-
let nonce = await accountsStore.get(address)
|
|
107
|
-
nonce = new TextDecoder().decode(nonce)
|
|
108
|
-
return Number(nonce)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* whenever method = createContract params should hold the contract hash
|
|
113
|
-
*
|
|
114
|
-
* example: [hash]
|
|
115
|
-
* createTransaction('0x0', 'createContract', [hash])
|
|
116
|
-
*
|
|
117
|
-
* @param {String} to - the contract address for the contract to interact with
|
|
118
|
-
* @param {String} method - the method/function to run
|
|
119
|
-
* @param {Array} params - array of paramters to apply to the contract method
|
|
120
|
-
* @param {Number} nonce - total transaction count [optional]
|
|
121
|
-
*/
|
|
122
|
-
async createTransaction(to, method, parameters, nonce, signature) {
|
|
123
|
-
return this.createTransactionFrom(peernet.selectedAccount, to, method, parameters, nonce)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
*
|
|
128
|
-
* @param {Transaction} transaction
|
|
129
|
-
* @param {String} transaction.from address
|
|
130
|
-
* @param {String} transaction.to address
|
|
131
|
-
* @param {Object} transaction.params {}
|
|
132
|
-
* @param {String} transaction.params.method get, call
|
|
133
|
-
* @param {Buffer} transaction.params.data
|
|
134
|
-
* @returns
|
|
135
|
-
*/
|
|
136
|
-
async createTransactionHash(transaction) {
|
|
137
|
-
// todo: validate
|
|
138
|
-
const peernetHash = await new CodecHash(transaction, {name: 'transaction-message'})
|
|
139
|
-
return peernetHash.digest
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* @param {Transaction} transaction
|
|
144
|
-
* @param {object} wallet any wallet/signer that supports sign(RAWtransaction)
|
|
145
|
-
*/
|
|
146
|
-
async #signTransaction (transaction, wallet) {
|
|
147
|
-
return wallet.sign(await this.createTransactionHash(transaction))
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
*
|
|
152
|
-
* @param {RawTransaction} transaction
|
|
153
|
-
* @param {Signer} signer
|
|
154
|
-
* @returns {Transaction} a signed transaction
|
|
155
|
-
*/
|
|
156
|
-
async signTransaction(transaction, signer) {
|
|
157
|
-
let identity = await walletStore.get('identity')
|
|
158
|
-
identity = JSON.parse(new TextDecoder().decode(identity))
|
|
159
|
-
const wallet = new MultiWallet(peernet.network)
|
|
160
|
-
await wallet.recover(identity.mnemonic)
|
|
161
|
-
const account = await wallet.account(0).external(0)
|
|
162
|
-
transaction.signature = await this.#signTransaction(transaction, account)
|
|
163
|
-
return transaction
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
*
|
|
168
|
-
* @param {Transaction} transaction
|
|
169
|
-
* @param {Address} transaction.from
|
|
170
|
-
* @param {Address} transaction.to
|
|
171
|
-
* @param {String} transaction.method
|
|
172
|
-
* @param {Array} transaction.params
|
|
173
|
-
* @param {Number} transaction.nonce
|
|
174
|
-
*
|
|
175
|
-
* @returns {RawTransaction} transaction
|
|
176
|
-
*/
|
|
177
|
-
async ensureNonce(transaction) {
|
|
178
|
-
if (!transaction.from) transaction.from = peernet.selectedAccount
|
|
179
|
-
transaction.timestamp = Date.now()
|
|
180
|
-
|
|
181
|
-
if (transaction.nonce === undefined) {
|
|
182
|
-
transaction.nonce = await this.getNonce(transaction.from)
|
|
183
|
-
} else {
|
|
184
|
-
let nonce = await accountsStore.get(transaction.from)
|
|
185
|
-
nonce = new TextDecoder().decode(nonce)
|
|
186
|
-
if (transaction.nonce < nonce) throw new Error(`a transaction with a higher nonce already exists`)
|
|
187
|
-
if (transaction.nonce === nonce) throw new Error(`a transaction with the same nonce already exists`)
|
|
188
|
-
}
|
|
189
|
-
return transaction
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* every tx done is trough contracts so no need for amount
|
|
194
|
-
* data is undefined when nothing is returned
|
|
195
|
-
* error is thrown on error so undefined data doesn't mean there is an error...
|
|
196
|
-
*
|
|
197
|
-
* @param {Address} from - the sender address
|
|
198
|
-
* @param {Address} to - the contract address for the contract to interact with
|
|
199
|
-
* @param {String} method - the method/function to run
|
|
200
|
-
* @param {Array} params - array of paramters to apply to the contract method
|
|
201
|
-
* @param {Number} nonce - total transaction count [optional]
|
|
202
|
-
*/
|
|
203
|
-
async createTransactionFrom(from, to, method, parameters, nonce) {
|
|
204
|
-
try {
|
|
205
|
-
const rawTransaction = await this.ensureNonce({from, to, nonce, method, params: parameters})
|
|
206
|
-
const transaction = await this.signTransaction(rawTransaction, from)
|
|
207
|
-
const message = await new TransactionMessage(transaction)
|
|
208
|
-
const hash = await message.hash()
|
|
209
|
-
|
|
210
|
-
let data
|
|
211
|
-
const wait = new Promise(async (resolve, reject) => {
|
|
212
|
-
if (pubsub.subscribers[`transaction.completed.${hash}`]) {
|
|
213
|
-
const result = pubsub.subscribers[`transaction.completed.${hash}`].value
|
|
214
|
-
result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error})
|
|
215
|
-
} else {
|
|
216
|
-
const completed = async result => {
|
|
217
|
-
result.status === 'fulfilled' ? resolve(result.hash) : reject({hash: result.hash, error: result.error})
|
|
218
|
-
|
|
219
|
-
setTimeout(async () => {
|
|
220
|
-
pubsub.unsubscribe(`transaction.completed.${hash}`, completed)
|
|
221
|
-
}, 10_000)
|
|
222
|
-
}
|
|
223
|
-
pubsub.subscribe(`transaction.completed.${hash}`, completed)
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
|
-
await transactionPoolStore.put(hash, message.encoded)
|
|
227
|
-
debug(`Added ${hash} to the transaction pool`)
|
|
228
|
-
peernet.publish('add-transaction', message.encoded)
|
|
229
|
-
return {hash: hash, data, fee: await calculateFee(message.decoded), wait, message}
|
|
230
|
-
} catch (error) {
|
|
231
|
-
throw error
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
package/src/type.index.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
type Address = string
|
|
2
|
-
|
|
3
|
-
interface Transaction {
|
|
4
|
-
to: Address,
|
|
5
|
-
from: Address,
|
|
6
|
-
method: String,
|
|
7
|
-
params: string[],
|
|
8
|
-
nonce: Number
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface RawTransaction extends Transaction {
|
|
12
|
-
timestamp: Number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface globalMessage {
|
|
16
|
-
sender: Address,
|
|
17
|
-
call: Function,
|
|
18
|
-
staticCall: Function,
|
|
19
|
-
delegate: Function,
|
|
20
|
-
staticDelegate: Function
|
|
21
|
-
}
|
package/src/typer.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import {BigNumber} from '@leofcoin/utils'
|
|
2
|
-
|
|
3
|
-
const codec = '0x'
|
|
4
|
-
|
|
5
|
-
const isAddress = address => {
|
|
6
|
-
if (!address.startsWith(codec)) return false
|
|
7
|
-
if (address.length !== 64) return false
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export default {
|
|
11
|
-
isType: (type, value) => {
|
|
12
|
-
type = type.toLowercase()
|
|
13
|
-
if (type === 'string') return typeof(value) === type
|
|
14
|
-
if (type === 'number') return !Number.isNaN(Number(value))
|
|
15
|
-
if (type === 'address') return isAddress(value)
|
|
16
|
-
if (type === 'BigNumber') return BigNumber.isBigNumber(value)
|
|
17
|
-
},
|
|
18
|
-
isAddress
|
|
19
|
-
}
|