@zubari/sdk 0.1.31 → 0.2.1

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.
@@ -1,4 +1,5 @@
1
1
  // src/config/contracts.ts
2
+ var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
2
3
  var PLATFORM_CONFIG = {
3
4
  // Platform fee in basis points (300 = 3%)
4
5
  tipFeeBps: 300,
@@ -16,37 +17,118 @@ var NFT_VOUCHER_TYPES = {
16
17
  { name: "uri", type: "string" },
17
18
  { name: "creator", type: "address" },
18
19
  { name: "royaltyBps", type: "uint256" },
19
- { name: "deadline", type: "uint256" }
20
+ { name: "deadline", type: "uint256" },
21
+ // Pricing fields
22
+ { name: "price", type: "uint256" },
23
+ { name: "currency", type: "address" },
24
+ { name: "nonce", type: "uint256" },
25
+ // Watermarking fields
26
+ { name: "contentHash", type: "bytes32" },
27
+ { name: "userId", type: "bytes32" },
28
+ { name: "watermarkTimestamp", type: "uint256" },
29
+ { name: "sessionId", type: "bytes32" }
20
30
  ]
21
31
  };
32
+ var CURRENCY_ADDRESSES = {
33
+ testnet: {
34
+ ETH: ZERO_ADDRESS,
35
+ USDT: "0xaA8E23Fb1079EA71e0a56F48a2aA51851D8433D0"
36
+ // USDT on Sepolia
37
+ },
38
+ mainnet: {
39
+ ETH: ZERO_ADDRESS,
40
+ USDT: "0xdAC17F958D2ee523a2206206994597C13D831ec7"
41
+ // USDT on Ethereum
42
+ }
43
+ };
22
44
 
23
45
  // src/protocols/NFTProtocol.ts
