@arcblock/nft 1.28.9 → 1.29.0
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/README.md +0 -1
- package/esm/enum.d.mts +23 -0
- package/esm/enum.mjs +22 -0
- package/esm/factory.d.mts +141 -0
- package/esm/factory.mjs +310 -0
- package/esm/index.d.mts +5 -0
- package/esm/index.mjs +6 -0
- package/esm/issuer.d.mts +44 -0
- package/esm/issuer.mjs +36 -0
- package/esm/package.mjs +5 -0
- package/esm/recipient.d.mts +40 -0
- package/esm/recipient.mjs +34 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/enum.cjs +24 -0
- package/lib/enum.d.cts +23 -0
- package/lib/factory.cjs +319 -0
- package/lib/factory.d.cts +141 -0
- package/lib/index.cjs +10 -0
- package/lib/index.d.cts +5 -0
- package/lib/issuer.cjs +38 -0
- package/lib/issuer.d.cts +44 -0
- package/lib/package.cjs +11 -0
- package/lib/recipient.cjs +36 -0
- package/lib/recipient.d.cts +40 -0
- package/package.json +41 -15
- package/lib/enum.d.ts +0 -18
- package/lib/enum.js +0 -21
- package/lib/factory.d.ts +0 -165
- package/lib/factory.js +0 -398
- package/lib/index.d.ts +0 -259
- package/lib/index.js +0 -12
- package/lib/issuer.d.ts +0 -31
- package/lib/issuer.js +0 -25
- package/lib/recipient.d.ts +0 -27
- package/lib/recipient.js +0 -24
package/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
[](https://github.com/prettier/prettier)
|
|
4
3
|
[](https://docs.arcblock.io)
|
|
5
4
|
[](https://gitter.im/ArcBlock/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
|
6
5
|
|
package/esm/enum.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/enum.d.ts
|
|
2
|
+
declare const NFTType: Readonly<{
|
|
3
|
+
readonly ticket: 0;
|
|
4
|
+
readonly coupon: 1;
|
|
5
|
+
readonly certificate: 2;
|
|
6
|
+
readonly badge: 3;
|
|
7
|
+
readonly license: 4;
|
|
8
|
+
readonly giftcard: 5;
|
|
9
|
+
readonly passport: 6;
|
|
10
|
+
readonly idcard: 7;
|
|
11
|
+
readonly receipt: 8;
|
|
12
|
+
readonly other: 127;
|
|
13
|
+
}>;
|
|
14
|
+
declare const NFTStatus: Readonly<{
|
|
15
|
+
readonly normal: 0;
|
|
16
|
+
readonly consumed: 1;
|
|
17
|
+
readonly invalid: 2;
|
|
18
|
+
readonly expired: 3;
|
|
19
|
+
}>;
|
|
20
|
+
type NFTTypeKey = keyof typeof NFTType;
|
|
21
|
+
type NFTStatusKey = keyof typeof NFTStatus;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { NFTStatus, NFTStatusKey, NFTType, NFTTypeKey };
|
package/esm/enum.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/enum.ts
|
|
2
|
+
const NFTType = Object.freeze({
|
|
3
|
+
ticket: 0,
|
|
4
|
+
coupon: 1,
|
|
5
|
+
certificate: 2,
|
|
6
|
+
badge: 3,
|
|
7
|
+
license: 4,
|
|
8
|
+
giftcard: 5,
|
|
9
|
+
passport: 6,
|
|
10
|
+
idcard: 7,
|
|
11
|
+
receipt: 8,
|
|
12
|
+
other: 127
|
|
13
|
+
});
|
|
14
|
+
const NFTStatus = Object.freeze({
|
|
15
|
+
normal: 0,
|
|
16
|
+
consumed: 1,
|
|
17
|
+
invalid: 2,
|
|
18
|
+
expired: 3
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { NFTStatus, NFTType };
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import NFTIssuer from "./issuer.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/factory.d.ts
|
|
4
|
+
interface WalletObject {
|
|
5
|
+
address: string;
|
|
6
|
+
publicKey: Uint8Array | string;
|
|
7
|
+
sign: (data: string) => Promise<Uint8Array | string>;
|
|
8
|
+
}
|
|
9
|
+
interface IssuerParams {
|
|
10
|
+
name: string;
|
|
11
|
+
url?: string;
|
|
12
|
+
logo?: string;
|
|
13
|
+
}
|
|
14
|
+
interface NFTFactoryParams {
|
|
15
|
+
chainHost: string;
|
|
16
|
+
wallet: WalletObject;
|
|
17
|
+
issuer: IssuerParams;
|
|
18
|
+
}
|
|
19
|
+
interface DisplayObject {
|
|
20
|
+
type?: string;
|
|
21
|
+
content?: string;
|
|
22
|
+
}
|
|
23
|
+
interface AssetData {
|
|
24
|
+
typeUrl: string;
|
|
25
|
+
value: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
interface Asset {
|
|
28
|
+
address?: string;
|
|
29
|
+
moniker: string;
|
|
30
|
+
readonly: boolean;
|
|
31
|
+
transferrable: boolean;
|
|
32
|
+
data: AssetData;
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}
|
|
35
|
+
interface CreateParams {
|
|
36
|
+
display?: string | DisplayObject;
|
|
37
|
+
data?: Record<string, unknown>;
|
|
38
|
+
attributes?: Record<string, unknown>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Used to create standard asset on forge powered blockchain
|
|
42
|
+
* All assets are signed assets, eg, the asset data are self approvable
|
|
43
|
+
*/
|
|
44
|
+
declare class NFTFactory {
|
|
45
|
+
chainHost: string;
|
|
46
|
+
client: unknown;
|
|
47
|
+
wallet: WalletObject;
|
|
48
|
+
issuer: NFTIssuer;
|
|
49
|
+
/**
|
|
50
|
+
* Creates an instance of NFTFactory.
|
|
51
|
+
* @param params - Factory parameters
|
|
52
|
+
* @param params.chainHost - on which chain to create wallet
|
|
53
|
+
* @param params.wallet - issuer wallet
|
|
54
|
+
* @param params.issuer - issuer attributes, such as name, url and logo
|
|
55
|
+
*/
|
|
56
|
+
constructor({
|
|
57
|
+
chainHost,
|
|
58
|
+
wallet,
|
|
59
|
+
issuer
|
|
60
|
+
}: NFTFactoryParams);
|
|
61
|
+
/**
|
|
62
|
+
* Create a ticket
|
|
63
|
+
*
|
|
64
|
+
* @param params - Ticket parameters
|
|
65
|
+
* @param params.display - display of the ticket { type, content }
|
|
66
|
+
* @param params.data - asset payload
|
|
67
|
+
* @param params.attributes - asset attributes
|
|
68
|
+
* @returns Promise resolving to [asset, hash]
|
|
69
|
+
*/
|
|
70
|
+
createTicket({
|
|
71
|
+
display,
|
|
72
|
+
data,
|
|
73
|
+
attributes
|
|
74
|
+
}: CreateParams): Promise<[Asset, string]>;
|
|
75
|
+
/**
|
|
76
|
+
* Create a coupon asset
|
|
77
|
+
*
|
|
78
|
+
* @param params - Coupon parameters
|
|
79
|
+
* @param params.display - display of the coupon { type, content }
|
|
80
|
+
* @param params.data - asset payload
|
|
81
|
+
* @param params.attributes - asset attributes
|
|
82
|
+
* @returns Promise resolving to [asset, hash]
|
|
83
|
+
*/
|
|
84
|
+
createCoupon({
|
|
85
|
+
display,
|
|
86
|
+
data,
|
|
87
|
+
attributes
|
|
88
|
+
}: CreateParams): Promise<[Asset, string]>;
|
|
89
|
+
/**
|
|
90
|
+
* Create a certificate asset
|
|
91
|
+
*
|
|
92
|
+
* @param params - Certificate parameters
|
|
93
|
+
* @param params.display - display of the certificate { type, content }
|
|
94
|
+
* @param params.data - asset payload
|
|
95
|
+
* @param params.attributes - asset attributes
|
|
96
|
+
* @returns Promise resolving to [asset, hash]
|
|
97
|
+
*/
|
|
98
|
+
createCertificate({
|
|
99
|
+
display,
|
|
100
|
+
data,
|
|
101
|
+
attributes
|
|
102
|
+
}: CreateParams): Promise<[Asset, string]>;
|
|
103
|
+
/**
|
|
104
|
+
* Create a badge
|
|
105
|
+
*
|
|
106
|
+
* @param params - Badge parameters
|
|
107
|
+
* @param params.display - display of the badge { type, content }
|
|
108
|
+
* @param params.data - asset payload
|
|
109
|
+
* @param params.attributes - asset attributes
|
|
110
|
+
* @returns Promise resolving to [asset, hash]
|
|
111
|
+
*/
|
|
112
|
+
createBadge({
|
|
113
|
+
display,
|
|
114
|
+
data,
|
|
115
|
+
attributes
|
|
116
|
+
}: CreateParams): Promise<[Asset, string]>;
|
|
117
|
+
/**
|
|
118
|
+
* Create a passport
|
|
119
|
+
*
|
|
120
|
+
* @param params - Passport parameters
|
|
121
|
+
* @param params.display - display of the passport { type, content }
|
|
122
|
+
* @param params.data - asset payload
|
|
123
|
+
* @param params.attributes - asset attributes
|
|
124
|
+
* @returns Promise resolving to [asset, hash]
|
|
125
|
+
*/
|
|
126
|
+
createPassport({
|
|
127
|
+
display,
|
|
128
|
+
data,
|
|
129
|
+
attributes
|
|
130
|
+
}: CreateParams): Promise<[Asset, string]>;
|
|
131
|
+
createSignedAsset(payload: Record<string, unknown>, attributes: Record<string, unknown>): Promise<[Asset, string]>;
|
|
132
|
+
_createDisplay(display: string | DisplayObject): DisplayObject;
|
|
133
|
+
_createCert({
|
|
134
|
+
display,
|
|
135
|
+
data,
|
|
136
|
+
attributes
|
|
137
|
+
}: CreateParams): Promise<[Asset, string]>;
|
|
138
|
+
getVCBody(asset: Asset): Record<string, unknown> | null;
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
export { NFTFactory as default };
|
package/esm/factory.mjs
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import issuer_default from "./issuer.mjs";
|
|
2
|
+
import recipient_default from "./recipient.mjs";
|
|
3
|
+
import { name } from "./package.mjs";
|
|
4
|
+
import { toAssetAddress } from "@arcblock/did-util";
|
|
5
|
+
import { create } from "@arcblock/vc";
|
|
6
|
+
import Client from "@ocap/client";
|
|
7
|
+
import { toBase58 } from "@ocap/util";
|
|
8
|
+
import { isValid } from "@ocap/wallet";
|
|
9
|
+
import Debug from "debug";
|
|
10
|
+
import stringify from "json-stable-stringify";
|
|
11
|
+
import cloneDeep from "lodash/cloneDeep.js";
|
|
12
|
+
import get from "lodash/get.js";
|
|
13
|
+
import isDate from "lodash/isDate.js";
|
|
14
|
+
import isNumber from "lodash/isNumber.js";
|
|
15
|
+
|
|
16
|
+
//#region src/factory.ts
|
|
17
|
+
const debug = Debug(name);
|
|
18
|
+
/**
|
|
19
|
+
* Used to create standard asset on forge powered blockchain
|
|
20
|
+
* All assets are signed assets, eg, the asset data are self approvable
|
|
21
|
+
*/
|
|
22
|
+
var NFTFactory = class {
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of NFTFactory.
|
|
25
|
+
* @param params - Factory parameters
|
|
26
|
+
* @param params.chainHost - on which chain to create wallet
|
|
27
|
+
* @param params.wallet - issuer wallet
|
|
28
|
+
* @param params.issuer - issuer attributes, such as name, url and logo
|
|
29
|
+
*/
|
|
30
|
+
constructor({ chainHost, wallet, issuer }) {
|
|
31
|
+
if (!chainHost) throw new Error("NFTFactory requires valid chain host");
|
|
32
|
+
if (isValid(wallet) === false) throw new Error("NFTFactory requires valid wallet issuer");
|
|
33
|
+
this.chainHost = chainHost;
|
|
34
|
+
this.client = new Client(chainHost);
|
|
35
|
+
this.wallet = wallet;
|
|
36
|
+
this.issuer = new issuer_default({
|
|
37
|
+
wallet,
|
|
38
|
+
...issuer
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a ticket
|
|
43
|
+
*
|
|
44
|
+
* @param params - Ticket parameters
|
|
45
|
+
* @param params.display - display of the ticket { type, content }
|
|
46
|
+
* @param params.data - asset payload
|
|
47
|
+
* @param params.attributes - asset attributes
|
|
48
|
+
* @returns Promise resolving to [asset, hash]
|
|
49
|
+
*/
|
|
50
|
+
async createTicket({ display = "", data = {}, attributes = {} }) {
|
|
51
|
+
const requiredFields = [
|
|
52
|
+
"name",
|
|
53
|
+
"description",
|
|
54
|
+
"location"
|
|
55
|
+
];
|
|
56
|
+
const timeFields = ["startTime", "endTime"];
|
|
57
|
+
requiredFields.forEach((x) => {
|
|
58
|
+
if (!data[x]) throw new Error(`Missing required field ${x} when creating ticket asset`);
|
|
59
|
+
});
|
|
60
|
+
timeFields.forEach((x) => {
|
|
61
|
+
if (!isDate(new Date(data[x]))) throw new Error(`Invalid date field ${x} when creating ticket asset`);
|
|
62
|
+
});
|
|
63
|
+
const { name: name$1, description, location, recipient, type, startTime, endTime, display: innerDisplay = "", issuanceDate } = data;
|
|
64
|
+
if (!(recipient instanceof recipient_default)) throw new Error(`Invalid recipient field when creating ${type} asset`);
|
|
65
|
+
const vc = await create({
|
|
66
|
+
type: [
|
|
67
|
+
type,
|
|
68
|
+
"NFTTicket",
|
|
69
|
+
"VerifiableCredential"
|
|
70
|
+
].filter(Boolean),
|
|
71
|
+
issuer: {
|
|
72
|
+
wallet: this.wallet,
|
|
73
|
+
name: this.issuer.attributes.name
|
|
74
|
+
},
|
|
75
|
+
subject: {
|
|
76
|
+
id: recipient.wallet.address,
|
|
77
|
+
name: name$1,
|
|
78
|
+
description,
|
|
79
|
+
location,
|
|
80
|
+
display: this._createDisplay(display || innerDisplay),
|
|
81
|
+
startTime: new Date(startTime),
|
|
82
|
+
endTime: new Date(endTime)
|
|
83
|
+
},
|
|
84
|
+
issuanceDate: issuanceDate || /* @__PURE__ */ new Date(),
|
|
85
|
+
expirationDate: new Date(endTime)
|
|
86
|
+
});
|
|
87
|
+
debug("createTicket.vc", vc);
|
|
88
|
+
return this.createSignedAsset(vc, attributes);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Create a coupon asset
|
|
92
|
+
*
|
|
93
|
+
* @param params - Coupon parameters
|
|
94
|
+
* @param params.display - display of the coupon { type, content }
|
|
95
|
+
* @param params.data - asset payload
|
|
96
|
+
* @param params.attributes - asset attributes
|
|
97
|
+
* @returns Promise resolving to [asset, hash]
|
|
98
|
+
*/
|
|
99
|
+
async createCoupon({ display = "", data = {}, attributes = {} }) {
|
|
100
|
+
const requiredFields = ["name", "description"];
|
|
101
|
+
const numberFields = [
|
|
102
|
+
"ratio",
|
|
103
|
+
"amount",
|
|
104
|
+
"minAmount"
|
|
105
|
+
];
|
|
106
|
+
const timeFields = ["startTime", "expireTime"];
|
|
107
|
+
requiredFields.forEach((x) => {
|
|
108
|
+
if (!data[x]) throw new Error(`Missing required field ${x} when creating coupon asset`);
|
|
109
|
+
});
|
|
110
|
+
numberFields.forEach((x) => {
|
|
111
|
+
if (!data[x] || !isNumber(data[x])) throw new Error(`Invalid number field ${x} when creating coupon asset`);
|
|
112
|
+
});
|
|
113
|
+
timeFields.forEach((x) => {
|
|
114
|
+
if (!isDate(new Date(data[x]))) throw new Error(`Invalid date field ${x} when creating coupon asset`);
|
|
115
|
+
});
|
|
116
|
+
const { name: name$1, description, ratio, amount, location, minAmount, expireTime, type, recipient, display: innerDisplay = "", issuanceDate, endpoint, endpointScope } = data;
|
|
117
|
+
const vc = await create({
|
|
118
|
+
type: [
|
|
119
|
+
type,
|
|
120
|
+
"NFTCoupon",
|
|
121
|
+
"VerifiableCredential"
|
|
122
|
+
].filter(Boolean),
|
|
123
|
+
issuer: {
|
|
124
|
+
wallet: this.issuer.wallet,
|
|
125
|
+
name: this.issuer.attributes.name
|
|
126
|
+
},
|
|
127
|
+
subject: {
|
|
128
|
+
id: recipient.wallet.address,
|
|
129
|
+
name: name$1,
|
|
130
|
+
description,
|
|
131
|
+
location,
|
|
132
|
+
ratio,
|
|
133
|
+
amount,
|
|
134
|
+
minAmount,
|
|
135
|
+
display: this._createDisplay(display || innerDisplay)
|
|
136
|
+
},
|
|
137
|
+
endpoint,
|
|
138
|
+
endpointScope,
|
|
139
|
+
issuanceDate: issuanceDate || /* @__PURE__ */ new Date(),
|
|
140
|
+
expirationDate: new Date(expireTime)
|
|
141
|
+
});
|
|
142
|
+
debug("createTicket.coupon", vc);
|
|
143
|
+
return this.createSignedAsset(vc, attributes);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Create a certificate asset
|
|
147
|
+
*
|
|
148
|
+
* @param params - Certificate parameters
|
|
149
|
+
* @param params.display - display of the certificate { type, content }
|
|
150
|
+
* @param params.data - asset payload
|
|
151
|
+
* @param params.attributes - asset attributes
|
|
152
|
+
* @returns Promise resolving to [asset, hash]
|
|
153
|
+
*/
|
|
154
|
+
createCertificate({ display = "", data = {}, attributes = {} }) {
|
|
155
|
+
return this._createCert({
|
|
156
|
+
data,
|
|
157
|
+
display,
|
|
158
|
+
attributes
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Create a badge
|
|
163
|
+
*
|
|
164
|
+
* @param params - Badge parameters
|
|
165
|
+
* @param params.display - display of the badge { type, content }
|
|
166
|
+
* @param params.data - asset payload
|
|
167
|
+
* @param params.attributes - asset attributes
|
|
168
|
+
* @returns Promise resolving to [asset, hash]
|
|
169
|
+
*/
|
|
170
|
+
async createBadge({ display = "", data = {}, attributes = {} }) {
|
|
171
|
+
const { name: name$1, recipient, description, display: innerDisplay = "", type, endpoint, endpointScope } = data;
|
|
172
|
+
const vc = await create({
|
|
173
|
+
type: [
|
|
174
|
+
type,
|
|
175
|
+
"NFTBadge",
|
|
176
|
+
"VerifiableCredential"
|
|
177
|
+
].filter(Boolean),
|
|
178
|
+
issuer: {
|
|
179
|
+
wallet: this.wallet,
|
|
180
|
+
name: this.issuer.attributes.name
|
|
181
|
+
},
|
|
182
|
+
subject: {
|
|
183
|
+
id: recipient.wallet.address,
|
|
184
|
+
name: name$1,
|
|
185
|
+
description,
|
|
186
|
+
display: this._createDisplay(display || innerDisplay)
|
|
187
|
+
},
|
|
188
|
+
endpoint,
|
|
189
|
+
endpointScope
|
|
190
|
+
});
|
|
191
|
+
return this.createSignedAsset(vc, attributes);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Create a passport
|
|
195
|
+
*
|
|
196
|
+
* @param params - Passport parameters
|
|
197
|
+
* @param params.display - display of the passport { type, content }
|
|
198
|
+
* @param params.data - asset payload
|
|
199
|
+
* @param params.attributes - asset attributes
|
|
200
|
+
* @returns Promise resolving to [asset, hash]
|
|
201
|
+
*/
|
|
202
|
+
async createPassport({ display = "", data = {}, attributes = {} }) {
|
|
203
|
+
const { recipient, passport, display: innerDisplay = "", type, endpoint, endpointScope } = data;
|
|
204
|
+
const passportData = passport;
|
|
205
|
+
if (!passportData || !passportData.name || !passportData.title) throw new Error("NFTPassport requires valid passport object: which must have a name and a title");
|
|
206
|
+
const vc = await create({
|
|
207
|
+
type: [
|
|
208
|
+
type,
|
|
209
|
+
"NFTPassport",
|
|
210
|
+
"VerifiableCredential"
|
|
211
|
+
].filter(Boolean),
|
|
212
|
+
issuer: {
|
|
213
|
+
wallet: this.wallet,
|
|
214
|
+
name: this.issuer.attributes.name
|
|
215
|
+
},
|
|
216
|
+
subject: {
|
|
217
|
+
id: recipient.wallet.address,
|
|
218
|
+
passport,
|
|
219
|
+
display: this._createDisplay(display || innerDisplay)
|
|
220
|
+
},
|
|
221
|
+
endpoint,
|
|
222
|
+
endpointScope
|
|
223
|
+
});
|
|
224
|
+
return this.createSignedAsset(vc, attributes);
|
|
225
|
+
}
|
|
226
|
+
async createSignedAsset(payload, attributes) {
|
|
227
|
+
payload.signature = toBase58(await this.wallet.sign(stringify(payload)));
|
|
228
|
+
const asset = {
|
|
229
|
+
moniker: get(payload, "data.name") || get(payload, "credentialSubject.name") || "signed_asset",
|
|
230
|
+
readonly: false,
|
|
231
|
+
transferrable: true,
|
|
232
|
+
data: {
|
|
233
|
+
typeUrl: "vc",
|
|
234
|
+
value: payload
|
|
235
|
+
},
|
|
236
|
+
...attributes
|
|
237
|
+
};
|
|
238
|
+
asset.address = toAssetAddress(asset);
|
|
239
|
+
return [asset, await this.client.sendCreateAssetTx({
|
|
240
|
+
tx: { itx: asset },
|
|
241
|
+
wallet: this.wallet
|
|
242
|
+
})];
|
|
243
|
+
}
|
|
244
|
+
_createDisplay(display) {
|
|
245
|
+
if (display) {
|
|
246
|
+
if (typeof display === "string") return {
|
|
247
|
+
type: "svg_gzipped",
|
|
248
|
+
content: display
|
|
249
|
+
};
|
|
250
|
+
return display;
|
|
251
|
+
}
|
|
252
|
+
return {};
|
|
253
|
+
}
|
|
254
|
+
async _createCert({ display = "", data = {}, attributes = {} }) {
|
|
255
|
+
const requiredFields = [
|
|
256
|
+
"name",
|
|
257
|
+
"description",
|
|
258
|
+
"reason",
|
|
259
|
+
"logoUrl"
|
|
260
|
+
];
|
|
261
|
+
const timeFields = ["issueTime", "expireTime"];
|
|
262
|
+
requiredFields.forEach((x) => {
|
|
263
|
+
if (!data[x]) throw new Error(`Missing required field ${x} when creating certificate`);
|
|
264
|
+
});
|
|
265
|
+
timeFields.forEach((x) => {
|
|
266
|
+
if (!isDate(new Date(data[x]))) throw new Error(`Invalid date field ${x} when creating certificate`);
|
|
267
|
+
});
|
|
268
|
+
if (!(data.recipient instanceof recipient_default)) throw new Error("Invalid recipient field when creating certificate");
|
|
269
|
+
const { name: name$1, description, reason, logoUrl, recipient, type, expireTime, display: innerDisplay = "", issuanceDate, endpoint, endpointScope } = data;
|
|
270
|
+
const vc = await create({
|
|
271
|
+
type: [
|
|
272
|
+
type,
|
|
273
|
+
"NFTCertificate",
|
|
274
|
+
"VerifiableCredential"
|
|
275
|
+
].filter(Boolean),
|
|
276
|
+
issuer: {
|
|
277
|
+
wallet: this.wallet,
|
|
278
|
+
name: this.issuer.attributes.name
|
|
279
|
+
},
|
|
280
|
+
subject: {
|
|
281
|
+
id: recipient.wallet.address,
|
|
282
|
+
name: name$1,
|
|
283
|
+
description,
|
|
284
|
+
reason,
|
|
285
|
+
logoUrl,
|
|
286
|
+
display: this._createDisplay(display || innerDisplay)
|
|
287
|
+
},
|
|
288
|
+
endpoint,
|
|
289
|
+
endpointScope,
|
|
290
|
+
issuanceDate: issuanceDate || /* @__PURE__ */ new Date(),
|
|
291
|
+
expirationDate: +new Date(expireTime)
|
|
292
|
+
});
|
|
293
|
+
debug("createCert.vc", vc);
|
|
294
|
+
return this.createSignedAsset(vc, attributes);
|
|
295
|
+
}
|
|
296
|
+
getVCBody(asset) {
|
|
297
|
+
if (!asset.data) return null;
|
|
298
|
+
if (!asset.data.typeUrl) return null;
|
|
299
|
+
if (asset.data.typeUrl === "vc") {
|
|
300
|
+
const vc = cloneDeep(asset.data.value);
|
|
301
|
+
delete vc.signature;
|
|
302
|
+
return vc;
|
|
303
|
+
}
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
var factory_default = NFTFactory;
|
|
308
|
+
|
|
309
|
+
//#endregion
|
|
310
|
+
export { factory_default as default };
|
package/esm/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { NFTStatus, NFTStatusKey, NFTType, NFTTypeKey } from "./enum.mjs";
|
|
2
|
+
import NFTIssuer from "./issuer.mjs";
|
|
3
|
+
import NFTFactory from "./factory.mjs";
|
|
4
|
+
import NFTRecipient from "./recipient.mjs";
|
|
5
|
+
export { NFTFactory, NFTIssuer, NFTRecipient, NFTStatus, type NFTStatusKey, NFTType, type NFTTypeKey };
|
package/esm/index.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NFTStatus, NFTType } from "./enum.mjs";
|
|
2
|
+
import issuer_default from "./issuer.mjs";
|
|
3
|
+
import recipient_default from "./recipient.mjs";
|
|
4
|
+
import factory_default from "./factory.mjs";
|
|
5
|
+
|
|
6
|
+
export { factory_default as NFTFactory, issuer_default as NFTIssuer, recipient_default as NFTRecipient, NFTStatus, NFTType };
|
package/esm/issuer.d.mts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/issuer.d.ts
|
|
2
|
+
interface IssuerAttributes {
|
|
3
|
+
name: string;
|
|
4
|
+
url?: string;
|
|
5
|
+
logo?: string;
|
|
6
|
+
}
|
|
7
|
+
interface WalletObject {
|
|
8
|
+
address: string;
|
|
9
|
+
publicKey: Uint8Array | string;
|
|
10
|
+
}
|
|
11
|
+
interface NFTIssuerParams extends IssuerAttributes {
|
|
12
|
+
wallet: WalletObject;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Represents an NFT issuer with wallet and metadata
|
|
16
|
+
*/
|
|
17
|
+
declare class NFTIssuer {
|
|
18
|
+
wallet: WalletObject;
|
|
19
|
+
attributes: IssuerAttributes;
|
|
20
|
+
/**
|
|
21
|
+
* Creates an instance of NFTIssuer.
|
|
22
|
+
*
|
|
23
|
+
* @param params - Issuer parameters
|
|
24
|
+
* @param params.wallet - wallet object
|
|
25
|
+
* @param params.name - issuer name
|
|
26
|
+
* @param params.url - issuer website
|
|
27
|
+
* @param params.logo - issuer logo image url or logo base64
|
|
28
|
+
*/
|
|
29
|
+
constructor({
|
|
30
|
+
wallet,
|
|
31
|
+
name,
|
|
32
|
+
url,
|
|
33
|
+
logo
|
|
34
|
+
}: NFTIssuerParams);
|
|
35
|
+
toJSON(): {
|
|
36
|
+
name: string;
|
|
37
|
+
url?: string;
|
|
38
|
+
logo?: string;
|
|
39
|
+
did: string;
|
|
40
|
+
pk: any;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { NFTIssuer as default };
|
package/esm/issuer.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { toBase58 } from "@ocap/util";
|
|
2
|
+
|
|
3
|
+
//#region src/issuer.ts
|
|
4
|
+
/**
|
|
5
|
+
* Represents an NFT issuer with wallet and metadata
|
|
6
|
+
*/
|
|
7
|
+
var NFTIssuer = class {
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of NFTIssuer.
|
|
10
|
+
*
|
|
11
|
+
* @param params - Issuer parameters
|
|
12
|
+
* @param params.wallet - wallet object
|
|
13
|
+
* @param params.name - issuer name
|
|
14
|
+
* @param params.url - issuer website
|
|
15
|
+
* @param params.logo - issuer logo image url or logo base64
|
|
16
|
+
*/
|
|
17
|
+
constructor({ wallet, name, url, logo }) {
|
|
18
|
+
this.wallet = wallet;
|
|
19
|
+
this.attributes = {
|
|
20
|
+
name,
|
|
21
|
+
url,
|
|
22
|
+
logo
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
toJSON() {
|
|
26
|
+
return {
|
|
27
|
+
did: this.wallet.address,
|
|
28
|
+
pk: toBase58(this.wallet.publicKey),
|
|
29
|
+
...this.attributes
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var issuer_default = NFTIssuer;
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { issuer_default as default };
|
package/esm/package.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/recipient.d.ts
|
|
2
|
+
interface RecipientAttributes {
|
|
3
|
+
name: string;
|
|
4
|
+
location?: string;
|
|
5
|
+
}
|
|
6
|
+
interface WalletObject {
|
|
7
|
+
address: string;
|
|
8
|
+
publicKey: Uint8Array | string;
|
|
9
|
+
}
|
|
10
|
+
interface NFTRecipientParams extends RecipientAttributes {
|
|
11
|
+
wallet: WalletObject;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Represents an NFT recipient with wallet and metadata
|
|
15
|
+
*/
|
|
16
|
+
declare class NFTRecipient {
|
|
17
|
+
wallet: WalletObject;
|
|
18
|
+
attributes: RecipientAttributes;
|
|
19
|
+
/**
|
|
20
|
+
* Creates an instance of NFTRecipient.
|
|
21
|
+
*
|
|
22
|
+
* @param params - Recipient parameters
|
|
23
|
+
* @param params.wallet - wallet object
|
|
24
|
+
* @param params.name - recipient name
|
|
25
|
+
* @param params.location - recipient location
|
|
26
|
+
*/
|
|
27
|
+
constructor({
|
|
28
|
+
wallet,
|
|
29
|
+
name,
|
|
30
|
+
location
|
|
31
|
+
}: NFTRecipientParams);
|
|
32
|
+
toJSON(): {
|
|
33
|
+
name: string;
|
|
34
|
+
location?: string;
|
|
35
|
+
did: string;
|
|
36
|
+
pk: any;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { NFTRecipient as default };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { toBase58 } from "@ocap/util";
|
|
2
|
+
|
|
3
|
+
//#region src/recipient.ts
|
|
4
|
+
/**
|
|
5
|
+
* Represents an NFT recipient with wallet and metadata
|
|
6
|
+
*/
|
|
7
|
+
var NFTRecipient = class {
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of NFTRecipient.
|
|
10
|
+
*
|
|
11
|
+
* @param params - Recipient parameters
|
|
12
|
+
* @param params.wallet - wallet object
|
|
13
|
+
* @param params.name - recipient name
|
|
14
|
+
* @param params.location - recipient location
|
|
15
|
+
*/
|
|
16
|
+
constructor({ wallet, name, location }) {
|
|
17
|
+
this.wallet = wallet;
|
|
18
|
+
this.attributes = {
|
|
19
|
+
name,
|
|
20
|
+
location
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
toJSON() {
|
|
24
|
+
return {
|
|
25
|
+
did: this.wallet.address,
|
|
26
|
+
pk: toBase58(this.wallet.publicKey),
|
|
27
|
+
...this.attributes
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var recipient_default = NFTRecipient;
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { recipient_default as default };
|