forked from aeternity/aepp-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
127 lines (118 loc) · 4.5 KB
/
Copy pathnode.js
File metadata and controls
127 lines (118 loc) · 4.5 KB
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
124
125
126
127
/*
* ISC License (ISC)
* Copyright (c) 2021 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* Node module
* @module @aeternity/aepp-sdk/es/node
* @export Node
* @example import Node from '@aeternity/aepp-sdk/es/node'
*/
import AsyncInit from './utils/async-init'
import genSwaggerClient from './utils/swagger'
import semverSatisfies from './utils/semver-satisfies'
/**
* Obtain networkId from account or node
* @instance
* @category async
* @rtype () => networkId: String
* @return {String} NetworkId
*/
export function getNetworkId ({ networkId, force = false } = {}) {
if (!force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) throw new Error('networkId is not provided')
if (force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) return null
return networkId || this.networkId || this.selectedNode.networkId
}
/**
* get consensus protocol version
* @param {Array} protocols Array of protocols
* @param {Number} height Height
* @return {Number} version Protocol version
*/
async function getConsensusProtocolVersion (protocols = [], height) {
if (!protocols) throw new Error('Protocols must be an array')
if (!height) height = (await this.api.getCurrentKeyBlock()).height
if (height < 0) throw new Error('height must be a number >= 0')
const { version } = protocols
.reduce(
({ effectiveAtHeight, version }, p) => height >= p.effectiveAtHeight && p.effectiveAtHeight > effectiveAtHeight
? { effectiveAtHeight: p.effectiveAtHeight, version: p.version }
: { effectiveAtHeight, version },
{ effectiveAtHeight: -1, version: 0 }
)
return version
}
/**
* {@link genSwaggerClient} based Node remote API Stamp
* @function
* @alias module:@aeternity/aepp-sdk/es/node
* @rtype Stamp
* @param {Object} [options={}] - Options
* @param {String} options.url - Base URL for Node
* @param {String} options.internalUrl - Base URL for internal requests
* @param {String} options.axiosConfig - Object with axios configuration. Example { config: {}, errorHandler: (err) => throw err }
* @return {Object} Node client
* @example Node({url: 'https://testnet.aeternity.io'})
*/
const Node = AsyncInit.compose({
async init ({ url = this.url, internalUrl = this.internalUrl }) {
if (!url) throw new Error('"url" required')
url = url.replace(/\/?$/, '')
internalUrl = internalUrl ? internalUrl.replace(/\/?$/, '') : url
const client = await genSwaggerClient(`${url}/api`, internalUrl)
this.version = client.spec.info.version
this.api = client.api
},
methods: {
getNodeInfo () {
return {
url: this.url,
internalUrl: this.internalUrl,
nodeNetworkId: this.nodeNetworkId,
version: this.version,
consensusProtocolVersion: this.consensusProtocolVersion
}
},
getConsensusProtocolVersion
},
props: {
version: null,
consensusProtocolVersion: null,
nodeNetworkId: null
}
}, {
async init ({ forceCompatibility = false }) {
const { nodeRevision: revision, genesisKeyBlockHash: genesisHash, networkId, protocols } = await this.api.getStatus()
this.consensusProtocolVersion = await this.getConsensusProtocolVersion(protocols)
if (
(
!semverSatisfies(this.version.split('-')[0], NODE_GE_VERSION, NODE_LT_VERSION) ||
this.version === '5.0.0-rc1'
) &&
// Todo implement 'rc' version comparision in semverSatisfies
!forceCompatibility
) {
throw new Error(
`Unsupported node version ${this.version}. ` +
`Supported: >= ${NODE_GE_VERSION} < ${NODE_LT_VERSION}`
)
}
this.nodeNetworkId = networkId
return Object.assign(this, { revision, genesisHash })
}
})
const NODE_GE_VERSION = '5.0.0'
const NODE_LT_VERSION = '6.0.0'
export default Node