24
46
  var ZubariNFTProtocol = class {
25
47
  contractAddress;
26
48
  _marketplaceAddress;
27
49
  chainId;
28
- constructor(contractAddress, marketplaceAddress, chainId) {
50
+ network;
51
+ nonceCounter = 0;
52
+ constructor(contractAddress, marketplaceAddress, chainId, network = "testnet") {
29
53
  this.contractAddress = contractAddress;
30
54
  this._marketplaceAddress = marketplaceAddress;
31
55
  this.chainId = chainId;
56
+ this.network = network;
57
+ }
58
+ /**
59
+ * Convert human-readable price to wei
60
+ * @param price - Price in human-readable format (e.g., "1.5")
61
+ * @param currency - ETH (18 decimals) or USDT (6 decimals)
62
+ */
63
+ priceToWei(price, currency) {
64
+ const decimals = currency === "ETH" ? 18 : 6;
65
+ const [whole, fraction = ""] = price.split(".");
66
+ const paddedFraction = fraction.padEnd(decimals, "0").slice(0, decimals);
67
+ const wei = whole + paddedFraction;
68
+ return wei.replace(/^0+/, "") || "0";
69
+ }
70
+ /**
71
+ * Get currency address for the configured network
72
+ */
73
+ getCurrencyAddress(currency) {
74
+ return CURRENCY_ADDRESSES[this.network][currency];
75
+ }
76
+ /**
77
+ * Generate a unique nonce
78
+ */
79
+ generateNonce() {
80
+ return Date.now() * 1e3 + this.nonceCounter++;
81
+ }
82
+ /**
83
+ * Pad string to bytes32 format
84
+ */
85
+ toBytes32(value) {
86
+ if (value.startsWith("0x")) {
87
+ return value.padEnd(66, "0");
88
+ }
89
+ const hex = Buffer.from(value).toString("hex");
90
+ return "0x" + hex.padEnd(64, "0");
32
91
  }
33
92
  /**
34
93
  * Create a lazy mint voucher for off-chain NFT creation
35
94
  * The voucher can be redeemed on-chain when purchased
95
+ *
96
+ * @param params - Voucher creation parameters
97
+ * @param signer - Object with signTypedData method (ethers.js wallet or viem client)
36
98
  */
37
- async createLazyMintVoucher(metadata, creatorAddress, signer) {
38
- if (metadata.royaltyBps > PLATFORM_CONFIG.maxRoyaltyBps) {
99
+ async createLazyMintVoucher(params, signer) {
100
+ const { metadata, creatorAddress, price, currency, nonce, watermarking } = params;
101
+ const royaltyBps = metadata.royaltyBps ?? 500;
102
+ if (royaltyBps > PLATFORM_CONFIG.maxRoyaltyBps) {
39
103
  throw new Error(`Royalty cannot exceed ${PLATFORM_CONFIG.maxRoyaltyBps / 100}%`);
40
104
  }
105
+ if (!price || parseFloat(price) <= 0) {
106
+ throw new Error("Price must be greater than 0");
107
+ }
41
108
  const tokenId = this.generateTokenId();
42
109
  const deadline = Math.floor(Date.now() / 1e3) + PLATFORM_CONFIG.voucherValiditySecs;
110
+ const priceInWei = this.priceToWei(price, currency);
111
+ const currencyAddress = this.getCurrencyAddress(currency);
112
+ const voucherNonce = nonce ?? this.generateNonce();
113
+ const watermarkTimestamp = watermarking ? Math.floor(Date.now() / 1e3) : 0;
114
+ const contentHash = watermarking ? this.toBytes32(watermarking.contentHash) : ZERO_ADDRESS.replace("0x", "0x" + "0".repeat(64)).slice(0, 66);
115
+ const userId = watermarking ? this.toBytes32(watermarking.userId) : ZERO_ADDRESS.replace("0x", "0x" + "0".repeat(64)).slice(0, 66);
116
+ const sessionId = watermarking ? this.toBytes32(watermarking.sessionId) : ZERO_ADDRESS.replace("0x", "0x" + "0".repeat(64)).slice(0, 66);
43
117
  const voucherData = {
44
118
  tokenId,
45
119
  uri: metadata.image,
46
120
  // Will be IPFS URI
47
121
  creator: creatorAddress,
48
- royaltyBps: metadata.royaltyBps,
49
- deadline
122
+ royaltyBps,
123
+ // Use the validated/defaulted value
124
+ deadline,
125
+ price: priceInWei,
126
+ currency: currencyAddress,
127
+ nonce: voucherNonce,
128
+ contentHash,
129
+ userId,
130
+ watermarkTimestamp,
131
+ sessionId
50
132
  };
51
133
  const domain = {
52
134
  ...NFT_VOUCHER_DOMAIN,
@@ -55,10 +137,37 @@ var ZubariNFTProtocol = class {
55
137
  };
56
138
  const signature = await signer.signTypedData(domain, NFT_VOUCHER_TYPES, voucherData);
57
139
  return {
58
- ...voucherData,
59
- signature
140
+ tokenId,
141
+ uri: metadata.image,
142
+ creator: creatorAddress,
143
+ royaltyBps,
144
+ // Use the validated/defaulted value
145
+ deadline,
146
+ signature,
147
+ price: priceInWei,
148
+ currency: currencyAddress,
149
+ nonce: voucherNonce,
150
+ contentHash: watermarking ? contentHash : void 0,
151
+ userId: watermarking ? userId : void 0,
152
+ watermarkTimestamp: watermarking ? watermarkTimestamp : void 0,
153
+ sessionId: watermarking ? sessionId : void 0
60
154
  };
61
155
  }
156
+ /**
157
+ * @deprecated Use createLazyMintVoucher(params, signer) instead
158
+ * Legacy method for backward compatibility
159
+ */
160
+ async createLazyMintVoucherLegacy(metadata, creatorAddress, signer) {
161
+ return this.createLazyMintVoucher(
162
+ {
163
+ metadata,
164
+ creatorAddress,
165
+ price: "0.01",
166
+ currency: "ETH"
167
+ },
168
+ signer
169
+ );
170
+ }
62
171
  /**
63
172
  * Redeem a lazy mint voucher to mint the NFT on-chain
64
173
  */