|
| 1 | +/** |
| 2 | + * This class was copied from cashu-ts v2.7.2 and extended with the following methods: |
| 3 | + * - get iconUrl |
| 4 | + * - get internalMeltsOnly |
| 5 | + * |
| 6 | + * As of cashu-ts v2.7.2, the MintInfo class is not exported, so we need to copy it here in order to extend it. |
| 7 | + */ |
| 8 | + |
| 9 | +import type { |
| 10 | + GetInfoResponse, |
| 11 | + MPPMethod, |
| 12 | + SwapMethod, |
| 13 | + WebSocketSupport, |
| 14 | +} from '@cashu/cashu-ts'; |
| 15 | + |
| 16 | +/** |
| 17 | + * A class that represents the data fetched from the mint's |
| 18 | + * [NUT-06 info endpoint](https://github.com/cashubtc/nuts/blob/main/06.md) |
| 19 | + */ |
| 20 | +export class ExtendedMintInfo { |
| 21 | + private readonly _mintInfo: GetInfoResponse; |
| 22 | + private readonly _protectedEnpoints?: { |
| 23 | + cache: { |
| 24 | + [url: string]: boolean; |
| 25 | + }; |
| 26 | + apiReturn: Array<{ |
| 27 | + method: 'GET' | 'POST'; |
| 28 | + regex: RegExp; |
| 29 | + cachedValue?: boolean; |
| 30 | + }>; |
| 31 | + }; |
| 32 | + |
| 33 | + constructor(info: GetInfoResponse) { |
| 34 | + this._mintInfo = info; |
| 35 | + if (info.nuts[22]) { |
| 36 | + this._protectedEnpoints = { |
| 37 | + cache: {}, |
| 38 | + apiReturn: info.nuts[22].protected_endpoints.map((o) => ({ |
| 39 | + method: o.method, |
| 40 | + regex: new RegExp(o.path), |
| 41 | + })), |
| 42 | + }; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + isSupported(num: 4 | 5): { disabled: boolean; params: SwapMethod[] }; |
| 47 | + isSupported(num: 7 | 8 | 9 | 10 | 11 | 12 | 14 | 20): { supported: boolean }; |
| 48 | + isSupported(num: 17): { supported: boolean; params?: WebSocketSupport[] }; |
| 49 | + isSupported(num: 15): { supported: boolean; params?: MPPMethod[] }; |
| 50 | + isSupported(num: number) { |
| 51 | + switch (num) { |
| 52 | + case 4: |
| 53 | + case 5: { |
| 54 | + return this.checkMintMelt(num); |
| 55 | + } |
| 56 | + case 7: |
| 57 | + case 8: |
| 58 | + case 9: |
| 59 | + case 10: |
| 60 | + case 11: |
| 61 | + case 12: |
| 62 | + case 14: |
| 63 | + case 20: { |
| 64 | + return this.checkGenericNut(num); |
| 65 | + } |
| 66 | + case 17: { |
| 67 | + return this.checkNut17(); |
| 68 | + } |
| 69 | + case 15: { |
| 70 | + return this.checkNut15(); |
| 71 | + } |
| 72 | + default: { |
| 73 | + throw new Error('nut is not supported by cashu-ts'); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + requiresBlindAuthToken(path: string) { |
| 79 | + if (!this._protectedEnpoints) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + if (typeof this._protectedEnpoints.cache[path] === 'boolean') { |
| 83 | + return this._protectedEnpoints.cache[path]; |
| 84 | + } |
| 85 | + const isProtectedEndpoint = this._protectedEnpoints.apiReturn.some((e) => |
| 86 | + e.regex.test(path), |
| 87 | + ); |
| 88 | + this._protectedEnpoints.cache[path] = isProtectedEndpoint; |
| 89 | + return isProtectedEndpoint; |
| 90 | + } |
| 91 | + |
| 92 | + private checkGenericNut(num: 7 | 8 | 9 | 10 | 11 | 12 | 14 | 20) { |
| 93 | + if (this._mintInfo.nuts[num]?.supported) { |
| 94 | + return { supported: true }; |
| 95 | + } |
| 96 | + return { supported: false }; |
| 97 | + } |
| 98 | + private checkMintMelt(num: 4 | 5) { |
| 99 | + const mintMeltInfo = this._mintInfo.nuts[num]; |
| 100 | + if ( |
| 101 | + mintMeltInfo && |
| 102 | + mintMeltInfo.methods.length > 0 && |
| 103 | + !mintMeltInfo.disabled |
| 104 | + ) { |
| 105 | + return { disabled: false, params: mintMeltInfo.methods }; |
| 106 | + } |
| 107 | + return { disabled: true, params: mintMeltInfo.methods }; |
| 108 | + } |
| 109 | + private checkNut17() { |
| 110 | + if ( |
| 111 | + this._mintInfo.nuts[17] && |
| 112 | + this._mintInfo.nuts[17].supported.length > 0 |
| 113 | + ) { |
| 114 | + return { supported: true, params: this._mintInfo.nuts[17].supported }; |
| 115 | + } |
| 116 | + return { supported: false }; |
| 117 | + } |
| 118 | + private checkNut15() { |
| 119 | + if (this._mintInfo.nuts[15] && this._mintInfo.nuts[15].methods.length > 0) { |
| 120 | + return { supported: true, params: this._mintInfo.nuts[15].methods }; |
| 121 | + } |
| 122 | + return { supported: false }; |
| 123 | + } |
| 124 | + |
| 125 | + get contact() { |
| 126 | + return this._mintInfo.contact; |
| 127 | + } |
| 128 | + |
| 129 | + get description() { |
| 130 | + return this._mintInfo.description; |
| 131 | + } |
| 132 | + |
| 133 | + get description_long() { |
| 134 | + return this._mintInfo.description_long; |
| 135 | + } |
| 136 | + |
| 137 | + get name() { |
| 138 | + return this._mintInfo.name; |
| 139 | + } |
| 140 | + |
| 141 | + get pubkey() { |
| 142 | + return this._mintInfo.pubkey; |
| 143 | + } |
| 144 | + |
| 145 | + get nuts() { |
| 146 | + return this._mintInfo.nuts; |
| 147 | + } |
| 148 | + |
| 149 | + get version() { |
| 150 | + return this._mintInfo.version; |
| 151 | + } |
| 152 | + |
| 153 | + get motd() { |
| 154 | + return this._mintInfo.motd; |
| 155 | + } |
| 156 | + |
| 157 | + // Below methods are added in addition to what the cashu-ts MintInfo class provides |
| 158 | + |
| 159 | + get iconUrl() { |
| 160 | + return this._mintInfo.icon_url; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Whether the mint only allows internal melts. |
| 165 | + * |
| 166 | + * NOTE: This flag is not currently defined in the NUTs. |
| 167 | + * Internal melts only is a feature that we have added to agicash mints |
| 168 | + * for creating a closed-loop mint. |
| 169 | + */ |
| 170 | + get internalMeltsOnly() { |
| 171 | + const methods = this._mintInfo.nuts[5].methods as (SwapMethod & { |
| 172 | + options?: { internal_melts_only?: boolean }; |
| 173 | + })[]; |
| 174 | + return methods.some( |
| 175 | + (method) => method.options?.internal_melts_only === true, |
| 176 | + ); |
| 177 | + } |
| 178 | +} |
0 commit comments