@totemsdk/core 1.0.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.
Files changed (175) hide show
  1. package/dist/Streamable.d.ts +268 -0
  2. package/dist/Streamable.js +416 -0
  3. package/dist/adapters/index.d.ts +190 -0
  4. package/dist/adapters/index.js +107 -0
  5. package/dist/adapters/types.d.ts +149 -0
  6. package/dist/adapters/types.js +67 -0
  7. package/dist/binio.d.ts +9 -0
  8. package/dist/binio.js +30 -0
  9. package/dist/bip39.d.ts +91 -0
  10. package/dist/bip39.js +417 -0
  11. package/dist/derive.d.ts +2 -0
  12. package/dist/derive.js +9 -0
  13. package/dist/index.d.ts +30 -0
  14. package/dist/index.js +53 -0
  15. package/dist/javaStreamables.d.ts +233 -0
  16. package/dist/javaStreamables.js +332 -0
  17. package/dist/lease/LeaseMonitor.d.ts +44 -0
  18. package/dist/lease/LeaseMonitor.js +107 -0
  19. package/dist/lease/LeaseStore.d.ts +57 -0
  20. package/dist/lease/LeaseStore.js +144 -0
  21. package/dist/lease/WatermarkStore.d.ts +61 -0
  22. package/dist/lease/WatermarkStore.js +202 -0
  23. package/dist/lease/index.d.ts +14 -0
  24. package/dist/lease/index.js +13 -0
  25. package/dist/lease-client.d.ts +31 -0
  26. package/dist/lease-client.js +39 -0
  27. package/dist/minima32.d.ts +18 -0
  28. package/dist/minima32.js +112 -0
  29. package/dist/minimaWireSerializer.d.ts +60 -0
  30. package/dist/minimaWireSerializer.js +71 -0
  31. package/dist/mmr.d.ts +156 -0
  32. package/dist/mmr.js +342 -0
  33. package/dist/mx.d.ts +1 -0
  34. package/dist/mx.js +1 -0
  35. package/dist/params.d.ts +29 -0
  36. package/dist/params.js +16 -0
  37. package/dist/script.d.ts +7 -0
  38. package/dist/script.js +22 -0
  39. package/dist/scripts/contract-helpers.d.ts +252 -0
  40. package/dist/scripts/contract-helpers.js +394 -0
  41. package/dist/scripts/dapp-types.d.ts +119 -0
  42. package/dist/scripts/dapp-types.js +72 -0
  43. package/dist/scripts/index.d.ts +4 -0
  44. package/dist/scripts/index.js +4 -0
  45. package/dist/scripts/types.d.ts +81 -0
  46. package/dist/scripts/types.js +145 -0
  47. package/dist/scripts/witness-serializer.d.ts +29 -0
  48. package/dist/scripts/witness-serializer.js +288 -0
  49. package/dist/transaction.d.ts +106 -0
  50. package/dist/transaction.js +284 -0
  51. package/dist/treekey.d.ts +383 -0
  52. package/dist/treekey.js +762 -0
  53. package/dist/tx/TransactionLifecycle.d.ts +55 -0
  54. package/dist/tx/TransactionLifecycle.js +189 -0
  55. package/dist/tx/TransactionReceiptStore.d.ts +36 -0
  56. package/dist/tx/TransactionReceiptStore.js +85 -0
  57. package/dist/tx/TransactionService.d.ts +50 -0
  58. package/dist/tx/TransactionService.js +178 -0
  59. package/dist/tx/index.d.ts +13 -0
  60. package/dist/tx/index.js +12 -0
  61. package/dist/tx/types.d.ts +114 -0
  62. package/dist/tx/types.js +5 -0
  63. package/dist/util.d.ts +1 -0
  64. package/dist/util.js +1 -0
  65. package/dist/utils.d.ts +8 -0
  66. package/dist/utils.js +35 -0
  67. package/dist/verify.d.ts +18 -0
  68. package/dist/verify.js +162 -0
  69. package/dist/version.d.ts +12 -0
  70. package/dist/version.js +12 -0
  71. package/dist/wots.d.ts +232 -0
  72. package/dist/wots.js +538 -0
  73. package/package.json +65 -0
  74. package/src/Streamable.d.ts +265 -0
  75. package/src/Streamable.js +431 -0
  76. package/src/Streamable.ts +567 -0
  77. package/src/__tests__/Streamable.parity.test.ts +422 -0
  78. package/src/__tests__/perAddressDerivation.parity.test.ts +152 -0
  79. package/src/__tests__/wots-parity.test.ts +289 -0
  80. package/src/adapters/index.d.ts +190 -0
  81. package/src/adapters/index.js +117 -0
  82. package/src/adapters/index.ts +301 -0
  83. package/src/adapters/types.ts +235 -0
  84. package/src/addr.oracle.test.ts +25 -0
  85. package/src/address.test.ts +55 -0
  86. package/src/binio.ts +21 -0
  87. package/src/bip39.d.ts +91 -0
  88. package/src/bip39.js +427 -0
  89. package/src/bip39.ts +432 -0
  90. package/src/derive.d.ts +2 -0
  91. package/src/derive.js +13 -0
  92. package/src/derive.ts +11 -0
  93. package/src/index.d.ts +26 -0
  94. package/src/index.js +128 -0
  95. package/src/index.test.ts +5 -0
  96. package/src/index.ts +206 -0
  97. package/src/javaStreamables.d.ts +233 -0
  98. package/src/javaStreamables.js +328 -0
  99. package/src/javaStreamables.ts +403 -0
  100. package/src/lease/LeaseMonitor.d.ts +44 -0
  101. package/src/lease/LeaseMonitor.js +111 -0
  102. package/src/lease/LeaseMonitor.ts +157 -0
  103. package/src/lease/LeaseStore.d.ts +57 -0
  104. package/src/lease/LeaseStore.js +148 -0
  105. package/src/lease/LeaseStore.ts +203 -0
  106. package/src/lease/WatermarkStore.d.ts +61 -0
  107. package/src/lease/WatermarkStore.js +206 -0
  108. package/src/lease/WatermarkStore.ts +270 -0
  109. package/src/lease/index.d.ts +14 -0
  110. package/src/lease/index.js +22 -0
  111. package/src/lease/index.ts +41 -0
  112. package/src/lease-client.d.ts +31 -0
  113. package/src/lease-client.js +44 -0
  114. package/src/lease-client.ts +69 -0
  115. package/src/minima32.d.ts +10 -0
  116. package/src/minima32.js +113 -0
  117. package/src/minima32.test.ts +11 -0
  118. package/src/minima32.ts +127 -0
  119. package/src/minimaWireSerializer.d.ts +189 -0
  120. package/src/minimaWireSerializer.js +311 -0
  121. package/src/minimaWireSerializer.test.ts +466 -0
  122. package/src/minimaWireSerializer.ts +117 -0
  123. package/src/mmr.d.ts +153 -0
  124. package/src/mmr.js +351 -0
  125. package/src/mmr.oracle.test.ts +28 -0
  126. package/src/mmr.ts +429 -0
  127. package/src/mx.test.ts +25 -0
  128. package/src/mx.ts +1 -0
  129. package/src/params.d.ts +29 -0
  130. package/src/params.d.ts.map +1 -0
  131. package/src/params.js +20 -0
  132. package/src/params.js.map +1 -0
  133. package/src/params.ts +44 -0
  134. package/src/script.d.ts +7 -0
  135. package/src/script.js +27 -0
  136. package/src/script.ts +24 -0
  137. package/src/scripts/contract-helpers.ts +609 -0
  138. package/src/scripts/dapp-types.ts +240 -0
  139. package/src/scripts/index.ts +22 -0
  140. package/src/scripts/types.ts +247 -0
  141. package/src/scripts/witness-serializer.ts +384 -0
  142. package/src/transaction.ts +430 -0
  143. package/src/treekey.d.ts +383 -0
  144. package/src/treekey.js +778 -0
  145. package/src/treekey.parity.test.ts +459 -0
  146. package/src/treekey.ts +979 -0
  147. package/src/tx/TransactionLifecycle.d.ts +55 -0
  148. package/src/tx/TransactionLifecycle.js +188 -0
  149. package/src/tx/TransactionLifecycle.ts +280 -0
  150. package/src/tx/TransactionReceiptStore.d.ts +36 -0
  151. package/src/tx/TransactionReceiptStore.js +89 -0
  152. package/src/tx/TransactionReceiptStore.ts +112 -0
  153. package/src/tx/TransactionService.d.ts +31 -0
  154. package/src/tx/TransactionService.js +140 -0
  155. package/src/tx/TransactionService.ts +250 -0
  156. package/src/tx/index.d.ts +13 -0
  157. package/src/tx/index.js +20 -0
  158. package/src/tx/index.ts +44 -0
  159. package/src/tx/types.d.ts +80 -0
  160. package/src/tx/types.js +6 -0
  161. package/src/tx/types.ts +127 -0
  162. package/src/util.d.ts +1 -0
  163. package/src/util.js +5 -0
  164. package/src/util.ts +1 -0
  165. package/src/utils.d.ts +8 -0
  166. package/src/utils.js +42 -0
  167. package/src/utils.ts +38 -0
  168. package/src/verify.ts +209 -0
  169. package/src/version.ts +13 -0
  170. package/src/wots.d.ts +224 -0
  171. package/src/wots.d.ts.map +1 -0
  172. package/src/wots.js +559 -0
  173. package/src/wots.js.map +1 -0
  174. package/src/wots.ts +636 -0
  175. package/test-vectors.json +45 -0
