-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
120 lines (113 loc) · 4.18 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { signTransaction, transactionDigest } from './transactions/signTransaction.js'
import { createTransaction } from './transactions/createTransaction.js'
import { broadcastTransaction } from './transactions/broadcastTransaction.js'
import { PrivateKey } from './helpers/PrivateKey.js'
import { PublicKey } from './helpers/PublicKey.js'
import { Signature } from './helpers/Signature.js'
import { call } from './helpers/call.js'
import { config } from './config.js'
import { Memo } from './helpers/memo.js'
import * as utils from './helpers/utils.js'
/** Transaction for Hive blockchain */
class Transaction {
/** A transaction object could be passed or created later
* @param {{}} trx Object of transaction - Optional
*/
constructor (trx = null) {
this.created = true
if (!trx) {
this.created = false
}
this.transaction = trx
}
/** Create the transaction by operations
* @param {[Array]} operations
* @param {Number} expiration Optional - Default 60 seconds
*/
async create (operations, expiration = 60) {
this.transaction = await createTransaction(operations, expiration)
this.created = true
return this.transaction
}
/** Sign the transaction by key or keys[] (supports multi signature).
* It is also possible to sign with one key at a time for multi signature.
* @param {PrivateKey|[PrivateKey]} keys single key or multiple keys in array
*/
sign (keys) {
if (!this.created) {
throw new Error('First create a transaction by .create(operations)')
}
if (this.signedTransaction) {
const { signedTransaction, txId } = signTransaction(this.signedTransaction, keys)
this.signedTransaction = signedTransaction
this.txId = txId
} else {
const { signedTransaction, txId } = signTransaction(this.transaction, keys)
this.signedTransaction = signedTransaction
this.txId = txId
}
return this.signedTransaction
}
/** Broadcast the signed transaction. */
async broadcast (timeout = 5, retry = 5) {
if (!this.created) {
throw new Error('First create a transaction by .create(operations)')
}
if (!this.signedTransaction) {
throw new Error('First sign the transaction by .sign(keys)')
}
const result = await broadcastTransaction(this.signedTransaction, timeout, retry)
if (result.error) {
// When we retry, we might have already broadcasted the transaction
// So catch duplicate trx error and return trx id
if (result.error.message.includes('Duplicate transaction check failed')) {
return {
id: 1,
jsonrpc: '2.0',
result: { tx_id: this.txId, status: 'unkown' }
}
}
return result
}
if (!this.txId) {
this.txId = this.digest().txId
}
return {
id: 1,
jsonrpc: '2.0',
result: { tx_id: this.txId, status: 'unkown' }
}
}
/** Return the transaction hash which can be used to verify against a signature */
digest () {
if (!this.created) {
throw new Error('First create a transaction by .create(operations)')
}
return transactionDigest(this.transaction)
}
/**
* Add a signature to already created transaction. You can add multiple signatures to one transaction but one at a time.
* This method is used when you sign your transaction with other tools instead of built-in .sign() method.
*/
addSignature (signature = '') {
if (!this.created) {
throw new Error('First create a transaction by .create(operations)')
}
if (typeof signature !== 'string') {
throw new Error('Signature must be string')
}
if (signature.length !== 130) {
throw new Error('Signature must be 130 characters long')
}
if (!this.signedTransaction) {
this.signedTransaction = { ...this.transaction }
}
if (Array.isArray(this.signedTransaction.signature)) {
this.signedTransaction.signatures.push(signature)
} else {
this.signedTransaction.signatures = [signature]
}
return this.signedTransaction
}
}
export { Transaction, PrivateKey, call, config, PublicKey, Signature, Memo, utils }