@twin.org/nft-service 0.0.1-next.10
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/LICENSE +201 -0
- package/README.md +21 -0
- package/dist/cjs/index.cjs +465 -0
- package/dist/esm/index.mjs +455 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/models/INftServiceConfig.d.ts +9 -0
- package/dist/types/models/INftServiceConstructorOptions.d.ts +10 -0
- package/dist/types/nftRoutes.d.ts +53 -0
- package/dist/types/nftService.d.ts +68 -0
- package/dist/types/restEntryPoints.d.ts +2 -0
- package/docs/changelog.md +5 -0
- package/docs/examples.md +1 -0
- package/docs/open-api/spec.json +685 -0
- package/docs/reference/classes/NftService.md +269 -0
- package/docs/reference/functions/generateRestRoutesNft.md +25 -0
- package/docs/reference/functions/nftBurn.md +31 -0
- package/docs/reference/functions/nftMint.md +31 -0
- package/docs/reference/functions/nftResolve.md +31 -0
- package/docs/reference/functions/nftTransfer.md +31 -0
- package/docs/reference/functions/nftUpdate.md +31 -0
- package/docs/reference/index.md +24 -0
- package/docs/reference/interfaces/INftServiceConfig.md +11 -0
- package/docs/reference/interfaces/INftServiceConstructorOptions.md +11 -0
- package/docs/reference/variables/restEntryPoints.md +3 -0
- package/docs/reference/variables/tagsNft.md +5 -0
- package/locales/en.json +13 -0
- package/package.json +42 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { Guards, ComponentFactory, GeneralError, Urn } from '@twin.org/core';
|
|
2
|
+
import { HttpStatusCode, HeaderTypes } from '@twin.org/web';
|
|
3
|
+
import { NftConnectorFactory } from '@twin.org/nft-models';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The source used when communicating about these routes.
|
|
7
|
+
*/
|
|
8
|
+
const ROUTES_SOURCE = "nftRoutes";
|
|
9
|
+
/**
|
|
10
|
+
* The tag to associate with the routes.
|
|
11
|
+
*/
|
|
12
|
+
const tagsNft = [
|
|
13
|
+
{
|
|
14
|
+
name: "NFT",
|
|
15
|
+
description: "Endpoints which are modelled to access an NFT contract."
|
|
16
|
+
}
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* The REST routes for NFT.
|
|
20
|
+
* @param baseRouteName Prefix to prepend to the paths.
|
|
21
|
+
* @param componentName The name of the component to use in the routes stored in the ComponentFactory.
|
|
22
|
+
* @returns The generated routes.
|
|
23
|
+
*/
|
|
24
|
+
function generateRestRoutesNft(baseRouteName, componentName) {
|
|
25
|
+
const mintRoute = {
|
|
26
|
+
operationId: "nftMint",
|
|
27
|
+
summary: "Mint an NFT",
|
|
28
|
+
tag: tagsNft[0].name,
|
|
29
|
+
method: "POST",
|
|
30
|
+
path: `${baseRouteName}/`,
|
|
31
|
+
handler: async (httpRequestContext, request) => nftMint(httpRequestContext, componentName, request),
|
|
32
|
+
requestType: {
|
|
33
|
+
type: "INftMintRequest",
|
|
34
|
+
examples: [
|
|
35
|
+
{
|
|
36
|
+
id: "nftMintExample",
|
|
37
|
+
request: {
|
|
38
|
+
body: {
|
|
39
|
+
issuer: "tst1prctjk5ck0dutnsunnje6u90jk5htx03qznjjmkd6843pzltlgz87srjzzv",
|
|
40
|
+
tag: "MY-NFT",
|
|
41
|
+
immutableMetadata: {
|
|
42
|
+
docName: "bill-of-lading",
|
|
43
|
+
mimeType: "application/pdf",
|
|
44
|
+
fingerprint: "0xf0b95a98b3dbc5ce1c9ce59d70af95a97599f100a7296ecdd1eb108bebfa047f"
|
|
45
|
+
},
|
|
46
|
+
metadata: {
|
|
47
|
+
data: "tst1prctjk5ck0dutnsunnje6u90jk5htx03qznjjmkd6843pzltlgz87srjzzv"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
responseType: [
|
|
55
|
+
{
|
|
56
|
+
type: "ICreatedResponse",
|
|
57
|
+
examples: [
|
|
58
|
+
{
|
|
59
|
+
id: "nftMintResponseExample",
|
|
60
|
+
response: {
|
|
61
|
+
statusCode: HttpStatusCode.created,
|
|
62
|
+
headers: {
|
|
63
|
+
[HeaderTypes.Location]: "nft:iota:aW90YS1uZnQ6dHN0OjB4NzYyYjljNDllYTg2OWUwZWJkYTliYmZhNzY5Mzk0NDdhNDI4ZGNmMTc4YzVkMTVhYjQ0N2UyZDRmYmJiNGViMg=="
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
};
|
|
71
|
+
const resolveRoute = {
|
|
72
|
+
operationId: "nftResolve",
|
|
73
|
+
summary: "Resolve an NFT",
|
|
74
|
+
tag: tagsNft[0].name,
|
|
75
|
+
method: "GET",
|
|
76
|
+
path: `${baseRouteName}/:id`,
|
|
77
|
+
handler: async (httpRequestContext, request) => nftResolve(httpRequestContext, componentName, request),
|
|
78
|
+
requestType: {
|
|
79
|
+
type: "INftResolveRequest",
|
|
80
|
+
examples: [
|
|
81
|
+
{
|
|
82
|
+
id: "nftResolveExample",
|
|
83
|
+
request: {
|
|
84
|
+
pathParams: {
|
|
85
|
+
id: "nft:iota:aW90YS1uZnQ6dHN0OjB4NzYyYjljNDllYTg2OWUwZWJkYTliYmZhNzY5Mzk0NDdhNDI4ZGNmMTc4YzVkMTVhYjQ0N2UyZDRmYmJiNGViMg=="
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
},
|
|
91
|
+
responseType: [
|
|
92
|
+
{
|
|
93
|
+
type: "INftResolveResponse",
|
|
94
|
+
examples: [
|
|
95
|
+
{
|
|
96
|
+
id: "nftResolveResponseExample",
|
|
97
|
+
response: {
|
|
98
|
+
body: {
|
|
99
|
+
issuer: "tst1prctjk5ck0dutnsunnje6u90jk5htx03qznjjmkd6843pzltlgz87srjzzv",
|
|
100
|
+
owner: "tst1prctjk5ck0dutnsunnje6u90jk5htx03qznjjmkd6843pzltlgz87srjzzv",
|
|
101
|
+
tag: "MY-NFT",
|
|
102
|
+
immutableMetadata: {
|
|
103
|
+
docName: "bill-of-lading",
|
|
104
|
+
mimeType: "application/pdf",
|
|
105
|
+
fingerprint: "0xf0b95a98b3dbc5ce1c9ce59d70af95a97599f100a7296ecdd1eb108bebfa047f"
|
|
106
|
+
},
|
|
107
|
+
metadata: {
|
|
108
|
+
data: "AAAAA"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
};
|
|
117
|
+
const burnRoute = {
|
|
118
|
+
operationId: "nftBurn",
|
|
119
|
+
summary: "Burn an NFT",
|
|
120
|
+
tag: tagsNft[0].name,
|
|
121
|
+
method: "DELETE",
|
|
122
|
+
path: `${baseRouteName}/:id`,
|
|
123
|
+
handler: async (httpRequestContext, request) => nftBurn(httpRequestContext, componentName, request),
|
|
124
|
+
requestType: {
|
|
125
|
+
type: "INftBurnRequest",
|
|
126
|
+
examples: [
|
|
127
|
+
{
|
|
128
|
+
id: "nftBurnExample",
|
|
129
|
+
request: {
|
|
130
|
+
pathParams: {
|
|
131
|
+
id: "nft:iota:aW90YS1uZnQ6dHN0OjB4NzYyYjljNDllYTg2OWUwZWJkYTliYmZhNzY5Mzk0NDdhNDI4ZGNmMTc4YzVkMTVhYjQ0N2UyZDRmYmJiNGViMg=="
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
},
|
|
137
|
+
responseType: [
|
|
138
|
+
{
|
|
139
|
+
type: "INoContentResponse"
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
};
|
|
143
|
+
const transferRoute = {
|
|
144
|
+
operationId: "nftTransfer",
|
|
145
|
+
summary: "Transfer an NFT",
|
|
146
|
+
tag: tagsNft[0].name,
|
|
147
|
+
method: "POST",
|
|
148
|
+
path: `${baseRouteName}/:id/transfer`,
|
|
149
|
+
handler: async (httpRequestContext, request) => nftTransfer(httpRequestContext, componentName, request),
|
|
150
|
+
requestType: {
|
|
151
|
+
type: "INftTransferRequest",
|
|
152
|
+
examples: [
|
|
153
|
+
{
|
|
154
|
+
id: "nftTransferExample",
|
|
155
|
+
request: {
|
|
156
|
+
pathParams: {
|
|
157
|
+
id: "nft:iota:aW90YS1uZnQ6dHN0OjB4NzYyYjljNDllYTg2OWUwZWJkYTliYmZhNzY5Mzk0NDdhNDI4ZGNmMTc4YzVkMTVhYjQ0N2UyZDRmYmJiNGViMg=="
|
|
158
|
+
},
|
|
159
|
+
body: {
|
|
160
|
+
recipient: "tst1prctjk5ck0dutnsunnje6u90jk5htx03qznjjmkd6843pzltlgz87srjzzv",
|
|
161
|
+
metadata: {
|
|
162
|
+
data: "AAAAA"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
]
|
|
168
|
+
},
|
|
169
|
+
responseType: [
|
|
170
|
+
{
|
|
171
|
+
type: "INoContentResponse"
|
|
172
|
+
}
|
|
173
|
+
]
|
|
174
|
+
};
|
|
175
|
+
const updateRoute = {
|
|
176
|
+
operationId: "nftUpdate",
|
|
177
|
+
summary: "Update an NFT",
|
|
178
|
+
tag: tagsNft[0].name,
|
|
179
|
+
method: "PUT",
|
|
180
|
+
path: `${baseRouteName}/:id`,
|
|
181
|
+
handler: async (httpRequestContext, request) => nftUpdate(httpRequestContext, componentName, request),
|
|
182
|
+
requestType: {
|
|
183
|
+
type: "INftUpdateRequest",
|
|
184
|
+
examples: [
|
|
185
|
+
{
|
|
186
|
+
id: "nftUpdateExample",
|
|
187
|
+
request: {
|
|
188
|
+
pathParams: {
|
|
189
|
+
id: "nft:iota:aW90YS1uZnQ6dHN0OjB4NzYyYjljNDllYTg2OWUwZWJkYTliYmZhNzY5Mzk0NDdhNDI4ZGNmMTc4YzVkMTVhYjQ0N2UyZDRmYmJiNGViMg=="
|
|
190
|
+
},
|
|
191
|
+
body: {
|
|
192
|
+
metadata: {
|
|
193
|
+
data: "AAAAA"
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
]
|
|
199
|
+
},
|
|
200
|
+
responseType: [
|
|
201
|
+
{
|
|
202
|
+
type: "INoContentResponse"
|
|
203
|
+
}
|
|
204
|
+
]
|
|
205
|
+
};
|
|
206
|
+
return [mintRoute, resolveRoute, burnRoute, transferRoute, updateRoute];
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Mint an NFT.
|
|
210
|
+
* @param httpRequestContext The request context for the API.
|
|
211
|
+
* @param componentName The name of the component to use in the routes.
|
|
212
|
+
* @param request The request.
|
|
213
|
+
* @returns The response object with additional http response properties.
|
|
214
|
+
*/
|
|
215
|
+
async function nftMint(httpRequestContext, componentName, request) {
|
|
216
|
+
Guards.object(ROUTES_SOURCE, "request", request);
|
|
217
|
+
Guards.object(ROUTES_SOURCE, "request.body", request.body);
|
|
218
|
+
Guards.stringValue(ROUTES_SOURCE, "request.body.issuer", request.body.issuer);
|
|
219
|
+
Guards.stringValue(ROUTES_SOURCE, "request.body.tag", request.body.tag);
|
|
220
|
+
const component = ComponentFactory.get(componentName);
|
|
221
|
+
const id = await component.mint(request.body.issuer, request.body.tag, request.body.immutableMetadata, request.body.metadata, request.body.namespace, httpRequestContext.userIdentity);
|
|
222
|
+
return {
|
|
223
|
+
statusCode: HttpStatusCode.created,
|
|
224
|
+
headers: {
|
|
225
|
+
[HeaderTypes.Location]: id
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Resolve an NFT.
|
|
231
|
+
* @param httpRequestContext The request context for the API.
|
|
232
|
+
* @param componentName The name of the component to use in the routes.
|
|
233
|
+
* @param request The request.
|
|
234
|
+
* @returns The response object with additional http response properties.
|
|
235
|
+
*/
|
|
236
|
+
async function nftResolve(httpRequestContext, componentName, request) {
|
|
237
|
+
Guards.object(ROUTES_SOURCE, "request", request);
|
|
238
|
+
Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
|
|
239
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
240
|
+
const component = ComponentFactory.get(componentName);
|
|
241
|
+
const result = await component.resolve(request.pathParams.id, httpRequestContext.userIdentity);
|
|
242
|
+
return {
|
|
243
|
+
body: result
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Burn an NFT.
|
|
248
|
+
* @param httpRequestContext The request context for the API.
|
|
249
|
+
* @param componentName The name of the component to use in the routes.
|
|
250
|
+
* @param request The request.
|
|
251
|
+
* @returns The response object with additional http response properties.
|
|
252
|
+
*/
|
|
253
|
+
async function nftBurn(httpRequestContext, componentName, request) {
|
|
254
|
+
Guards.object(ROUTES_SOURCE, "request", request);
|
|
255
|
+
Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
|
|
256
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
257
|
+
const component = ComponentFactory.get(componentName);
|
|
258
|
+
await component.burn(request.pathParams.id, httpRequestContext.userIdentity);
|
|
259
|
+
return {
|
|
260
|
+
statusCode: HttpStatusCode.noContent
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Transfer an NFT.
|
|
265
|
+
* @param httpRequestContext The request context for the API.
|
|
266
|
+
* @param componentName The name of the component to use in the routes.
|
|
267
|
+
* @param request The request.
|
|
268
|
+
* @returns The response object with additional http response properties.
|
|
269
|
+
*/
|
|
270
|
+
async function nftTransfer(httpRequestContext, componentName, request) {
|
|
271
|
+
Guards.object(ROUTES_SOURCE, "request", request);
|
|
272
|
+
Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
|
|
273
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
274
|
+
Guards.object(ROUTES_SOURCE, "request.body", request.body);
|
|
275
|
+
Guards.stringValue(ROUTES_SOURCE, "request.body.recipient", request.body.recipient);
|
|
276
|
+
const component = ComponentFactory.get(componentName);
|
|
277
|
+
await component.transfer(request.pathParams.id, request.body.recipient, request.body.metadata, httpRequestContext.userIdentity);
|
|
278
|
+
return {
|
|
279
|
+
statusCode: HttpStatusCode.noContent
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Update an NFT.
|
|
284
|
+
* @param httpRequestContext The request context for the API.
|
|
285
|
+
* @param componentName The name of the component to use in the routes.
|
|
286
|
+
* @param request The request.
|
|
287
|
+
* @returns The response object with additional http response properties.
|
|
288
|
+
*/
|
|
289
|
+
async function nftUpdate(httpRequestContext, componentName, request) {
|
|
290
|
+
Guards.object(ROUTES_SOURCE, "request", request);
|
|
291
|
+
Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
|
|
292
|
+
Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
|
|
293
|
+
Guards.object(ROUTES_SOURCE, "request.body", request.body);
|
|
294
|
+
Guards.object(ROUTES_SOURCE, "request.body.metadata", request.body.metadata);
|
|
295
|
+
const component = ComponentFactory.get(componentName);
|
|
296
|
+
await component.update(request.pathParams.id, request.body.metadata, httpRequestContext.userIdentity);
|
|
297
|
+
return {
|
|
298
|
+
statusCode: HttpStatusCode.noContent
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Copyright 2024 IOTA Stiftung.
|
|
303
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
304
|
+
/**
|
|
305
|
+
* Service for performing NFT operations to a connector.
|
|
306
|
+
*/
|
|
307
|
+
class NftService {
|
|
308
|
+
/**
|
|
309
|
+
* The namespace supported by the nft service.
|
|
310
|
+
*/
|
|
311
|
+
static NAMESPACE = "nft";
|
|
312
|
+
/**
|
|
313
|
+
* Runtime name for the class.
|
|
314
|
+
*/
|
|
315
|
+
CLASS_NAME = "NftService";
|
|
316
|
+
/**
|
|
317
|
+
* The default namespace for the connector to use.
|
|
318
|
+
* @internal
|
|
319
|
+
*/
|
|
320
|
+
_defaultNamespace;
|
|
321
|
+
/**
|
|
322
|
+
* Create a new instance of NftService.
|
|
323
|
+
* @param options The options for the service.
|
|
324
|
+
*/
|
|
325
|
+
constructor(options) {
|
|
326
|
+
const names = NftConnectorFactory.names();
|
|
327
|
+
if (names.length === 0) {
|
|
328
|
+
throw new GeneralError(this.CLASS_NAME, "noConnectors");
|
|
329
|
+
}
|
|
330
|
+
this._defaultNamespace = options?.config?.defaultNamespace ?? names[0];
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Mint an NFT.
|
|
334
|
+
* @param issuer The issuer for the NFT, will also be the initial owner.
|
|
335
|
+
* @param tag The tag for the NFT.
|
|
336
|
+
* @param immutableMetadata The immutable metadata for the NFT.
|
|
337
|
+
* @param metadata The metadata for the NFT.
|
|
338
|
+
* @param namespace The namespace of the connector to use for the NFT, defaults to service configured namespace.
|
|
339
|
+
* @param identity The identity to perform the nft operation on.
|
|
340
|
+
* @returns The id of the created NFT in urn format.
|
|
341
|
+
*/
|
|
342
|
+
async mint(issuer, tag, immutableMetadata, metadata, namespace, identity) {
|
|
343
|
+
Guards.stringValue(this.CLASS_NAME, "issuer", issuer);
|
|
344
|
+
Guards.stringValue(this.CLASS_NAME, "tag", tag);
|
|
345
|
+
Guards.stringValue(this.CLASS_NAME, "identity", identity);
|
|
346
|
+
try {
|
|
347
|
+
const connectorNamespace = namespace ?? this._defaultNamespace;
|
|
348
|
+
const nftConnector = NftConnectorFactory.get(connectorNamespace);
|
|
349
|
+
const nftUrn = await nftConnector.mint(identity, issuer, tag, immutableMetadata, metadata);
|
|
350
|
+
return nftUrn;
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
throw new GeneralError(this.CLASS_NAME, "mintFailed", undefined, error);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Resolve an NFT.
|
|
358
|
+
* @param id The id of the NFT to resolve.
|
|
359
|
+
* @param identity The identity to perform the nft operation on.
|
|
360
|
+
* @returns The data for the NFT.
|
|
361
|
+
*/
|
|
362
|
+
async resolve(id, identity) {
|
|
363
|
+
Urn.guard(this.CLASS_NAME, "id", id);
|
|
364
|
+
try {
|
|
365
|
+
const nftConnector = this.getConnector(id);
|
|
366
|
+
return nftConnector.resolve(id);
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
throw new GeneralError(this.CLASS_NAME, "resolveFailed", undefined, error);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Burn an NFT.
|
|
374
|
+
* @param id The id of the NFT to burn in urn format.
|
|
375
|
+
* @param identity The identity to perform the nft operation on.
|
|
376
|
+
* @returns Nothing.
|
|
377
|
+
*/
|
|
378
|
+
async burn(id, identity) {
|
|
379
|
+
Urn.guard(this.CLASS_NAME, "id", id);
|
|
380
|
+
Guards.stringValue(this.CLASS_NAME, "identity", identity);
|
|
381
|
+
try {
|
|
382
|
+
const nftConnector = this.getConnector(id);
|
|
383
|
+
await nftConnector.burn(identity, id);
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
throw new GeneralError(this.CLASS_NAME, "burnFailed", undefined, error);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Transfer an NFT.
|
|
391
|
+
* @param id The id of the NFT to transfer in urn format.
|
|
392
|
+
* @param recipient The recipient of the NFT.
|
|
393
|
+
* @param metadata Optional mutable data to include during the transfer.
|
|
394
|
+
* @param identity The identity to perform the nft operation on.
|
|
395
|
+
* @returns Nothing.
|
|
396
|
+
*/
|
|
397
|
+
async transfer(id, recipient, metadata, identity) {
|
|
398
|
+
Urn.guard(this.CLASS_NAME, "id", id);
|
|
399
|
+
Guards.stringValue(this.CLASS_NAME, "recipient", recipient);
|
|
400
|
+
Guards.stringValue(this.CLASS_NAME, "identity", identity);
|
|
401
|
+
try {
|
|
402
|
+
const nftConnector = this.getConnector(id);
|
|
403
|
+
await nftConnector.transfer(identity, id, recipient, metadata);
|
|
404
|
+
}
|
|
405
|
+
catch (error) {
|
|
406
|
+
throw new GeneralError(this.CLASS_NAME, "transferFailed", undefined, error);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Update the data of the NFT.
|
|
411
|
+
* @param id The id of the NFT to update in urn format.
|
|
412
|
+
* @param metadata The mutable data to update.
|
|
413
|
+
* @param identity The identity to perform the nft operation on.
|
|
414
|
+
* @returns Nothing.
|
|
415
|
+
*/
|
|
416
|
+
async update(id, metadata, identity) {
|
|
417
|
+
Urn.guard(this.CLASS_NAME, "id", id);
|
|
418
|
+
Guards.object(this.CLASS_NAME, "metadata", metadata);
|
|
419
|
+
Guards.stringValue(this.CLASS_NAME, "identity", identity);
|
|
420
|
+
try {
|
|
421
|
+
const nftConnector = this.getConnector(id);
|
|
422
|
+
await nftConnector.update(identity, id, metadata);
|
|
423
|
+
}
|
|
424
|
+
catch (error) {
|
|
425
|
+
throw new GeneralError(this.CLASS_NAME, "updateFailed", undefined, error);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Get the connector from the uri.
|
|
430
|
+
* @param id The id of the NFT in urn format.
|
|
431
|
+
* @returns The connector.
|
|
432
|
+
* @internal
|
|
433
|
+
*/
|
|
434
|
+
getConnector(id) {
|
|
435
|
+
const idUri = Urn.fromValidString(id);
|
|
436
|
+
if (idUri.namespaceIdentifier() !== NftService.NAMESPACE) {
|
|
437
|
+
throw new GeneralError(this.CLASS_NAME, "namespaceMismatch", {
|
|
438
|
+
namespace: NftService.NAMESPACE,
|
|
439
|
+
id
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
return NftConnectorFactory.get(idUri.namespaceMethod());
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const restEntryPoints = [
|
|
447
|
+
{
|
|
448
|
+
name: "nft",
|
|
449
|
+
defaultBaseRoute: "nft",
|
|
450
|
+
tags: tagsNft,
|
|
451
|
+
generateRoutes: generateRestRoutesNft
|
|
452
|
+
}
|
|
453
|
+
];
|
|
454
|
+
|
|
455
|
+
export { NftService, generateRestRoutesNft, nftBurn, nftMint, nftResolve, nftTransfer, nftUpdate, restEntryPoints, tagsNft };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ICreatedResponse, IHttpRequestContext, INoContentResponse, IRestRoute, ITag } from "@twin.org/api-models";
|
|
2
|
+
import type { INftBurnRequest, INftMintRequest, INftResolveRequest, INftResolveResponse, INftTransferRequest, INftUpdateRequest } from "@twin.org/nft-models";
|
|
3
|
+
/**
|
|
4
|
+
* The tag to associate with the routes.
|
|
5
|
+
*/
|
|
6
|
+
export declare const tagsNft: ITag[];
|
|
7
|
+
/**
|
|
8
|
+
* The REST routes for NFT.
|
|
9
|
+
* @param baseRouteName Prefix to prepend to the paths.
|
|
10
|
+
* @param componentName The name of the component to use in the routes stored in the ComponentFactory.
|
|
11
|
+
* @returns The generated routes.
|
|
12
|
+
*/
|
|
13
|
+
export declare function generateRestRoutesNft(baseRouteName: string, componentName: string): IRestRoute[];
|
|
14
|
+
/**
|
|
15
|
+
* Mint an NFT.
|
|
16
|
+
* @param httpRequestContext The request context for the API.
|
|
17
|
+
* @param componentName The name of the component to use in the routes.
|
|
18
|
+
* @param request The request.
|
|
19
|
+
* @returns The response object with additional http response properties.
|
|
20
|
+
*/
|
|
21
|
+
export declare function nftMint(httpRequestContext: IHttpRequestContext, componentName: string, request: INftMintRequest): Promise<ICreatedResponse>;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve an NFT.
|
|
24
|
+
* @param httpRequestContext The request context for the API.
|
|
25
|
+
* @param componentName The name of the component to use in the routes.
|
|
26
|
+
* @param request The request.
|
|
27
|
+
* @returns The response object with additional http response properties.
|
|
28
|
+
*/
|
|
29
|
+
export declare function nftResolve(httpRequestContext: IHttpRequestContext, componentName: string, request: INftResolveRequest): Promise<INftResolveResponse>;
|
|
30
|
+
/**
|
|
31
|
+
* Burn an NFT.
|
|
32
|
+
* @param httpRequestContext The request context for the API.
|
|
33
|
+
* @param componentName The name of the component to use in the routes.
|
|
34
|
+
* @param request The request.
|
|
35
|
+
* @returns The response object with additional http response properties.
|
|
36
|
+
*/
|
|
37
|
+
export declare function nftBurn(httpRequestContext: IHttpRequestContext, componentName: string, request: INftBurnRequest): Promise<INoContentResponse>;
|
|
38
|
+
/**
|
|
39
|
+
* Transfer an NFT.
|
|
40
|
+
* @param httpRequestContext The request context for the API.
|
|
41
|
+
* @param componentName The name of the component to use in the routes.
|
|
42
|
+
* @param request The request.
|
|
43
|
+
* @returns The response object with additional http response properties.
|
|
44
|
+
*/
|
|
45
|
+
export declare function nftTransfer(httpRequestContext: IHttpRequestContext, componentName: string, request: INftTransferRequest): Promise<INoContentResponse>;
|
|
46
|
+
/**
|
|
47
|
+
* Update an NFT.
|
|
48
|
+
* @param httpRequestContext The request context for the API.
|
|
49
|
+
* @param componentName The name of the component to use in the routes.
|
|
50
|
+
* @param request The request.
|
|
51
|
+
* @returns The response object with additional http response properties.
|
|
52
|
+
*/
|
|
53
|
+
export declare function nftUpdate(httpRequestContext: IHttpRequestContext, componentName: string, request: INftUpdateRequest): Promise<INoContentResponse>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type INftComponent } from "@twin.org/nft-models";
|
|
2
|
+
import type { INftServiceConstructorOptions } from "./models/INftServiceConstructorOptions";
|
|
3
|
+
/**
|
|
4
|
+
* Service for performing NFT operations to a connector.
|
|
5
|
+
*/
|
|
6
|
+
export declare class NftService implements INftComponent {
|
|
7
|
+
/**
|
|
8
|
+
* The namespace supported by the nft service.
|
|
9
|
+
*/
|
|
10
|
+
static readonly NAMESPACE: string;
|
|
11
|
+
/**
|
|
12
|
+
* Runtime name for the class.
|
|
13
|
+
*/
|
|
14
|
+
readonly CLASS_NAME: string;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new instance of NftService.
|
|
17
|
+
* @param options The options for the service.
|
|
18
|
+
*/
|
|
19
|
+
constructor(options?: INftServiceConstructorOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Mint an NFT.
|
|
22
|
+
* @param issuer The issuer for the NFT, will also be the initial owner.
|
|
23
|
+
* @param tag The tag for the NFT.
|
|
24
|
+
* @param immutableMetadata The immutable metadata for the NFT.
|
|
25
|
+
* @param metadata The metadata for the NFT.
|
|
26
|
+
* @param namespace The namespace of the connector to use for the NFT, defaults to service configured namespace.
|
|
27
|
+
* @param identity The identity to perform the nft operation on.
|
|
28
|
+
* @returns The id of the created NFT in urn format.
|
|
29
|
+
*/
|
|
30
|
+
mint<T = unknown, U = unknown>(issuer: string, tag: string, immutableMetadata?: T, metadata?: U, namespace?: string, identity?: string): Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* Resolve an NFT.
|
|
33
|
+
* @param id The id of the NFT to resolve.
|
|
34
|
+
* @param identity The identity to perform the nft operation on.
|
|
35
|
+
* @returns The data for the NFT.
|
|
36
|
+
*/
|
|
37
|
+
resolve<T = unknown, U = unknown>(id: string, identity?: string): Promise<{
|
|
38
|
+
issuer: string;
|
|
39
|
+
owner: string;
|
|
40
|
+
tag: string;
|
|
41
|
+
immutableMetadata?: T;
|
|
42
|
+
metadata?: U;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Burn an NFT.
|
|
46
|
+
* @param id The id of the NFT to burn in urn format.
|
|
47
|
+
* @param identity The identity to perform the nft operation on.
|
|
48
|
+
* @returns Nothing.
|
|
49
|
+
*/
|
|
50
|
+
burn(id: string, identity?: string): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Transfer an NFT.
|
|
53
|
+
* @param id The id of the NFT to transfer in urn format.
|
|
54
|
+
* @param recipient The recipient of the NFT.
|
|
55
|
+
* @param metadata Optional mutable data to include during the transfer.
|
|
56
|
+
* @param identity The identity to perform the nft operation on.
|
|
57
|
+
* @returns Nothing.
|
|
58
|
+
*/
|
|
59
|
+
transfer<T = unknown>(id: string, recipient: string, metadata?: T, identity?: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Update the data of the NFT.
|
|
62
|
+
* @param id The id of the NFT to update in urn format.
|
|
63
|
+
* @param metadata The mutable data to update.
|
|
64
|
+
* @param identity The identity to perform the nft operation on.
|
|
65
|
+
* @returns Nothing.
|
|
66
|
+
*/
|
|
67
|
+
update<T = unknown>(id: string, metadata: T, identity?: string): Promise<void>;
|
|
68
|
+
}
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @twin.org/nft-service - Examples
|