-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistry.js
132 lines (119 loc) · 4.87 KB
/
registry.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
124
125
126
127
128
129
130
131
132
const { getTLD, getTLDSpec, connectToNetwork, getJSONResponse, connector } = require("../util");
const { TxType } = require('../util');
const { createResolver } = require("./resolver");
const { createTransaction } = require("../transaction/transaction");
/**
* Registry class provides methods to interact with blockchain networks
* for registering assets, TLDs, and domains.
*/
class Registry {
/**
* Constructs a new Registry instance.
*
* @param {string} rootNetworkSpecUrl - URL to the root network's chain specification.
* @param {string} phrase - Mnemonic phrase used for signing transactions.
*/
constructor(rootNetworkSpecUrl, phrase) {
this.rootNetworkSpecUrl = rootNetworkSpecUrl;
this.phrase = phrase;
this.rootSpec = null; // Holds the chain specification of the root network.
}
/**
* Initializes the Registry by fetching the root network's chain specification.
*
* @returns {Promise<void>}
* @throws {Error} If fetching the chain specification fails.
*/
async init() {
this.rootSpec = await getJSONResponse(this.rootNetworkSpecUrl);
}
/**
* Registers a new asset on a blockchain associated with a specific domain.
*
* @param {string} domain - Domain representing the blockchain network.
* @param {string} assetId - Unique identifier for the asset.
* @param {number} amount - Minimum balance required for the asset.
* @returns {Promise<void>}
* @throws {Error} If the domain network connection or transaction fails.
*/
async registerAsset(domain, assetId, amount) {
// Create a resolver to find the chain specification for the domain.
let resolver = await createResolver(this.rootNetworkSpecUrl);
await resolver.init();
// Resolve the chain specification for the domain.
let chainSpec = await resolver.resolve(domain);
// Connect to the domain's blockchain network.
let api = await connector.connectToNetwork(chainSpec.targetSpec);
// Create and send the transaction to register the asset.
await createTransaction(TxType.TX_ASSET_CREATE, {
assetId: assetId,
minBalance: amount
}, api, this.phrase).sendTransaction();
}
/**
* Registers a new TLD (Top-Level Domain) on the root network.
*
* @param {string} tld - The TLD to register.
* @param {object} tldSpec - Chain specification for the TLD.
* @returns {Promise<void>}
* @throws {Error} If the root network connection or transaction fails.
*/
async registerTLD(tld, tldSpec) {
try {
// Connect to the root network.
let api = await connector.connectToNetwork(this.rootSpec);
// Create and send the transaction to register the TLD.
await createTransaction(TxType.TX_ROOT, {
target: tld,
targetSpec: tldSpec
}, api, this.phrase).sendTransaction();
} catch (error) {
// Handle specific connection errors or propagate other errors.
if (error.toString() === "CONNECTION_ERROR") {
throw new Error("Could not connect to the root network.");
} else {
throw error;
}
}
}
/**
* Registers a new domain under a specified TLD.
*
* @param {string} domain - The domain to register.
* @param {object} domainSpec - Chain specification for the domain.
* @returns {Promise<void>}
* @throws {Error} If the TLD network connection or transaction fails.
*/
async registerDomain(domain, domainSpec) {
try {
// Extract the TLD from the domain.
let tld = getTLD(domain);
// Fetch the chain specification for the TLD.
let tldSpec = await getTLDSpec(tld, this.rootSpec);
// Connect to the TLD's blockchain network.
let api = await connector.connectToNetwork(tldSpec);
// Create and send the transaction to register the domain.
await createTransaction(TxType.TX_TLD, {
target: domain,
targetSpec: domainSpec
}, api, this.phrase).sendTransaction();
} catch (error) {
// Handle specific connection errors or propagate other errors.
if (error.toString() === "CONNECTION_ERROR") {
throw new Error("Could not connect to the TLD network.");
} else {
throw error;
}
}
}
}
/**
* Factory function to create a new Registry instance.
*
* @param {string} rootSpecAddr - URL of the root network's chain specification.
* @param {string} phrase - Mnemonic phrase for signing transactions.
* @returns {Registry} A new Registry instance.
*/
exports.createRegistry = (rootSpecAddr, phrase) => {
return new Registry(rootSpecAddr, phrase);
};