|
1 | 1 | # NFD SDK Utilities
|
2 | 2 |
|
3 |
| -## Error Parser |
| 3 | +The NFD SDK provides utility functions to help developers work with NFDs and handle errors when interacting with the Algorand blockchain. |
4 | 4 |
|
5 |
| -The error parser utility provides user-friendly error messages for common Algorand transaction errors. It helps developers and users understand what went wrong when a transaction fails, without having to decipher the complex error messages returned by the Algorand blockchain. |
| 5 | +## Importing Utilities |
6 | 6 |
|
7 |
| -### Usage |
| 7 | +All public utility functions can be imported directly from the main package: |
8 | 8 |
|
9 | 9 | ```typescript
|
10 |
| -import { parseTransactionError, withErrorParsing } from '@txnlab/nfd-sdk' |
| 10 | +import { |
| 11 | + // NFD utilities |
| 12 | + isValidName, |
| 13 | + isSegmentName, |
| 14 | + extractParentName, |
| 15 | + getNfdBasename, |
| 16 | + isSegmentMintingUnlocked, |
| 17 | + canMintSegment, |
| 18 | + |
| 19 | + // Error handling utilities |
| 20 | + parseTransactionError, |
| 21 | + withErrorParsing, |
| 22 | +} from '@txnlab/nfd-sdk' |
| 23 | +``` |
| 24 | + |
| 25 | +## NFD Utilities |
| 26 | + |
| 27 | +### `isValidName(name: string): boolean` |
| 28 | + |
| 29 | +Checks if a string is a valid NFD name according to the naming rules. |
11 | 30 |
|
12 |
| -// Basic usage |
| 31 | +```typescript |
| 32 | +import { isValidName } from '@txnlab/nfd-sdk' |
| 33 | + |
| 34 | +// Check if a name is valid |
| 35 | +const isValid = isValidName('alice.algo') |
| 36 | +// Returns true |
| 37 | +``` |
| 38 | + |
| 39 | +### `isSegmentName(name: string): boolean` |
| 40 | + |
| 41 | +Determines if a name is a segment NFD. A segment is a child NFD that is created under a parent NFD (e.g., 'sub.alice.algo' is a segment of 'alice.algo'). Segments are sovereign NFDs with all functionality once minted. |
| 42 | + |
| 43 | +```typescript |
| 44 | +import { isSegmentName } from '@txnlab/nfd-sdk' |
| 45 | + |
| 46 | +// Check if a name is a segment |
| 47 | +const isSegment = isSegmentName('sub.alice.algo') |
| 48 | +// Returns true |
| 49 | +``` |
| 50 | + |
| 51 | +### `extractParentName(segmentName: string): string` |
| 52 | + |
| 53 | +Extracts the parent name from a segment name. |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { extractParentName } from '@txnlab/nfd-sdk' |
| 57 | + |
| 58 | +// Get the parent name |
| 59 | +const parentName = extractParentName('sub.alice.algo') |
| 60 | +// Returns 'alice.algo' |
| 61 | +``` |
| 62 | + |
| 63 | +### `getNfdBasename(name: string): string` |
| 64 | + |
| 65 | +Gets the base name of an NFD (without the .algo extension). |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { getNfdBasename } from '@txnlab/nfd-sdk' |
| 69 | + |
| 70 | +// Get the base name |
| 71 | +const baseName = getNfdBasename('alice.algo') |
| 72 | +// Returns 'alice' |
| 73 | +``` |
| 74 | + |
| 75 | +### `isSegmentMintingUnlocked(nfd: Nfd | null): boolean` |
| 76 | + |
| 77 | +Checks if segment minting is unlocked for a parent/root NFD. This determines whether anyone besides the owner can mint segments under this NFD. Note that the owner of the parent NFD always has permission to mint segments regardless of this setting. |
| 78 | + |
| 79 | +```typescript |
| 80 | +import { isSegmentMintingUnlocked } from '@txnlab/nfd-sdk' |
| 81 | + |
| 82 | +// Check if segment minting is unlocked for a parent NFD |
| 83 | +const isUnlocked = isSegmentMintingUnlocked(parentNfd) |
| 84 | +``` |
| 85 | + |
| 86 | +### `canMintSegment(nfd: Nfd | null, callerAddress: string): boolean` |
| 87 | + |
| 88 | +Checks if the caller is authorized to mint a segment for the given parent NFD. |
| 89 | + |
| 90 | +```typescript |
| 91 | +import { canMintSegment } from '@txnlab/nfd-sdk' |
| 92 | + |
| 93 | +// Check if the caller can mint a segment |
| 94 | +const canMint = canMintSegment(parentNfd, userAddress) |
| 95 | +``` |
| 96 | + |
| 97 | +## Error Handling Utilities |
| 98 | + |
| 99 | +### `parseTransactionError(error: unknown): string` |
| 100 | + |
| 101 | +Parses an error message and returns a user-friendly version. |
| 102 | + |
| 103 | +```typescript |
| 104 | +import { parseTransactionError } from '@txnlab/nfd-sdk' |
| 105 | + |
| 106 | +// Parse an error to get a user-friendly message |
13 | 107 | try {
|
14 |
| - // Some operation that might throw an error |
15 | 108 | await nfd.setSigner(addr, signer).manage(nfdName).linkAddress(address)
|
16 | 109 | } catch (error) {
|
17 |
| - // Parse the error to get a user-friendly message |
18 | 110 | const friendlyError = parseTransactionError(error)
|
19 | 111 | console.error(friendlyError)
|
20 | 112 | }
|
| 113 | +``` |
| 114 | + |
| 115 | +### `withErrorParsing<T>(fn: () => Promise<T>): Promise<T>` |
| 116 | + |
| 117 | +A wrapper function that automatically parses errors thrown by the wrapped function. |
| 118 | + |
| 119 | +```typescript |
| 120 | +import { withErrorParsing } from '@txnlab/nfd-sdk' |
21 | 121 |
|
22 |
| -// Using the wrapper function |
23 | 122 | const safeFunction = withErrorParsing(async () => {
|
24 |
| - // Some operation that might throw an error |
25 | 123 | return await nfd.setSigner(addr, signer).manage(nfdName).linkAddress(address)
|
26 | 124 | })
|
27 | 125 |
|
|
0 commit comments