-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
123 lines (104 loc) · 3.27 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
121
122
123
/* Import (local) modules. */
import { decodeAddress as _decodeAddress } from './src/cashaddr.js'
import _decodeBase58AddressFormat from './src/decodeBase58AddressFormat.js'
import { encodeAddress as _encodeAddress } from './src/cashaddr.js'
import _encodeBase58AddressFormat from './src/encodeBase58AddressFormat.js'
import getSeedType from './src/getSeedType.js'
import _getSender from './src/getSender.js'
import _listUnspent from './src/listUnspent.js'
import _watchAddress from './src/watchAddress.js'
/* Export (local) modules. */
export const decodeAddress = _decodeAddress
export const decodeBase58AddressFormat = _decodeBase58AddressFormat
export const encodeAddress = _encodeAddress
export const encodeBase58AddressFormat = _encodeBase58AddressFormat
export const getSender = _getSender
export const listUnspent = _listUnspent
export const watchAddress = _watchAddress
/**
* Address Class
*
* Manages address functions.
*/
export class Address {
constructor(_params, _notif) {
/* Initialize Address class. */
// console.info('Initializing Address...')
// console.log(JSON.stringify(_params, null, 2))
/* Handle notifications. */
if (_params?.notif) {
notif = _params.notif
} else if (_notif) {
notif = _notif
}
/* Set (address) seed. */
this._seed = _params?.seed
/* Validate seed. */
if (!this._seed) {
/* Set (address) seed. */
this._seed = _params
}
/* Set seed type. */
this._seedType = getSeedType(_params)
/* Validate seed type. */
if (!this._seedType) {
throw new Error(`Oops! Invalid address seed.`)
}
}
test() {
testAddr()
}
decode(_addr) {
return decodeAddress(_addr)
}
static decode(_addr) {
return decodeAddress(_addr)
}
static getSender(_input) {
return getSender(_input)
}
encode(_prefix, _type, _hash) {
return encodeAddress(_prefix, _type, _hash)
}
static encode(_prefix, _type, _hash) {
return encodeAddress(_prefix, _type, _hash)
}
/**
* To String
*
* Serializes (or marshalls) the Address details into a JSON package.
*
* (optional) Set formatting to (true) for "pretty" output styling.
*/
toString(_formatted = false) {
/* Build Address (data) package. */
const pkg = {
seed: this._seed,
seedType: this._seedType,
}
/* Handle formatting flag. */
if (_formatted) {
return JSON.stringify(pkg, null, 2)
} else {
return pkg
}
}
}
/* Initialize (globalThis) Nexa class. */
const Nexa = {}
/* Initialize Address class. */
Nexa.Address = Address
/* Initialize Address modules. */
Nexa.decodeAddress = decodeAddress
Nexa.decodeBase58AddressFormat = decodeBase58AddressFormat
Nexa.encodeAddress = encodeAddress
Nexa.encodeBase58AddressFormat = encodeBase58AddressFormat
Nexa.getSender = getSender
Nexa.listUnspent = listUnspent
Nexa.watchAddress = watchAddress
/* Export Nexa to globalThis. */
// NOTE: We merge to avoid conflict with other libraries.
globalThis.Nexa = {
...globalThis.Nexa, // preserve Nexa object
...Nexa, // extend Nexa object
}