@@ -0,0 +1,240 @@
1
+ export const TOTEM_SEND_TRANSACTION_VERSION = 1;
2
+
3
+ export type DAppTransactionIntent =
4
+ | 'send'
5
+ | 'token_send'
6
+ | 'swap'
7
+ | 'liquidity_add'
8
+ | 'liquidity_remove'
9
+ | 'contract_call'
10
+ | 'multisig'
11
+ | 'timelock'
12
+ | 'htlc'
13
+ | 'custom';
14
+
15
+ export interface DAppStateVariable {
16
+ port: number;
17
+ value: string;
18
+ type?: 'number' | 'string' | 'hex' | 'address';
19
+ }
20
+
21
+ export interface DAppTransactionOutput {
22
+ address: string;
23
+ amount: string;
24
+ tokenId?: string;
25
+ state?: DAppStateVariable[];
26
+ storeState?: boolean;
27
+ script?: string;
28
+ scriptRef?: string;
29
+ }
30
+
31
+ export interface DAppTransactionInput {
32
+ coinId: string;
33
+ address?: string;
34
+ amount?: string;
35
+ tokenId?: string;
36
+ }
37
+
38
+ export interface DAppSwapParams {
39
+ fromTokenId: string;
40
+ toTokenId: string;
41
+ amountIn: string;
42
+ minAmountOut: string;
43
+ slippageBps?: number;
44
+ poolAddress?: string;
45
+ }
46
+
47
+ export interface DAppLiquidityParams {
48
+ poolAddress: string;
49
+ tokenAId: string;
50
+ tokenBId: string;
51
+ amountA?: string;
52
+ amountB?: string;
53
+ lpTokenAmount?: string;
54
+ }
55
+
56
+ export interface DAppMultisigParams {
57
+ requiredSignatures: number;
58
+ publicKeys: string[];
59
+ timeoutBlocks?: number;
60
+ }
61
+
62
+ export interface DAppTimelockParams {
63
+ releaseBlock: number;
64
+ fallbackAddress?: string;
65
+ }
66
+
67
+ export interface DAppHtlcParams {
68
+ hashlock: string;
69
+ timeoutBlocks: number;
70
+ recipientAddress: string;
71
+ refundAddress: string;
72
+ }
73
+
74
+ export interface DAppContractCallParams {
75
+ contractAddress: string;
76
+ method?: string;
77
+ args?: Record<string, string>;
78
+ script?: string;
79
+ }
80
+
81
+ export interface TotemSendTransactionRequest {
82
+ version: typeof TOTEM_SEND_TRANSACTION_VERSION;
83
+ intent: DAppTransactionIntent;
84
+ outputs: DAppTransactionOutput[];
85
+ inputs?: DAppTransactionInput[];
86
+ burn?: string;
87
+ memo?: string;
88
+ metadata?: {
89
+ appName?: string;
90
+ description?: string;
91
+ iconUrl?: string;
92
+ };
93
+ options?: {
94
+ verifyWithTotemidea?: boolean;
95
+ skipPreview?: boolean;
96
+ useSourceAddress?: string;
97
+ excludeAddresses?: string[];
98
+ };
99
+ swap?: DAppSwapParams;
100
+ liquidity?: DAppLiquidityParams;
101
+ multisig?: DAppMultisigParams;
102
+ timelock?: DAppTimelockParams;
103
+ htlc?: DAppHtlcParams;
104
+ contract?: DAppContractCallParams;
105
+ }
106
+
107
+ export interface TotemSendTransactionResponse {
108
+ success: boolean;
109
+ txpowid?: string;
110
+ status?: 'pending' | 'submitted' | 'confirmed' | 'rejected';
111
+ artifactId?: string;
112
+ digestHex?: string;
113
+ error?: string;
114
+ errorCode?: TotemTransactionErrorCode;
115
+ verification?: {
116
+ totemideaValid?: boolean;
117
+ totemideaWarnings?: string[];
118
+ totemideaNotes?: string[];
119
+ };
120
+ }
121
+
122
+ export type TotemTransactionErrorCode =
123
+ | 'INVALID_REQUEST'
124
+ | 'INSUFFICIENT_FUNDS'
125
+ | 'PERMISSION_DENIED'
126
+ | 'USER_REJECTED'
127
+ | 'SITE_NOT_CONNECTED'
128
+ | 'SPENDING_LIMIT_EXCEEDED'
129
+ | 'TOKEN_NOT_ALLOWED'
130
+ | 'VERIFICATION_FAILED'
131
+ | 'BUILD_FAILED'
132
+ | 'SIGN_FAILED'
133
+ | 'BROADCAST_FAILED'
134
+ | 'TIMEOUT';
135
+
136
+ export interface SiteTransactionPermission {
137
+ origin: string;
138
+ grantedAt: number;
139
+ expiresAt: number;
140
+ scopes: TransactionScope[];
141
+ }
142
+
143
+ export interface TransactionScope {
144
+ tokenId: string;
145
+ tokenSymbol?: string;
146
+ maxAmountPerTx: string;
147
+ maxDailyAmount: string;
148
+ dailyUsed: string;
149
+ lastResetDate: string;
150
+ allowedIntents: DAppTransactionIntent[];
151
+ }
152
+
153
+ export function validateSendTransactionRequest(request: unknown): { valid: boolean; errors: string[] } {
154
+ const errors: string[] = [];
155
+
156
+ if (!request || typeof request !== 'object') {
157
+ return { valid: false, errors: ['Request must be an object'] };
158
+ }
159
+
160
+ const req = request as Record<string, unknown>;
161
+
162
+ if (req.version !== TOTEM_SEND_TRANSACTION_VERSION) {
163
+ errors.push(`Invalid version: expected ${TOTEM_SEND_TRANSACTION_VERSION}, got ${req.version}`);
164
+ }
165
+
166
+ const validIntents: DAppTransactionIntent[] = [
167
+ 'send', 'token_send', 'swap', 'liquidity_add', 'liquidity_remove',
168
+ 'contract_call', 'multisig', 'timelock', 'htlc', 'custom'
169
+ ];
170
+ if (!validIntents.includes(req.intent as DAppTransactionIntent)) {
171
+ errors.push(`Invalid intent: ${req.intent}. Must be one of: ${validIntents.join(', ')}`);
172
+ }
173
+
174
+ if (!Array.isArray(req.outputs) || req.outputs.length === 0) {
175
+ errors.push('outputs must be a non-empty array');
176
+ } else {
177
+ for (let i = 0; i < req.outputs.length; i++) {
178
+ const output = req.outputs[i] as Record<string, unknown>;
179
+ if (!output.address || typeof output.address !== 'string') {
180
+ errors.push(`outputs[${i}].address is required and must be a string`);
181
+ }
182
+ if (!output.amount || typeof output.amount !== 'string') {
183
+ errors.push(`outputs[${i}].amount is required and must be a string`);
184
+ }
185
+ const amountNum = parseFloat(output.amount as string);
186
+ if (isNaN(amountNum) || amountNum <= 0) {
187
+ errors.push(`outputs[${i}].amount must be a positive number`);
188
+ }
189
+ }
190
+ }
191
+
192
+ if (req.burn !== undefined) {
193
+ const burnNum = parseFloat(req.burn as string);
194
+ if (isNaN(burnNum) || burnNum < 0) {
195
+ errors.push('burn must be a non-negative number string');
196
+ }
197
+ }
198
+
199
+ if (req.intent === 'swap' && !req.swap) {
200
+ errors.push('swap params required for swap intent');
201
+ }
202
+
203
+ if ((req.intent === 'liquidity_add' || req.intent === 'liquidity_remove') && !req.liquidity) {
204
+ errors.push('liquidity params required for liquidity intent');
205
+ }
206
+
207
+ if (req.intent === 'multisig' && !req.multisig) {
208
+ errors.push('multisig params required for multisig intent');
209
+ }
210
+
211
+ if (req.intent === 'timelock' && !req.timelock) {
212
+ errors.push('timelock params required for timelock intent');
213
+ }
214
+
215
+ if (req.intent === 'htlc' && !req.htlc) {
216
+ errors.push('htlc params required for htlc intent');
217
+ }
218
+
219
+ if (req.intent === 'contract_call' && !req.contract) {
220
+ errors.push('contract params required for contract_call intent');
221
+ }
222
+
223
+ return { valid: errors.length === 0, errors };
224
+ }
225
+
226
+ export function simpleTotemSendRequest(
227
+ to: string,
228
+ amount: string,
229
+ tokenId?: string
230
+ ): TotemSendTransactionRequest {
231
+ return {
232
+ version: TOTEM_SEND_TRANSACTION_VERSION,
233
+ intent: tokenId && tokenId !== '0x00' ? 'token_send' : 'send',
234
+ outputs: [{
235
+ address: to,
236
+ amount,
237
+ tokenId: tokenId || '0x00'
238
+ }]
239
+ };
240
+ }
@@ -0,0 +1,22 @@
1
+ export * from './types.js';
2
+ export * from './dapp-types.js';
3
+ export {
4
+ encodeMiniNumber,
5
+ encodeMiniData,
6
+ encodeMiniString,
7
+ serializeMMRProofChunk,
8
+ serializeScriptProofWithProof,
9
+ STATETYPE_HEX,
10
+ STATETYPE_NUMBER,
11
+ STATETYPE_STRING,
12
+ STATETYPE_BOOL,
13
+ encodeStateValue,
14
+ serializeStateVariables,
15
+ buildScriptProofFromDescriptor,
16
+ deduplicateScriptDescriptors,
17
+ serializeExtraScripts,
18
+ aggregateSignatures,
19
+ validateExternalSignature,
20
+ computeScriptAddress,
21
+ } from './witness-serializer.js';
22
+ export * from './contract-helpers.js';
@@ -0,0 +1,247 @@
1
+ import type { MMRData, MMRProofChunk, MMRProof } from '../mmr.js';
2
+ export type { MMRProof };
3
+
4
+ function kissHex(hex: string): string {
5
+ const raw = hex.startsWith('0x') || hex.startsWith('0X') ? hex.slice(2) : hex;
6
+ return '0x' + raw.toUpperCase();
7
+ }
8
+
9
+ export type StateVariableType = 'STATE' | 'PREVSTATE' | 'SAMESTATE';
10
+
11
+ export interface StateValue {
12
+ port: number;
13
+ value: string | bigint | Uint8Array | boolean;
14
+ type: 'bool' | 'number' | 'hex' | 'string';
15
+ }
16
+
17
+ export interface VerifyOutExpectation {
18
+ inputIndex: number | '@INPUT';
19
+ outputAddress: string;
20
+ amount: string | bigint;
21
+ tokenId: string;
22
+ keepState: boolean;
23
+ }
24
+
25
+ export type ScriptType =
26
+ | 'signedby'
27
+ | 'multisig'
28
+ | 'multisig_mofn'
29
+ | 'timelock'
30
+ | 'htlc'
31
+ | 'mast'
32
+ | 'exchange'
33
+ | 'vault'
34
+ | 'flashcash'
35
+ | 'slowcash'
36
+ | 'stateful'
37
+ | 'custom';
38
+
39
+ export interface ScriptDescriptor {
40
+ address: string;
41
+ scriptType: ScriptType;
42
+ script: string;
43
+ wotsRootPublicKey?: string;
44
+ mastProof?: MMRProof;
45
+ extraScripts?: Map<string, string>;
46
+ stateVariables?: StateValue[];
47
+ storeState?: boolean;
48
+ verifyOutExpectations?: VerifyOutExpectation[];
49
+ timelockBlock?: bigint;
50
+ htlcHash?: string;
51
+ htlcPreimage?: string;
52
+ multisigKeys?: string[];
53
+ multisigThreshold?: number;
54
+ externalSignatures?: ExternalSignature[];
55
+ }
56
+
57
+ export interface ExternalSignature {
58
+ publicKey: string;
59
+ signature: string;
60
+ proof?: MMRProof;
61
+ signatureType: 'wots' | 'standard';
62
+ validated?: boolean;
63
+ }
64
+
65
+ export interface ScriptCatalogEntry {
66
+ address: string;
67
+ script: string;
68
+ scriptType: ScriptType;
69
+ createdAt: number;
70
+ lastUsed: number;
71
+ }
72
+
73
+ export interface ScriptProofResult {
74
+ script: string;
75
+ proof: MMRProof;
76
+ serialized: Uint8Array;
77
+ }
78
+
79
+ export interface TransactionRoundState {
80
+ round: number;
81
+ previousRound: number;
82
+ preservedPorts: number[];
83
+ newStates: StateValue[];
84
+ }
85
+
86
+ export interface FlatMMRProofChunk {
87
+ isLeft: boolean;
88
+ data: Uint8Array;
89
+ }
90
+
91
+ export interface LegacyMMRProof {
92
+ blockTime: bigint;
93
+ proofChain: FlatMMRProofChunk[];
94
+ }
95
+
96
+ export function convertFlatChunkToSDK(chunk: FlatMMRProofChunk): MMRProofChunk {
97
+ return {
98
+ isLeft: chunk.isLeft,
99
+ mmrData: { data: chunk.data, value: 0n }
100
+ };
101
+ }
102
+
103
+ export function convertLegacyProofToSDK(legacy: LegacyMMRProof): { proof: MMRProof; blockTime: bigint } {
104
+ return {
105
+ proof: {
106
+ chunks: legacy.proofChain.map(convertFlatChunkToSDK)
107
+ },
108
+ blockTime: legacy.blockTime
109
+ };
110
+ }
111
+
112
+ export function createEmptyMMRProof(): MMRProof {
113
+ return { chunks: [] };
114
+ }
115
+
116
+ export function createSignedByDescriptor(address: string, wotsRootPublicKey: string): ScriptDescriptor {
117
+ return {
118
+ address,
119
+ scriptType: 'signedby',
120
+ script: `RETURN SIGNEDBY(${kissHex(wotsRootPublicKey)})`,
121
+ wotsRootPublicKey,
122
+ mastProof: createEmptyMMRProof(),
123
+ storeState: false
124
+ };
125
+ }
126
+
127
+ export function createMultisigDescriptor(address: string, publicKey1: string, publicKey2: string, ownPublicKey: string): ScriptDescriptor {
128
+ return {
129
+ address,
130
+ scriptType: 'multisig',
131
+ script: `RETURN SIGNEDBY(${kissHex(publicKey1)}) AND SIGNEDBY(${kissHex(publicKey2)})`,
132
+ wotsRootPublicKey: ownPublicKey,
133
+ multisigKeys: [publicKey1, publicKey2],
134
+ multisigThreshold: 2,
135
+ mastProof: createEmptyMMRProof(),
136
+ storeState: false
137
+ };
138
+ }
139
+
140
+ export function createMofNMultisigDescriptor(address: string, threshold: number, publicKeys: string[], ownPublicKey: string): ScriptDescriptor {
141
+ const formattedKeys = publicKeys.map(pk => kissHex(pk)).join(' ');
142
+ return {
143
+ address,
144
+ scriptType: 'multisig_mofn',
145
+ script: `RETURN MULTISIG(${threshold} ${formattedKeys})`,
146
+ wotsRootPublicKey: ownPublicKey,
147
+ multisigKeys: publicKeys,
148
+ multisigThreshold: threshold,
149
+ mastProof: createEmptyMMRProof(),
150
+ storeState: false
151
+ };
152
+ }
153
+
154
+ export function createTimelockDescriptor(address: string, publicKey: string, unlockBlock: bigint): ScriptDescriptor {
155
+ return {
156
+ address,
157
+ scriptType: 'timelock',
158
+ script: `RETURN SIGNEDBY(${kissHex(publicKey)}) AND @BLOCK GT ${unlockBlock}`,
159
+ wotsRootPublicKey: publicKey,
160
+ timelockBlock: unlockBlock,
161
+ mastProof: createEmptyMMRProof(),
162
+ storeState: false
163
+ };
164
+ }
165
+
166
+ export function createHTLCDescriptor(address: string, ownerPublicKey: string, recipientPublicKey: string, hashLock: string, timeoutBlock: bigint, isOwner: boolean, preimage?: string): ScriptDescriptor {
167
+ const script = `IF @BLOCK GT ${timeoutBlock} AND SIGNEDBY(${kissHex(ownerPublicKey)}) THEN RETURN TRUE ENDIF RETURN (SIGNEDBY(${kissHex(recipientPublicKey)}) AND SHA3(STATE(1)) EQ ${kissHex(hashLock)})`;
168
+ const descriptor: ScriptDescriptor = {
169
+ address,
170
+ scriptType: 'htlc',
171
+ script,
172
+ wotsRootPublicKey: isOwner ? ownerPublicKey : recipientPublicKey,
173
+ htlcHash: hashLock,
174
+ timelockBlock: timeoutBlock,
175
+ mastProof: createEmptyMMRProof(),
176
+ storeState: false
177
+ };
178
+ if (preimage) {
179
+ descriptor.htlcPreimage = preimage;
180
+ descriptor.stateVariables = [{ port: 1, value: preimage, type: 'string' }];
181
+ }
182
+ return descriptor;
183
+ }
184
+
185
+ export function createMASTDescriptor(address: string, rootHash: string, branchScript: string, branchProof: string, wotsPublicKey?: string): ScriptDescriptor {
186
+ return {
187
+ address,
188
+ scriptType: 'mast',
189
+ script: `MAST ${kissHex(rootHash)}`,
190
+ wotsRootPublicKey: wotsPublicKey,
191
+ extraScripts: new Map([[branchScript, branchProof]]),
192
+ mastProof: createEmptyMMRProof(),
193
+ storeState: false
194
+ };
195
+ }
196
+
197
+ export function createExchangeDescriptor(address: string, ownerPublicKey: string, desiredAddress: string, desiredAmount: string, desiredTokenId: string): ScriptDescriptor {
198
+ const ownerPk = kissHex(ownerPublicKey);
199
+ const script = `IF SIGNEDBY(PREVSTATE(0)) THEN RETURN TRUE ENDIF ASSERT VERIFYOUT(@INPUT PREVSTATE(1) PREVSTATE(2) PREVSTATE(3) TRUE) RETURN TRUE`;
200
+ return {
201
+ address,
202
+ scriptType: 'exchange',
203
+ script,
204
+ wotsRootPublicKey: ownerPublicKey,
205
+ stateVariables: [
206
+ { port: 0, value: ownerPk, type: 'hex' },
207
+ { port: 1, value: desiredAddress, type: 'hex' },
208
+ { port: 2, value: desiredAmount, type: 'number' },
209
+ { port: 3, value: desiredTokenId, type: 'hex' }
210
+ ],
211
+ verifyOutExpectations: [{
212
+ inputIndex: '@INPUT',
213
+ outputAddress: desiredAddress,
214
+ amount: desiredAmount,
215
+ tokenId: desiredTokenId,
216
+ keepState: true
217
+ }],
218
+ mastProof: createEmptyMMRProof(),
219
+ storeState: true
220
+ };
221
+ }
222
+
223
+ export function createFlashCashDescriptor(address: string, ownerPublicKey: string, interestMultiplier: string = '1.01'): ScriptDescriptor {
224
+ const script = `IF SIGNEDBY(PREVSTATE(1)) THEN RETURN TRUE ENDIF ASSERT SAMESTATE(1 1) RETURN VERIFYOUT(@INPUT @ADDRESS @AMOUNT*${interestMultiplier} @TOKENID TRUE)`;
225
+ return {
226
+ address,
227
+ scriptType: 'flashcash',
228
+ script,
229
+ wotsRootPublicKey: ownerPublicKey,
230
+ stateVariables: [{ port: 1, value: ownerPublicKey, type: 'hex' }],
231
+ mastProof: createEmptyMMRProof(),
232
+ storeState: true
233
+ };
234
+ }
235
+
236
+ export function createSlowCashDescriptor(address: string, ownerPublicKey: string, withdrawalPercent: string = '0.9', cooldownBlocks: bigint = 10000n): ScriptDescriptor {
237
+ const script = `IF @COINAGE LT ${cooldownBlocks} THEN RETURN FALSE ENDIF ASSERT SIGNEDBY(${kissHex(ownerPublicKey)}) AND VERIFYOUT(@INPUT @ADDRESS @AMOUNT*${withdrawalPercent} @TOKENID TRUE)`;
238
+ return {
239
+ address,
240
+ scriptType: 'slowcash',
241
+ script,
242
+ wotsRootPublicKey: ownerPublicKey,
243
+ timelockBlock: cooldownBlocks,
244
+ mastProof: createEmptyMMRProof(),
245
+ storeState: false
246
+ };
247
+ }