@bsv/sdk 1.1.33 → 1.2.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 (214) hide show
  1. package/dist/cjs/mod.js +4 -0
  2. package/dist/cjs/mod.js.map +1 -1
  3. package/dist/cjs/package.json +4 -3
  4. package/dist/cjs/src/auth/Certificate.js +163 -0
  5. package/dist/cjs/src/auth/Certificate.js.map +1 -0
  6. package/dist/cjs/src/auth/index.js +9 -0
  7. package/dist/cjs/src/auth/index.js.map +1 -0
  8. package/dist/cjs/src/compat/BSM.js +17 -7
  9. package/dist/cjs/src/compat/BSM.js.map +1 -1
  10. package/dist/cjs/src/compat/ECIES.js +17 -7
  11. package/dist/cjs/src/compat/ECIES.js.map +1 -1
  12. package/dist/cjs/src/compat/HD.js +17 -7
  13. package/dist/cjs/src/compat/HD.js.map +1 -1
  14. package/dist/cjs/src/compat/Mnemonic.js +17 -7
  15. package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
  16. package/dist/cjs/src/compat/index.js +17 -7
  17. package/dist/cjs/src/compat/index.js.map +1 -1
  18. package/dist/cjs/src/messages/index.js +17 -7
  19. package/dist/cjs/src/messages/index.js.map +1 -1
  20. package/dist/cjs/src/overlay-tools/LookupResolver.js +170 -0
  21. package/dist/cjs/src/overlay-tools/LookupResolver.js.map +1 -0
  22. package/dist/cjs/src/overlay-tools/OverlayAdminTokenTemplate.js +69 -0
  23. package/dist/cjs/src/overlay-tools/OverlayAdminTokenTemplate.js.map +1 -0
  24. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js +336 -0
  25. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js.map +1 -0
  26. package/dist/cjs/src/overlay-tools/index.js +29 -0
  27. package/dist/cjs/src/overlay-tools/index.js.map +1 -0
  28. package/dist/cjs/src/primitives/PrivateKey.js +17 -7
  29. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
  30. package/dist/cjs/src/primitives/TransactionSignature.js +17 -7
  31. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  32. package/dist/cjs/src/primitives/index.js +17 -7
  33. package/dist/cjs/src/primitives/index.js.map +1 -1
  34. package/dist/cjs/src/script/Spend.js +17 -7
  35. package/dist/cjs/src/script/Spend.js.map +1 -1
  36. package/dist/cjs/src/script/templates/PushDrop.js +218 -0
  37. package/dist/cjs/src/script/templates/PushDrop.js.map +1 -0
  38. package/dist/cjs/src/script/templates/index.js +3 -1
  39. package/dist/cjs/src/script/templates/index.js.map +1 -1
  40. package/dist/cjs/src/transaction/http/DefaultHttpClient.js +1 -1
  41. package/dist/cjs/src/transaction/http/DefaultHttpClient.js.map +1 -1
  42. package/dist/cjs/src/wallet/CachedKeyDeriver.js +177 -0
  43. package/dist/cjs/src/wallet/CachedKeyDeriver.js.map +1 -0
  44. package/dist/cjs/src/wallet/KeyDeriver.js +174 -0
  45. package/dist/cjs/src/wallet/KeyDeriver.js.map +1 -0
  46. package/dist/cjs/src/wallet/ProtoWallet.js +245 -0
  47. package/dist/cjs/src/wallet/ProtoWallet.js.map +1 -0
  48. package/dist/cjs/src/wallet/Wallet.interfaces.js +3 -0
  49. package/dist/cjs/src/wallet/Wallet.interfaces.js.map +1 -0
  50. package/dist/cjs/src/wallet/WalletClient.js +181 -0
  51. package/dist/cjs/src/wallet/WalletClient.js.map +1 -0
  52. package/dist/cjs/src/wallet/WalletError.js +28 -0
  53. package/dist/cjs/src/wallet/WalletError.js.map +1 -0
  54. package/dist/cjs/src/wallet/index.js +34 -0
  55. package/dist/cjs/src/wallet/index.js.map +1 -0
  56. package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js +45 -0
  57. package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js.map +1 -0
  58. package/dist/cjs/src/wallet/substrates/WalletWire.js +3 -0
  59. package/dist/cjs/src/wallet/substrates/WalletWire.js.map +1 -0
  60. package/dist/cjs/src/wallet/substrates/WalletWireCalls.js +36 -0
  61. package/dist/cjs/src/wallet/substrates/WalletWireCalls.js.map +1 -0
  62. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js +1821 -0
  63. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js.map +1 -0
  64. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +1305 -0
  65. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -0
  66. package/dist/cjs/src/wallet/substrates/XDM.js +130 -0
  67. package/dist/cjs/src/wallet/substrates/XDM.js.map +1 -0
  68. package/dist/cjs/src/wallet/substrates/index.js +33 -0
  69. package/dist/cjs/src/wallet/substrates/index.js.map +1 -0
  70. package/dist/cjs/src/wallet/substrates/window.CWI.js +102 -0
  71. package/dist/cjs/src/wallet/substrates/window.CWI.js.map +1 -0
  72. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  73. package/dist/esm/mod.js +4 -0
  74. package/dist/esm/mod.js.map +1 -1
  75. package/dist/esm/src/auth/Certificate.js +185 -0
  76. package/dist/esm/src/auth/Certificate.js.map +1 -0
  77. package/dist/esm/src/auth/index.js +2 -0
  78. package/dist/esm/src/auth/index.js.map +1 -0
  79. package/dist/esm/src/overlay-tools/LookupResolver.js +167 -0
  80. package/dist/esm/src/overlay-tools/LookupResolver.js.map +1 -0
  81. package/dist/esm/src/overlay-tools/OverlayAdminTokenTemplate.js +64 -0
  82. package/dist/esm/src/overlay-tools/OverlayAdminTokenTemplate.js.map +1 -0
  83. package/dist/esm/src/overlay-tools/SHIPBroadcaster.js +335 -0
  84. package/dist/esm/src/overlay-tools/SHIPBroadcaster.js.map +1 -0
  85. package/dist/esm/src/overlay-tools/index.js +6 -0
  86. package/dist/esm/src/overlay-tools/index.js.map +1 -0
  87. package/dist/esm/src/script/templates/PushDrop.js +215 -0
  88. package/dist/esm/src/script/templates/PushDrop.js.map +1 -0
  89. package/dist/esm/src/script/templates/index.js +1 -0
  90. package/dist/esm/src/script/templates/index.js.map +1 -1
  91. package/dist/esm/src/transaction/http/DefaultHttpClient.js +1 -1
  92. package/dist/esm/src/transaction/http/DefaultHttpClient.js.map +1 -1
  93. package/dist/esm/src/wallet/CachedKeyDeriver.js +174 -0
  94. package/dist/esm/src/wallet/CachedKeyDeriver.js.map +1 -0
  95. package/dist/esm/src/wallet/KeyDeriver.js +172 -0
  96. package/dist/esm/src/wallet/KeyDeriver.js.map +1 -0
  97. package/dist/esm/src/wallet/ProtoWallet.js +207 -0
  98. package/dist/esm/src/wallet/ProtoWallet.js.map +1 -0
  99. package/dist/esm/src/wallet/Wallet.interfaces.js +2 -0
  100. package/dist/esm/src/wallet/Wallet.interfaces.js.map +1 -0
  101. package/dist/esm/src/wallet/WalletClient.js +177 -0
  102. package/dist/esm/src/wallet/WalletClient.js.map +1 -0
  103. package/dist/esm/src/wallet/WalletError.js +25 -0
  104. package/dist/esm/src/wallet/WalletError.js.map +1 -0
  105. package/dist/esm/src/wallet/index.js +9 -0
  106. package/dist/esm/src/wallet/index.js.map +1 -0
  107. package/dist/esm/src/wallet/substrates/HTTPWalletWire.js +42 -0
  108. package/dist/esm/src/wallet/substrates/HTTPWalletWire.js.map +1 -0
  109. package/dist/esm/src/wallet/substrates/WalletWire.js +2 -0
  110. package/dist/esm/src/wallet/substrates/WalletWire.js.map +1 -0
  111. package/dist/esm/src/wallet/substrates/WalletWireCalls.js +34 -0
  112. package/dist/esm/src/wallet/substrates/WalletWireCalls.js.map +1 -0
  113. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js +1816 -0
  114. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js.map +1 -0
  115. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js +1300 -0
  116. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js.map +1 -0
  117. package/dist/esm/src/wallet/substrates/XDM.js +128 -0
  118. package/dist/esm/src/wallet/substrates/XDM.js.map +1 -0
  119. package/dist/esm/src/wallet/substrates/index.js +8 -0
  120. package/dist/esm/src/wallet/substrates/index.js.map +1 -0
  121. package/dist/esm/src/wallet/substrates/window.CWI.js +100 -0
  122. package/dist/esm/src/wallet/substrates/window.CWI.js.map +1 -0
  123. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  124. package/dist/types/mod.d.ts +4 -0
  125. package/dist/types/mod.d.ts.map +1 -1
  126. package/dist/types/src/auth/Certificate.d.ts +76 -0
  127. package/dist/types/src/auth/Certificate.d.ts.map +1 -0
  128. package/dist/types/src/auth/index.d.ts +2 -0
  129. package/dist/types/src/auth/index.d.ts.map +1 -0
  130. package/dist/types/src/overlay-tools/LookupResolver.d.ts +71 -0
  131. package/dist/types/src/overlay-tools/LookupResolver.d.ts.map +1 -0
  132. package/dist/types/src/overlay-tools/OverlayAdminTokenTemplate.d.ts +44 -0
  133. package/dist/types/src/overlay-tools/OverlayAdminTokenTemplate.d.ts.map +1 -0
  134. package/dist/types/src/overlay-tools/SHIPBroadcaster.d.ts +90 -0
  135. package/dist/types/src/overlay-tools/SHIPBroadcaster.d.ts.map +1 -0
  136. package/dist/types/src/overlay-tools/index.d.ts +6 -0
  137. package/dist/types/src/overlay-tools/index.d.ts.map +1 -0
  138. package/dist/types/src/script/templates/PushDrop.d.ts +53 -0
  139. package/dist/types/src/script/templates/PushDrop.d.ts.map +1 -0
  140. package/dist/types/src/script/templates/index.d.ts +1 -0
  141. package/dist/types/src/script/templates/index.d.ts.map +1 -1
  142. package/dist/types/src/wallet/CachedKeyDeriver.d.ts +92 -0
  143. package/dist/types/src/wallet/CachedKeyDeriver.d.ts.map +1 -0
  144. package/dist/types/src/wallet/KeyDeriver.d.ts +72 -0
  145. package/dist/types/src/wallet/KeyDeriver.d.ts.map +1 -0
  146. package/dist/types/src/wallet/ProtoWallet.d.ts +415 -0
  147. package/dist/types/src/wallet/ProtoWallet.d.ts.map +1 -0
  148. package/dist/types/src/wallet/Wallet.interfaces.d.ts +996 -0
  149. package/dist/types/src/wallet/Wallet.interfaces.d.ts.map +1 -0
  150. package/dist/types/src/wallet/WalletClient.d.ts +182 -0
  151. package/dist/types/src/wallet/WalletClient.d.ts.map +1 -0
  152. package/dist/types/src/wallet/WalletError.d.ts +14 -0
  153. package/dist/types/src/wallet/WalletError.d.ts.map +1 -0
  154. package/dist/types/src/wallet/index.d.ts +9 -0
  155. package/dist/types/src/wallet/index.d.ts.map +1 -0
  156. package/dist/types/src/wallet/substrates/HTTPWalletWire.d.ts +9 -0
  157. package/dist/types/src/wallet/substrates/HTTPWalletWire.d.ts.map +1 -0
  158. package/dist/types/src/wallet/substrates/WalletWire.d.ts +7 -0
  159. package/dist/types/src/wallet/substrates/WalletWire.d.ts.map +1 -0
  160. package/dist/types/src/wallet/substrates/WalletWireCalls.d.ts +33 -0
  161. package/dist/types/src/wallet/substrates/WalletWireCalls.d.ts.map +1 -0
  162. package/dist/types/src/wallet/substrates/WalletWireProcessor.d.ts +18 -0
  163. package/dist/types/src/wallet/substrates/WalletWireProcessor.d.ts.map +1 -0
  164. package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts +196 -0
  165. package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts.map +1 -0
  166. package/dist/types/src/wallet/substrates/XDM.d.ts +412 -0
  167. package/dist/types/src/wallet/substrates/XDM.d.ts.map +1 -0
  168. package/dist/types/src/wallet/substrates/index.d.ts +8 -0
  169. package/dist/types/src/wallet/substrates/index.d.ts.map +1 -0
  170. package/dist/types/src/wallet/substrates/window.CWI.d.ts +410 -0
  171. package/dist/types/src/wallet/substrates/window.CWI.d.ts.map +1 -0
  172. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  173. package/dist/umd/bundle.js +1 -1
  174. package/docs/overlay-tools.md +551 -0
  175. package/docs/script.md +135 -0
  176. package/docs/totp.md +119 -0
  177. package/docs/wallet-substrates.md +10 -0
  178. package/docs/wallet.md +4182 -0
  179. package/mod.ts +5 -1
  180. package/package.json +44 -3
  181. package/src/auth/Certificate.ts +233 -0
  182. package/src/auth/__tests/Certificate.test.ts +282 -0
  183. package/src/auth/index.ts +1 -0
  184. package/src/overlay-tools/LookupResolver.ts +228 -0
  185. package/src/overlay-tools/OverlayAdminTokenTemplate.ts +79 -0
  186. package/src/overlay-tools/SHIPBroadcaster.ts +405 -0
  187. package/src/overlay-tools/__tests/LookupResolver.test.ts +1403 -0
  188. package/src/overlay-tools/__tests/OverlayAdminTokenTemplate.test.ts +69 -0
  189. package/src/overlay-tools/__tests/SHIPBroadcaster.test.ts +904 -0
  190. package/src/overlay-tools/index.ts +5 -0
  191. package/src/script/templates/PushDrop.ts +246 -0
  192. package/src/script/templates/__tests/PushDrop.test.ts +158 -0
  193. package/src/script/templates/index.ts +1 -0
  194. package/src/transaction/http/DefaultHttpClient.ts +1 -1
  195. package/src/wallet/CachedKeyDeriver.ts +193 -0
  196. package/src/wallet/KeyDeriver.ts +178 -0
  197. package/src/wallet/ProtoWallet.ts +732 -0
  198. package/src/wallet/Wallet.interfaces.ts +1170 -0
  199. package/src/wallet/WalletClient.ts +201 -0
  200. package/src/wallet/WalletError.ts +27 -0
  201. package/src/wallet/__tests/CachedKeyDeriver.test.ts +322 -0
  202. package/src/wallet/__tests/KeyDeriver.test.ts +118 -0
  203. package/src/wallet/__tests/ProtoWallet.test.ts +543 -0
  204. package/src/wallet/index.ts +8 -0
  205. package/src/wallet/substrates/HTTPWalletWire.ts +47 -0
  206. package/src/wallet/substrates/WalletWire.ts +6 -0
  207. package/src/wallet/substrates/WalletWireCalls.ts +34 -0
  208. package/src/wallet/substrates/WalletWireProcessor.ts +2046 -0
  209. package/src/wallet/substrates/WalletWireTransceiver.ts +1454 -0
  210. package/src/wallet/substrates/XDM.ts +157 -0
  211. package/src/wallet/substrates/__tests/WalletWire.integration.test.ts +2194 -0
  212. package/src/wallet/substrates/__tests/XDM.test.ts +659 -0
  213. package/src/wallet/substrates/index.ts +7 -0
  214. package/src/wallet/substrates/window.CWI.ts +133 -0
@@ -0,0 +1,1816 @@
1
+ import { Utils } from '../../primitives/index.js';
2
+ import calls from './WalletWireCalls.js';
3
+ import Certificate from '../../auth/Certificate.js';
4
+ /**
5
+ * Processes incoming wallet calls received over a wallet wire, with a given wallet.
6
+ */
7
+ export default class WalletWireProcessor {
8
+ wallet;
9
+ constructor(wallet) {
10
+ this.wallet = wallet;
11
+ }
12
+ decodeOutpoint(reader) {
13
+ const txidBytes = reader.read(32);
14
+ const txid = Utils.toHex(txidBytes);
15
+ const index = reader.readVarIntNum();
16
+ return `${txid}.${index}`;
17
+ }
18
+ encodeOutpoint(outpoint) {
19
+ const writer = new Utils.Writer();
20
+ const [txid, index] = outpoint.split('.');
21
+ writer.write(Utils.toArray(txid, 'hex'));
22
+ writer.writeVarIntNum(Number(index));
23
+ return writer.toArray();
24
+ }
25
+ async transmitToWallet(message) {
26
+ const messageReader = new Utils.Reader(message);
27
+ try {
28
+ // Read call code
29
+ const callCode = messageReader.readUInt8();
30
+ // Map call code to call name
31
+ const callName = calls[callCode]; // calls is enum
32
+ if (!callName) {
33
+ // Invalid call code
34
+ throw new Error(`Invalid call code: ${callCode}`);
35
+ }
36
+ // Read originator length
37
+ const originatorLength = messageReader.readUInt8();
38
+ const originatorBytes = messageReader.read(originatorLength);
39
+ const originator = Utils.toUTF8(originatorBytes);
40
+ // Read parameters
41
+ const paramsReader = messageReader; // Remaining bytes
42
+ switch (callName) {
43
+ case 'createAction':
44
+ // Deserialize parameters from paramsReader
45
+ const args = {};
46
+ // Read description
47
+ const descriptionLength = paramsReader.readVarIntNum();
48
+ const descriptionBytes = paramsReader.read(descriptionLength);
49
+ args.description = Utils.toUTF8(descriptionBytes);
50
+ // tx
51
+ const inputBeefLength = paramsReader.readVarIntNum();
52
+ if (inputBeefLength >= 0) {
53
+ args.inputBEEF = paramsReader.read(inputBeefLength); // BEEF (Byte[])
54
+ }
55
+ else {
56
+ args.inputBEEF = undefined;
57
+ }
58
+ // Read inputs
59
+ const inputsLength = paramsReader.readVarIntNum();
60
+ if (inputsLength >= 0) {
61
+ args.inputs = [];
62
+ for (let i = 0; i < inputsLength; i++) {
63
+ const input = {};
64
+ // outpoint
65
+ input.outpoint = this.decodeOutpoint(paramsReader);
66
+ // unlockingScript / unlockingScriptLength
67
+ const unlockingScriptLength = paramsReader.readVarIntNum();
68
+ if (unlockingScriptLength >= 0) {
69
+ const unlockingScriptBytes = paramsReader.read(unlockingScriptLength);
70
+ input.unlockingScript = Utils.toHex(unlockingScriptBytes);
71
+ }
72
+ else {
73
+ input.unlockingScript = undefined;
74
+ const unlockingScriptLengthValue = paramsReader.readVarIntNum();
75
+ input.unlockingScriptLength = unlockingScriptLengthValue;
76
+ }
77
+ // inputDescription
78
+ const inputDescriptionLength = paramsReader.readVarIntNum();
79
+ const inputDescriptionBytes = paramsReader.read(inputDescriptionLength);
80
+ input.inputDescription = Utils.toUTF8(inputDescriptionBytes);
81
+ // sequenceNumber
82
+ const sequenceNumber = paramsReader.readVarIntNum();
83
+ if (sequenceNumber >= 0) {
84
+ input.sequenceNumber = sequenceNumber;
85
+ }
86
+ else {
87
+ input.sequenceNumber = undefined;
88
+ }
89
+ args.inputs.push(input);
90
+ }
91
+ }
92
+ else {
93
+ args.inputs = undefined;
94
+ }
95
+ // Read outputs
96
+ const outputsLength = paramsReader.readVarIntNum();
97
+ if (outputsLength >= 0) {
98
+ args.outputs = [];
99
+ for (let i = 0; i < outputsLength; i++) {
100
+ const output = {};
101
+ // lockingScript
102
+ const lockingScriptLength = paramsReader.readVarIntNum();
103
+ const lockingScriptBytes = paramsReader.read(lockingScriptLength);
104
+ output.lockingScript = Utils.toHex(lockingScriptBytes);
105
+ // satoshis
106
+ output.satoshis = paramsReader.readVarIntNum();
107
+ // outputDescription
108
+ const outputDescriptionLength = paramsReader.readVarIntNum();
109
+ const outputDescriptionBytes = paramsReader.read(outputDescriptionLength);
110
+ output.outputDescription = Utils.toUTF8(outputDescriptionBytes);
111
+ // basket
112
+ const basketLength = paramsReader.readVarIntNum();
113
+ if (basketLength >= 0) {
114
+ const basketBytes = paramsReader.read(basketLength);
115
+ output.basket = Utils.toUTF8(basketBytes);
116
+ }
117
+ else {
118
+ output.basket = undefined;
119
+ }
120
+ // customInstructions
121
+ const customInstructionsLength = paramsReader.readVarIntNum();
122
+ if (customInstructionsLength >= 0) {
123
+ const customInstructionsBytes = paramsReader.read(customInstructionsLength);
124
+ output.customInstructions = Utils.toUTF8(customInstructionsBytes);
125
+ }
126
+ else {
127
+ output.customInstructions = undefined;
128
+ }
129
+ // tags
130
+ const tagsLength = paramsReader.readVarIntNum();
131
+ if (tagsLength >= 0) {
132
+ output.tags = [];
133
+ for (let j = 0; j < tagsLength; j++) {
134
+ const tagLength = paramsReader.readVarIntNum();
135
+ const tagBytes = paramsReader.read(tagLength);
136
+ const tag = Utils.toUTF8(tagBytes);
137
+ output.tags.push(tag);
138
+ }
139
+ }
140
+ else {
141
+ output.tags = undefined;
142
+ }
143
+ args.outputs.push(output);
144
+ }
145
+ }
146
+ else {
147
+ args.outputs = undefined;
148
+ }
149
+ // lockTime
150
+ const lockTime = paramsReader.readVarIntNum();
151
+ if (lockTime >= 0) {
152
+ args.lockTime = lockTime;
153
+ }
154
+ else {
155
+ args.lockTime = undefined;
156
+ }
157
+ // version
158
+ const version = paramsReader.readVarIntNum();
159
+ if (version >= 0) {
160
+ args.version = version;
161
+ }
162
+ else {
163
+ args.version = undefined;
164
+ }
165
+ // labels
166
+ const labelsLength = paramsReader.readVarIntNum();
167
+ if (labelsLength >= 0) {
168
+ args.labels = [];
169
+ for (let i = 0; i < labelsLength; i++) {
170
+ const labelLength = paramsReader.readVarIntNum();
171
+ const labelBytes = paramsReader.read(labelLength);
172
+ const label = Utils.toUTF8(labelBytes);
173
+ args.labels.push(label);
174
+ }
175
+ }
176
+ else {
177
+ args.labels = undefined;
178
+ }
179
+ // options
180
+ const optionsPresent = paramsReader.readInt8();
181
+ if (optionsPresent === 1) {
182
+ args.options = {};
183
+ // signAndProcess
184
+ const signAndProcessFlag = paramsReader.readInt8();
185
+ if (signAndProcessFlag === -1) {
186
+ args.options.signAndProcess = undefined;
187
+ }
188
+ else {
189
+ args.options.signAndProcess = signAndProcessFlag === 1;
190
+ }
191
+ // acceptDelayedBroadcast
192
+ const acceptDelayedBroadcastFlag = paramsReader.readInt8();
193
+ if (acceptDelayedBroadcastFlag === -1) {
194
+ args.options.acceptDelayedBroadcast = undefined;
195
+ }
196
+ else {
197
+ args.options.acceptDelayedBroadcast = acceptDelayedBroadcastFlag === 1;
198
+ }
199
+ // trustSelf
200
+ const trustSelfFlag = paramsReader.readInt8();
201
+ if (trustSelfFlag === -1) {
202
+ args.options.trustSelf = undefined;
203
+ }
204
+ else if (trustSelfFlag === 1) {
205
+ args.options.trustSelf = 'known';
206
+ }
207
+ // knownTxids
208
+ const knownTxidsLength = paramsReader.readVarIntNum();
209
+ if (knownTxidsLength >= 0) {
210
+ args.options.knownTxids = [];
211
+ for (let i = 0; i < knownTxidsLength; i++) {
212
+ const txidBytes = paramsReader.read(32);
213
+ const txid = Utils.toHex(txidBytes);
214
+ args.options.knownTxids.push(txid);
215
+ }
216
+ }
217
+ else {
218
+ args.options.knownTxids = undefined;
219
+ }
220
+ // returnTXIDOnly
221
+ const returnTXIDOnlyFlag = paramsReader.readInt8();
222
+ if (returnTXIDOnlyFlag === -1) {
223
+ args.options.returnTXIDOnly = undefined;
224
+ }
225
+ else {
226
+ args.options.returnTXIDOnly = returnTXIDOnlyFlag === 1;
227
+ }
228
+ // noSend
229
+ const noSendFlag = paramsReader.readInt8();
230
+ if (noSendFlag === -1) {
231
+ args.options.noSend = undefined;
232
+ }
233
+ else {
234
+ args.options.noSend = noSendFlag === 1;
235
+ }
236
+ // noSendChange
237
+ const noSendChangeLength = paramsReader.readVarIntNum();
238
+ if (noSendChangeLength >= 0) {
239
+ args.options.noSendChange = [];
240
+ for (let i = 0; i < noSendChangeLength; i++) {
241
+ const outpoint = this.decodeOutpoint(paramsReader);
242
+ args.options.noSendChange.push(outpoint);
243
+ }
244
+ }
245
+ else {
246
+ args.options.noSendChange = undefined;
247
+ }
248
+ // sendWith
249
+ const sendWithLength = paramsReader.readVarIntNum();
250
+ if (sendWithLength >= 0) {
251
+ args.options.sendWith = [];
252
+ for (let i = 0; i < sendWithLength; i++) {
253
+ const txidBytes = paramsReader.read(32);
254
+ const txid = Utils.toHex(txidBytes);
255
+ args.options.sendWith.push(txid);
256
+ }
257
+ }
258
+ else {
259
+ args.options.sendWith = undefined;
260
+ }
261
+ // randomizeOutputs
262
+ const randomizeOutputsFlag = paramsReader.readInt8();
263
+ if (randomizeOutputsFlag === -1) {
264
+ args.options.randomizeOutputs = undefined;
265
+ }
266
+ else {
267
+ args.options.randomizeOutputs = randomizeOutputsFlag === 1;
268
+ }
269
+ }
270
+ else {
271
+ args.options = undefined;
272
+ }
273
+ // Call the method
274
+ const createActionResult = await this.wallet.createAction(args, originator);
275
+ // Serialize the result
276
+ const resultWriter = new Utils.Writer();
277
+ // txid
278
+ if (createActionResult.txid) {
279
+ resultWriter.writeInt8(1);
280
+ resultWriter.write(Utils.toArray(createActionResult.txid, 'hex'));
281
+ }
282
+ else {
283
+ resultWriter.writeInt8(0);
284
+ }
285
+ // tx
286
+ if (createActionResult.tx) {
287
+ resultWriter.writeInt8(1);
288
+ resultWriter.writeVarIntNum(createActionResult.tx.length);
289
+ resultWriter.write(createActionResult.tx);
290
+ }
291
+ else {
292
+ resultWriter.writeInt8(0);
293
+ }
294
+ // noSendChange
295
+ if (createActionResult.noSendChange) {
296
+ resultWriter.writeVarIntNum(createActionResult.noSendChange.length);
297
+ for (const outpoint of createActionResult.noSendChange) {
298
+ resultWriter.write(this.encodeOutpoint(outpoint));
299
+ }
300
+ }
301
+ else {
302
+ resultWriter.writeVarIntNum(-1);
303
+ }
304
+ // sendWithResults
305
+ if (createActionResult.sendWithResults) {
306
+ resultWriter.writeVarIntNum(createActionResult.sendWithResults.length);
307
+ for (const result of createActionResult.sendWithResults) {
308
+ resultWriter.write(Utils.toArray(result.txid, 'hex'));
309
+ let statusCode;
310
+ if (result.status === 'unproven')
311
+ statusCode = 1;
312
+ else if (result.status === 'sending')
313
+ statusCode = 2;
314
+ else if (result.status === 'failed')
315
+ statusCode = 3;
316
+ resultWriter.writeInt8(statusCode);
317
+ }
318
+ }
319
+ else {
320
+ resultWriter.writeVarIntNum(-1);
321
+ }
322
+ // signableTransaction
323
+ if (createActionResult.signableTransaction) {
324
+ resultWriter.writeInt8(1);
325
+ resultWriter.writeVarIntNum(createActionResult.signableTransaction.tx.length);
326
+ resultWriter.write(createActionResult.signableTransaction.tx);
327
+ const referenceBytes = Utils.toArray(createActionResult.signableTransaction.reference, 'base64');
328
+ resultWriter.writeVarIntNum(referenceBytes.length);
329
+ resultWriter.write(referenceBytes);
330
+ }
331
+ else {
332
+ resultWriter.writeInt8(0);
333
+ }
334
+ // Return success code and result
335
+ const responseWriter = new Utils.Writer();
336
+ responseWriter.writeUInt8(0); // errorByte = 0
337
+ responseWriter.write(resultWriter.toArray());
338
+ return responseWriter.toArray();
339
+ case 'signAction':
340
+ {
341
+ const args = {};
342
+ // Deserialize spends
343
+ const spendCount = paramsReader.readVarIntNum();
344
+ args.spends = {};
345
+ for (let i = 0; i < spendCount; i++) {
346
+ const inputIndex = paramsReader.readVarIntNum();
347
+ const spend = {};
348
+ // unlockingScript
349
+ const unlockingScriptLength = paramsReader.readVarIntNum();
350
+ const unlockingScriptBytes = paramsReader.read(unlockingScriptLength);
351
+ spend.unlockingScript = Utils.toHex(unlockingScriptBytes);
352
+ // sequenceNumber
353
+ const sequenceNumber = paramsReader.readVarIntNum();
354
+ if (sequenceNumber >= 0) {
355
+ spend.sequenceNumber = sequenceNumber;
356
+ }
357
+ else {
358
+ spend.sequenceNumber = undefined;
359
+ }
360
+ args.spends[inputIndex] = spend;
361
+ }
362
+ // Deserialize reference
363
+ const referenceLength = paramsReader.readVarIntNum();
364
+ const referenceBytes = paramsReader.read(referenceLength);
365
+ args.reference = Utils.toBase64(referenceBytes);
366
+ // Deserialize options
367
+ const optionsPresent = paramsReader.readInt8();
368
+ if (optionsPresent === 1) {
369
+ args.options = {};
370
+ // acceptDelayedBroadcast
371
+ const acceptDelayedBroadcastFlag = paramsReader.readInt8();
372
+ if (acceptDelayedBroadcastFlag === -1) {
373
+ args.options.acceptDelayedBroadcast = undefined;
374
+ }
375
+ else {
376
+ args.options.acceptDelayedBroadcast = acceptDelayedBroadcastFlag === 1;
377
+ }
378
+ // returnTXIDOnly
379
+ const returnTXIDOnlyFlag = paramsReader.readInt8();
380
+ if (returnTXIDOnlyFlag === -1) {
381
+ args.options.returnTXIDOnly = undefined;
382
+ }
383
+ else {
384
+ args.options.returnTXIDOnly = returnTXIDOnlyFlag === 1;
385
+ }
386
+ // noSend
387
+ const noSendFlag = paramsReader.readInt8();
388
+ if (noSendFlag === -1) {
389
+ args.options.noSend = undefined;
390
+ }
391
+ else {
392
+ args.options.noSend = noSendFlag === 1;
393
+ }
394
+ // sendWith
395
+ const sendWithLength = paramsReader.readVarIntNum();
396
+ if (sendWithLength >= 0) {
397
+ args.options.sendWith = [];
398
+ for (let i = 0; i < sendWithLength; i++) {
399
+ const txidBytes = paramsReader.read(32);
400
+ const txid = Utils.toHex(txidBytes);
401
+ args.options.sendWith.push(txid);
402
+ }
403
+ }
404
+ else {
405
+ args.options.sendWith = undefined;
406
+ }
407
+ }
408
+ else {
409
+ args.options = undefined;
410
+ }
411
+ // Call the method
412
+ const signActionResult = await this.wallet.signAction(args, originator);
413
+ // Serialize the result
414
+ const resultWriter = new Utils.Writer();
415
+ // txid
416
+ if (signActionResult.txid) {
417
+ resultWriter.writeInt8(1);
418
+ resultWriter.write(Utils.toArray(signActionResult.txid, 'hex'));
419
+ }
420
+ else {
421
+ resultWriter.writeInt8(0);
422
+ }
423
+ // tx
424
+ if (signActionResult.tx) {
425
+ resultWriter.writeInt8(1);
426
+ resultWriter.writeVarIntNum(signActionResult.tx.length);
427
+ resultWriter.write(signActionResult.tx);
428
+ }
429
+ else {
430
+ resultWriter.writeInt8(0);
431
+ }
432
+ // sendWithResults
433
+ if (signActionResult.sendWithResults) {
434
+ resultWriter.writeVarIntNum(signActionResult.sendWithResults.length);
435
+ for (const result of signActionResult.sendWithResults) {
436
+ resultWriter.write(Utils.toArray(result.txid, 'hex'));
437
+ let statusCode;
438
+ if (result.status === 'unproven')
439
+ statusCode = 1;
440
+ else if (result.status === 'sending')
441
+ statusCode = 2;
442
+ else if (result.status === 'failed')
443
+ statusCode = 3;
444
+ resultWriter.writeInt8(statusCode);
445
+ }
446
+ }
447
+ else {
448
+ resultWriter.writeVarIntNum(-1);
449
+ }
450
+ // Return success code and result
451
+ const responseWriter = new Utils.Writer();
452
+ responseWriter.writeUInt8(0); // errorByte = 0
453
+ responseWriter.write(resultWriter.toArray());
454
+ return responseWriter.toArray();
455
+ }
456
+ case 'abortAction':
457
+ {
458
+ // Deserialize reference
459
+ const referenceBytes = paramsReader.read();
460
+ const reference = Utils.toBase64(referenceBytes);
461
+ // Call the method
462
+ await this.wallet.abortAction({ reference }, originator);
463
+ // Return success code and result
464
+ const responseWriter = new Utils.Writer();
465
+ responseWriter.writeUInt8(0); // errorByte = 0
466
+ return responseWriter.toArray();
467
+ }
468
+ case 'listActions':
469
+ {
470
+ const args = {};
471
+ // Deserialize labels
472
+ const labelsLength = paramsReader.readVarIntNum();
473
+ args.labels = [];
474
+ for (let i = 0; i < labelsLength; i++) {
475
+ const labelLength = paramsReader.readVarIntNum();
476
+ const labelBytes = paramsReader.read(labelLength);
477
+ args.labels.push(Utils.toUTF8(labelBytes));
478
+ }
479
+ // Deserialize labelQueryMode
480
+ const labelQueryModeFlag = paramsReader.readInt8();
481
+ if (labelQueryModeFlag === -1) {
482
+ args.labelQueryMode = undefined;
483
+ }
484
+ else if (labelQueryModeFlag === 1) {
485
+ args.labelQueryMode = 'any';
486
+ }
487
+ else if (labelQueryModeFlag === 2) {
488
+ args.labelQueryMode = 'all';
489
+ }
490
+ // Deserialize include options
491
+ const includeOptionsNames = [
492
+ 'includeLabels',
493
+ 'includeInputs',
494
+ 'includeInputSourceLockingScripts',
495
+ 'includeInputUnlockingScripts',
496
+ 'includeOutputs',
497
+ 'includeOutputLockingScripts'
498
+ ];
499
+ for (const optionName of includeOptionsNames) {
500
+ const optionFlag = paramsReader.readInt8();
501
+ if (optionFlag === -1) {
502
+ args[optionName] = undefined;
503
+ }
504
+ else {
505
+ args[optionName] = optionFlag === 1;
506
+ }
507
+ }
508
+ // Deserialize limit
509
+ const limit = paramsReader.readVarIntNum();
510
+ if (limit >= 0) {
511
+ args.limit = limit;
512
+ }
513
+ else {
514
+ args.limit = undefined;
515
+ }
516
+ // Deserialize offset
517
+ const offset = paramsReader.readVarIntNum();
518
+ if (offset >= 0) {
519
+ args.offset = offset;
520
+ }
521
+ else {
522
+ args.offset = undefined;
523
+ }
524
+ // Deserialize seekPermission
525
+ const seekPermission = paramsReader.readInt8();
526
+ if (seekPermission >= 0) {
527
+ args.seekPermission = seekPermission === 1;
528
+ }
529
+ else {
530
+ args.seekPermission = undefined;
531
+ }
532
+ // Call the method
533
+ const listActionsResult = await this.wallet.listActions(args, originator);
534
+ // Serialize the result
535
+ const resultWriter = new Utils.Writer();
536
+ // totalActions
537
+ resultWriter.writeVarIntNum(listActionsResult.totalActions);
538
+ // actions
539
+ for (const action of listActionsResult.actions) {
540
+ // txid
541
+ resultWriter.write(Utils.toArray(action.txid, 'hex'));
542
+ // satoshis
543
+ resultWriter.writeVarIntNum(action.satoshis);
544
+ // status
545
+ let statusCode;
546
+ switch (action.status) {
547
+ case 'completed':
548
+ statusCode = 1;
549
+ break;
550
+ case 'unprocessed':
551
+ statusCode = 2;
552
+ break;
553
+ case 'sending':
554
+ statusCode = 3;
555
+ break;
556
+ case 'unproven':
557
+ statusCode = 4;
558
+ break;
559
+ case 'unsigned':
560
+ statusCode = 5;
561
+ break;
562
+ case 'nosend':
563
+ statusCode = 6;
564
+ break;
565
+ case 'nonfinal':
566
+ statusCode = 7;
567
+ break;
568
+ default:
569
+ statusCode = -1;
570
+ break;
571
+ }
572
+ resultWriter.writeInt8(statusCode);
573
+ // isOutgoing
574
+ resultWriter.writeInt8(action.isOutgoing ? 1 : 0);
575
+ // description
576
+ const descriptionBytes = Utils.toArray(action.description, 'utf8');
577
+ resultWriter.writeVarIntNum(descriptionBytes.length);
578
+ resultWriter.write(descriptionBytes);
579
+ // labels
580
+ if (action.labels !== undefined) {
581
+ resultWriter.writeVarIntNum(action.labels.length);
582
+ for (const label of action.labels) {
583
+ const labelBytes = Utils.toArray(label, 'utf8');
584
+ resultWriter.writeVarIntNum(labelBytes.length);
585
+ resultWriter.write(labelBytes);
586
+ }
587
+ }
588
+ else {
589
+ resultWriter.writeVarIntNum(-1);
590
+ }
591
+ // version
592
+ resultWriter.writeVarIntNum(action.version);
593
+ // lockTime
594
+ resultWriter.writeVarIntNum(action.lockTime);
595
+ // inputs
596
+ if (action.inputs !== undefined) {
597
+ resultWriter.writeVarIntNum(action.inputs.length);
598
+ for (const input of action.inputs) {
599
+ // sourceOutpoint
600
+ resultWriter.write(this.encodeOutpoint(input.sourceOutpoint));
601
+ // sourceSatoshis
602
+ resultWriter.writeVarIntNum(input.sourceSatoshis);
603
+ // sourceLockingScript
604
+ if (input.sourceLockingScript !== undefined) {
605
+ const sourceLockingScriptBytes = Utils.toArray(input.sourceLockingScript, 'hex');
606
+ resultWriter.writeVarIntNum(sourceLockingScriptBytes.length);
607
+ resultWriter.write(sourceLockingScriptBytes);
608
+ }
609
+ else {
610
+ resultWriter.writeVarIntNum(-1);
611
+ }
612
+ // unlockingScript
613
+ if (input.unlockingScript !== undefined) {
614
+ const unlockingScriptBytes = Utils.toArray(input.unlockingScript, 'hex');
615
+ resultWriter.writeVarIntNum(unlockingScriptBytes.length);
616
+ resultWriter.write(unlockingScriptBytes);
617
+ }
618
+ else {
619
+ resultWriter.writeVarIntNum(-1);
620
+ }
621
+ // inputDescription
622
+ const inputDescriptionBytes = Utils.toArray(input.inputDescription, 'utf8');
623
+ resultWriter.writeVarIntNum(inputDescriptionBytes.length);
624
+ resultWriter.write(inputDescriptionBytes);
625
+ // sequenceNumber
626
+ resultWriter.writeVarIntNum(input.sequenceNumber);
627
+ }
628
+ }
629
+ else {
630
+ resultWriter.writeVarIntNum(-1);
631
+ }
632
+ // outputs
633
+ if (action.outputs !== undefined) {
634
+ resultWriter.writeVarIntNum(action.outputs.length);
635
+ for (const output of action.outputs) {
636
+ // outputIndex
637
+ resultWriter.writeVarIntNum(output.outputIndex);
638
+ // satoshis
639
+ resultWriter.writeVarIntNum(output.satoshis);
640
+ // lockingScript
641
+ if (output.lockingScript !== undefined) {
642
+ const lockingScriptBytes = Utils.toArray(output.lockingScript, 'hex');
643
+ resultWriter.writeVarIntNum(lockingScriptBytes.length);
644
+ resultWriter.write(lockingScriptBytes);
645
+ }
646
+ else {
647
+ resultWriter.writeVarIntNum(-1);
648
+ }
649
+ // spendable
650
+ resultWriter.writeInt8(output.spendable ? 1 : 0);
651
+ // outputDescription
652
+ const outputDescriptionBytes = Utils.toArray(output.outputDescription, 'utf8');
653
+ resultWriter.writeVarIntNum(outputDescriptionBytes.length);
654
+ resultWriter.write(outputDescriptionBytes);
655
+ // basket
656
+ if (output.basket !== undefined) {
657
+ const basketBytes = Utils.toArray(output.basket, 'utf8');
658
+ resultWriter.writeVarIntNum(basketBytes.length);
659
+ resultWriter.write(basketBytes);
660
+ }
661
+ else {
662
+ resultWriter.writeVarIntNum(-1);
663
+ }
664
+ // tags
665
+ if (output.tags !== undefined) {
666
+ resultWriter.writeVarIntNum(output.tags.length);
667
+ for (const tag of output.tags) {
668
+ const tagBytes = Utils.toArray(tag, 'utf8');
669
+ resultWriter.writeVarIntNum(tagBytes.length);
670
+ resultWriter.write(tagBytes);
671
+ }
672
+ }
673
+ else {
674
+ resultWriter.writeVarIntNum(-1);
675
+ }
676
+ // customInstructions
677
+ if (output.customInstructions !== undefined) {
678
+ const customInstructionsBytes = Utils.toArray(output.customInstructions, 'utf8');
679
+ resultWriter.writeVarIntNum(customInstructionsBytes.length);
680
+ resultWriter.write(customInstructionsBytes);
681
+ }
682
+ else {
683
+ resultWriter.writeVarIntNum(-1);
684
+ }
685
+ }
686
+ }
687
+ else {
688
+ resultWriter.writeVarIntNum(-1);
689
+ }
690
+ }
691
+ const responseWriter = new Utils.Writer();
692
+ responseWriter.writeUInt8(0); // errorByte = 0
693
+ responseWriter.write(resultWriter.toArray());
694
+ return responseWriter.toArray();
695
+ }
696
+ case 'internalizeAction':
697
+ {
698
+ const args = {};
699
+ // Read tx
700
+ const txLength = paramsReader.readVarIntNum();
701
+ args.tx = paramsReader.read(txLength);
702
+ // Read outputs
703
+ const outputsLength = paramsReader.readVarIntNum();
704
+ args.outputs = [];
705
+ for (let i = 0; i < outputsLength; i++) {
706
+ const output = {};
707
+ // outputIndex
708
+ output.outputIndex = paramsReader.readVarIntNum();
709
+ // protocol
710
+ const protocolFlag = paramsReader.readUInt8();
711
+ if (protocolFlag === 1) {
712
+ output.protocol = 'wallet payment';
713
+ output.paymentRemittance = {};
714
+ // senderIdentityKey
715
+ const senderIdentityKeyBytes = paramsReader.read(33);
716
+ output.paymentRemittance.senderIdentityKey = Utils.toHex(senderIdentityKeyBytes);
717
+ // derivationPrefix
718
+ const derivationPrefixLength = paramsReader.readVarIntNum();
719
+ const derivationPrefixBytes = paramsReader.read(derivationPrefixLength);
720
+ output.paymentRemittance.derivationPrefix = Utils.toBase64(derivationPrefixBytes);
721
+ // derivationSuffix
722
+ const derivationSuffixLength = paramsReader.readVarIntNum();
723
+ const derivationSuffixBytes = paramsReader.read(derivationSuffixLength);
724
+ output.paymentRemittance.derivationSuffix = Utils.toBase64(derivationSuffixBytes);
725
+ }
726
+ else if (protocolFlag === 2) {
727
+ output.protocol = 'basket insertion';
728
+ output.insertionRemittance = {};
729
+ // basket
730
+ const basketLength = paramsReader.readVarIntNum();
731
+ const basketBytes = paramsReader.read(basketLength);
732
+ output.insertionRemittance.basket = Utils.toUTF8(basketBytes);
733
+ // customInstructions
734
+ const customInstructionsLength = paramsReader.readVarIntNum();
735
+ if (customInstructionsLength >= 0) {
736
+ const customInstructionsBytes = paramsReader.read(customInstructionsLength);
737
+ output.insertionRemittance.customInstructions = Utils.toUTF8(customInstructionsBytes);
738
+ }
739
+ // tags
740
+ const tagsLength = paramsReader.readVarIntNum();
741
+ if (tagsLength > 0) {
742
+ output.insertionRemittance.tags = [];
743
+ for (let j = 0; j < tagsLength; j++) {
744
+ const tagLength = paramsReader.readVarIntNum();
745
+ const tagBytes = paramsReader.read(tagLength);
746
+ output.insertionRemittance.tags.push(Utils.toUTF8(tagBytes));
747
+ }
748
+ }
749
+ else {
750
+ output.insertionRemittance.tags = [];
751
+ }
752
+ }
753
+ args.outputs.push(output);
754
+ }
755
+ const numberOfLabels = paramsReader.readVarIntNum();
756
+ if (numberOfLabels >= 0) {
757
+ args.labels = [];
758
+ for (let i = 0; i < numberOfLabels; i++) {
759
+ const labelLength = paramsReader.readVarIntNum();
760
+ args.labels.push(Utils.toUTF8(paramsReader.read(labelLength)));
761
+ }
762
+ }
763
+ const descriptionLength = paramsReader.readVarIntNum();
764
+ args.description = Utils.toUTF8(paramsReader.read(descriptionLength));
765
+ // Deserialize seekPermission
766
+ const seekPermission = paramsReader.readInt8();
767
+ if (seekPermission >= 0) {
768
+ args.seekPermission = seekPermission === 1;
769
+ }
770
+ else {
771
+ args.seekPermission = undefined;
772
+ }
773
+ // Call the method
774
+ await this.wallet.internalizeAction(args, originator);
775
+ // Return success code and result
776
+ const responseWriter = new Utils.Writer();
777
+ responseWriter.writeUInt8(0); // errorByte = 0
778
+ return responseWriter.toArray();
779
+ }
780
+ case 'listOutputs':
781
+ {
782
+ const args = {};
783
+ // Deserialize basket
784
+ const basketLength = paramsReader.readVarIntNum();
785
+ const basketBytes = paramsReader.read(basketLength);
786
+ args.basket = Utils.toUTF8(basketBytes);
787
+ // Deserialize tags
788
+ const tagsLength = paramsReader.readVarIntNum();
789
+ if (tagsLength > 0) {
790
+ args.tags = [];
791
+ for (let i = 0; i < tagsLength; i++) {
792
+ const tagLength = paramsReader.readVarIntNum();
793
+ const tagBytes = paramsReader.read(tagLength);
794
+ args.tags.push(Utils.toUTF8(tagBytes));
795
+ }
796
+ }
797
+ else {
798
+ args.tags = undefined;
799
+ }
800
+ // Deserialize tagQueryMode
801
+ const tagQueryModeFlag = paramsReader.readInt8();
802
+ if (tagQueryModeFlag === 1) {
803
+ args.tagQueryMode = 'all';
804
+ }
805
+ else if (tagQueryModeFlag === 2) {
806
+ args.tagQueryMode = 'any';
807
+ }
808
+ else {
809
+ args.tagQueryMode = undefined;
810
+ }
811
+ // Deserialize include
812
+ const includeFlag = paramsReader.readInt8();
813
+ if (includeFlag === 1) {
814
+ args.include = 'locking scripts';
815
+ }
816
+ else if (includeFlag === 2) {
817
+ args.include = 'entire transactions';
818
+ }
819
+ else {
820
+ args.include = undefined;
821
+ }
822
+ // Deserialize includeCustomInstructions
823
+ const includeCustomInstructionsFlag = paramsReader.readInt8();
824
+ if (includeCustomInstructionsFlag === -1) {
825
+ args.includeCustomInstructions = undefined;
826
+ }
827
+ else {
828
+ args.includeCustomInstructions = includeCustomInstructionsFlag === 1;
829
+ }
830
+ // Deserialize includeTags
831
+ const includeTagsFlag = paramsReader.readInt8();
832
+ if (includeTagsFlag === -1) {
833
+ args.includeTags = undefined;
834
+ }
835
+ else {
836
+ args.includeTags = includeTagsFlag === 1;
837
+ }
838
+ // Deserialize includeLabels
839
+ const includeLabelsFlag = paramsReader.readInt8();
840
+ if (includeLabelsFlag === -1) {
841
+ args.includeLabels = undefined;
842
+ }
843
+ else {
844
+ args.includeLabels = includeLabelsFlag === 1;
845
+ }
846
+ // Deserialize limit
847
+ const limit = paramsReader.readVarIntNum();
848
+ if (limit >= 0) {
849
+ args.limit = limit;
850
+ }
851
+ else {
852
+ args.limit = undefined;
853
+ }
854
+ // Deserialize offset
855
+ const offset = paramsReader.readVarIntNum();
856
+ if (offset >= 0) {
857
+ args.offset = offset;
858
+ }
859
+ else {
860
+ args.offset = undefined;
861
+ }
862
+ // Deserialize seekPermission
863
+ const seekPermission = paramsReader.readInt8();
864
+ if (seekPermission >= 0) {
865
+ args.seekPermission = seekPermission === 1;
866
+ }
867
+ else {
868
+ args.seekPermission = undefined;
869
+ }
870
+ // Call the method
871
+ const listOutputsResult = await this.wallet.listOutputs(args, originator);
872
+ // Serialize the result
873
+ const resultWriter = new Utils.Writer();
874
+ // totalOutputs
875
+ resultWriter.writeVarIntNum(listOutputsResult.totalOutputs);
876
+ // BEEF length and BEEF or -1
877
+ if (listOutputsResult.BEEF) {
878
+ resultWriter.writeVarIntNum(listOutputsResult.BEEF.length);
879
+ resultWriter.write(listOutputsResult.BEEF);
880
+ }
881
+ else {
882
+ resultWriter.writeVarIntNum(-1);
883
+ }
884
+ // outputs
885
+ for (const output of listOutputsResult.outputs) {
886
+ // outpoint
887
+ resultWriter.write(this.encodeOutpoint(output.outpoint));
888
+ // satoshis
889
+ resultWriter.writeVarIntNum(output.satoshis);
890
+ // lockingScript
891
+ if (output.lockingScript !== undefined) {
892
+ const lockingScriptBytes = Utils.toArray(output.lockingScript, 'hex');
893
+ resultWriter.writeVarIntNum(lockingScriptBytes.length);
894
+ resultWriter.write(lockingScriptBytes);
895
+ }
896
+ else {
897
+ resultWriter.writeVarIntNum(-1);
898
+ }
899
+ // customInstructions
900
+ if (output.customInstructions !== undefined) {
901
+ const customInstructionsBytes = Utils.toArray(output.customInstructions, 'utf8');
902
+ resultWriter.writeVarIntNum(customInstructionsBytes.length);
903
+ resultWriter.write(customInstructionsBytes);
904
+ }
905
+ else {
906
+ resultWriter.writeVarIntNum(-1);
907
+ }
908
+ // tags
909
+ if (output.tags !== undefined) {
910
+ resultWriter.writeVarIntNum(output.tags.length);
911
+ for (const tag of output.tags) {
912
+ const tagBytes = Utils.toArray(tag, 'utf8');
913
+ resultWriter.writeVarIntNum(tagBytes.length);
914
+ resultWriter.write(tagBytes);
915
+ }
916
+ }
917
+ else {
918
+ resultWriter.writeVarIntNum(-1);
919
+ }
920
+ // labels
921
+ if (output.labels !== undefined) {
922
+ resultWriter.writeVarIntNum(output.labels.length);
923
+ for (const label of output.labels) {
924
+ const labelBytes = Utils.toArray(label, 'utf8');
925
+ resultWriter.writeVarIntNum(labelBytes.length);
926
+ resultWriter.write(labelBytes);
927
+ }
928
+ }
929
+ else {
930
+ resultWriter.writeVarIntNum(-1);
931
+ }
932
+ }
933
+ // Return success code and result
934
+ const responseWriter = new Utils.Writer();
935
+ responseWriter.writeUInt8(0); // errorByte = 0
936
+ responseWriter.write(resultWriter.toArray());
937
+ return responseWriter.toArray();
938
+ }
939
+ case 'relinquishOutput':
940
+ {
941
+ const args = {};
942
+ // Deserialize basket
943
+ const basketLength = paramsReader.readVarIntNum();
944
+ const basketBytes = paramsReader.read(basketLength);
945
+ args.basket = Utils.toUTF8(basketBytes);
946
+ // Deserialize outpoint
947
+ args.output = this.decodeOutpoint(paramsReader);
948
+ // Call the method
949
+ await this.wallet.relinquishOutput(args, originator);
950
+ // Return success code and result
951
+ const responseWriter = new Utils.Writer();
952
+ responseWriter.writeUInt8(0); // errorByte = 0
953
+ return responseWriter.toArray();
954
+ }
955
+ case 'getPublicKey':
956
+ {
957
+ const args = {};
958
+ // Deserialize identityKey flag
959
+ const identityKeyFlag = paramsReader.readUInt8();
960
+ args.identityKey = identityKeyFlag === 1;
961
+ if (!args.identityKey) {
962
+ // Deserialize protocolID
963
+ args.protocolID = this.decodeProtocolID(paramsReader);
964
+ // Deserialize keyID
965
+ args.keyID = this.decodeString(paramsReader);
966
+ // Deserialize counterparty
967
+ args.counterparty = this.decodeCounterparty(paramsReader);
968
+ // Deserialize privilege parameters
969
+ const privilegedFlag = paramsReader.readInt8();
970
+ if (privilegedFlag === -1) {
971
+ args.privileged = undefined;
972
+ }
973
+ else {
974
+ args.privileged = privilegedFlag === 1;
975
+ }
976
+ const privilegedReasonLength = paramsReader.readInt8();
977
+ if (privilegedReasonLength !== -1) {
978
+ const privilegedReasonBytes = paramsReader.read(privilegedReasonLength);
979
+ args.privilegedReason = Utils.toUTF8(privilegedReasonBytes);
980
+ }
981
+ else {
982
+ args.privilegedReason = undefined;
983
+ }
984
+ // Deserialize forSelf
985
+ const forSelfFlag = paramsReader.readInt8();
986
+ if (forSelfFlag === -1) {
987
+ args.forSelf = undefined;
988
+ }
989
+ else {
990
+ args.forSelf = forSelfFlag === 1;
991
+ }
992
+ }
993
+ else {
994
+ // Deserialize privilege parameters
995
+ const privilegedFlag = paramsReader.readInt8();
996
+ if (privilegedFlag === -1) {
997
+ args.privileged = undefined;
998
+ }
999
+ else {
1000
+ args.privileged = privilegedFlag === 1;
1001
+ }
1002
+ const privilegedReasonLength = paramsReader.readInt8();
1003
+ if (privilegedReasonLength !== -1) {
1004
+ const privilegedReasonBytes = paramsReader.read(privilegedReasonLength);
1005
+ args.privilegedReason = Utils.toUTF8(privilegedReasonBytes);
1006
+ }
1007
+ else {
1008
+ args.privilegedReason = undefined;
1009
+ }
1010
+ }
1011
+ // Deserialize seekPermission
1012
+ const seekPermission = paramsReader.readInt8();
1013
+ if (seekPermission >= 0) {
1014
+ args.seekPermission = seekPermission === 1;
1015
+ }
1016
+ else {
1017
+ args.seekPermission = undefined;
1018
+ }
1019
+ // Call the method
1020
+ const getPublicKeyResult = await this.wallet.getPublicKey(args, originator);
1021
+ // Serialize the result
1022
+ const responseWriter = new Utils.Writer();
1023
+ responseWriter.writeUInt8(0); // errorByte = 0
1024
+ const publicKeyBytes = Utils.toArray(getPublicKeyResult.publicKey, 'hex');
1025
+ responseWriter.write(publicKeyBytes);
1026
+ return responseWriter.toArray();
1027
+ }
1028
+ case 'encrypt':
1029
+ {
1030
+ const args = this.decodeKeyRelatedParams(paramsReader);
1031
+ // Deserialize plaintext
1032
+ const plaintextLength = paramsReader.readVarIntNum();
1033
+ args.plaintext = paramsReader.read(plaintextLength);
1034
+ // Deserialize seekPermission
1035
+ const seekPermission = paramsReader.readInt8();
1036
+ if (seekPermission >= 0) {
1037
+ args.seekPermission = seekPermission === 1;
1038
+ }
1039
+ else {
1040
+ args.seekPermission = undefined;
1041
+ }
1042
+ // Call the method
1043
+ const encryptResult = await this.wallet.encrypt(args, originator);
1044
+ // Serialize the result
1045
+ const responseWriter = new Utils.Writer();
1046
+ responseWriter.writeUInt8(0); // errorByte = 0
1047
+ responseWriter.write(encryptResult.ciphertext);
1048
+ return responseWriter.toArray();
1049
+ }
1050
+ case 'decrypt':
1051
+ {
1052
+ const args = this.decodeKeyRelatedParams(paramsReader);
1053
+ // Deserialize ciphertext
1054
+ const ciphertextLength = paramsReader.readVarIntNum();
1055
+ args.ciphertext = paramsReader.read(ciphertextLength);
1056
+ // Deserialize seekPermission
1057
+ const seekPermission = paramsReader.readInt8();
1058
+ if (seekPermission >= 0) {
1059
+ args.seekPermission = seekPermission === 1;
1060
+ }
1061
+ else {
1062
+ args.seekPermission = undefined;
1063
+ }
1064
+ // Call the method
1065
+ const decryptResult = await this.wallet.decrypt(args, originator);
1066
+ // Serialize the result
1067
+ const responseWriter = new Utils.Writer();
1068
+ responseWriter.writeUInt8(0); // errorByte = 0
1069
+ responseWriter.write(decryptResult.plaintext);
1070
+ return responseWriter.toArray();
1071
+ }
1072
+ case 'createHmac':
1073
+ {
1074
+ const args = this.decodeKeyRelatedParams(paramsReader);
1075
+ // Deserialize data
1076
+ const dataLength = paramsReader.readVarIntNum();
1077
+ args.data = paramsReader.read(dataLength);
1078
+ // Deserialize seekPermission
1079
+ const seekPermission = paramsReader.readInt8();
1080
+ if (seekPermission >= 0) {
1081
+ args.seekPermission = seekPermission === 1;
1082
+ }
1083
+ else {
1084
+ args.seekPermission = undefined;
1085
+ }
1086
+ // Call the method
1087
+ const createHmacResult = await this.wallet.createHmac(args, originator);
1088
+ // Serialize the result
1089
+ const responseWriter = new Utils.Writer();
1090
+ responseWriter.writeUInt8(0); // errorByte = 0
1091
+ responseWriter.write(createHmacResult.hmac);
1092
+ return responseWriter.toArray();
1093
+ }
1094
+ case 'verifyHmac':
1095
+ {
1096
+ const args = this.decodeKeyRelatedParams(paramsReader);
1097
+ // Deserialize hmac
1098
+ args.hmac = paramsReader.read(32);
1099
+ // Deserialize data
1100
+ const dataLength = paramsReader.readVarIntNum();
1101
+ args.data = paramsReader.read(dataLength);
1102
+ // Deserialize seekPermission
1103
+ const seekPermission = paramsReader.readInt8();
1104
+ if (seekPermission >= 0) {
1105
+ args.seekPermission = seekPermission === 1;
1106
+ }
1107
+ else {
1108
+ args.seekPermission = undefined;
1109
+ }
1110
+ // Call the method
1111
+ await this.wallet.verifyHmac(args, originator);
1112
+ // Serialize the result (no data to return)
1113
+ const responseWriter = new Utils.Writer();
1114
+ responseWriter.writeUInt8(0); // errorByte = 0
1115
+ return responseWriter.toArray();
1116
+ }
1117
+ case 'createSignature':
1118
+ {
1119
+ const args = this.decodeKeyRelatedParams(paramsReader);
1120
+ // Deserialize data or hashToDirectlySign
1121
+ const dataTypeFlag = paramsReader.readUInt8();
1122
+ if (dataTypeFlag === 1) {
1123
+ const dataLength = paramsReader.readVarIntNum();
1124
+ args.data = paramsReader.read(dataLength);
1125
+ }
1126
+ else if (dataTypeFlag === 2) {
1127
+ args.hashToDirectlySign = paramsReader.read(32);
1128
+ }
1129
+ // Deserialize seekPermission
1130
+ const seekPermission = paramsReader.readInt8();
1131
+ if (seekPermission >= 0) {
1132
+ args.seekPermission = seekPermission === 1;
1133
+ }
1134
+ else {
1135
+ args.seekPermission = undefined;
1136
+ }
1137
+ // Call the method
1138
+ const createSignatureResult = await this.wallet.createSignature(args, originator);
1139
+ // Serialize the result
1140
+ const responseWriter = new Utils.Writer();
1141
+ responseWriter.writeUInt8(0); // errorByte = 0
1142
+ responseWriter.write(createSignatureResult.signature);
1143
+ return responseWriter.toArray();
1144
+ }
1145
+ case 'verifySignature':
1146
+ {
1147
+ const args = this.decodeKeyRelatedParams(paramsReader);
1148
+ // Deserialize forSelf
1149
+ const forSelfFlag = paramsReader.readInt8();
1150
+ if (forSelfFlag === -1) {
1151
+ args.forSelf = undefined;
1152
+ }
1153
+ else {
1154
+ args.forSelf = forSelfFlag === 1;
1155
+ }
1156
+ // Deserialize signature
1157
+ const signatureLength = paramsReader.readVarIntNum();
1158
+ args.signature = paramsReader.read(signatureLength);
1159
+ // Deserialize data or hashToDirectlyVerify
1160
+ const dataTypeFlag = paramsReader.readUInt8();
1161
+ if (dataTypeFlag === 1) {
1162
+ const dataLength = paramsReader.readVarIntNum();
1163
+ args.data = paramsReader.read(dataLength);
1164
+ }
1165
+ else if (dataTypeFlag === 2) {
1166
+ args.hashToDirectlyVerify = paramsReader.read(32);
1167
+ }
1168
+ // Deserialize seekPermission
1169
+ const seekPermission = paramsReader.readInt8();
1170
+ if (seekPermission >= 0) {
1171
+ args.seekPermission = seekPermission === 1;
1172
+ }
1173
+ else {
1174
+ args.seekPermission = undefined;
1175
+ }
1176
+ // Call the method
1177
+ await this.wallet.verifySignature(args, originator);
1178
+ // Serialize the result (no data to return)
1179
+ const responseWriter = new Utils.Writer();
1180
+ responseWriter.writeUInt8(0); // errorByte = 0
1181
+ return responseWriter.toArray();
1182
+ }
1183
+ case 'isAuthenticated':
1184
+ {
1185
+ // No parameters to deserialize
1186
+ // Call the method
1187
+ const isAuthenticatedResult = await this.wallet.isAuthenticated({}, originator);
1188
+ // Serialize the result
1189
+ const responseWriter = new Utils.Writer();
1190
+ responseWriter.writeUInt8(0); // errorByte = 0
1191
+ responseWriter.writeUInt8(isAuthenticatedResult.authenticated ? 1 : 0);
1192
+ return responseWriter.toArray();
1193
+ }
1194
+ case 'waitForAuthentication':
1195
+ {
1196
+ // No parameters to deserialize
1197
+ // Call the method
1198
+ await this.wallet.waitForAuthentication({}, originator);
1199
+ // Serialize the result (authenticated is always true)
1200
+ const responseWriter = new Utils.Writer();
1201
+ responseWriter.writeUInt8(0); // errorByte = 0
1202
+ return responseWriter.toArray();
1203
+ }
1204
+ case 'getHeight':
1205
+ {
1206
+ // No parameters to deserialize
1207
+ // Call the method
1208
+ const getHeightResult = await this.wallet.getHeight({}, originator);
1209
+ // Serialize the result
1210
+ const responseWriter = new Utils.Writer();
1211
+ responseWriter.writeUInt8(0); // errorByte = 0
1212
+ responseWriter.writeVarIntNum(getHeightResult.height);
1213
+ return responseWriter.toArray();
1214
+ }
1215
+ case 'getHeaderForHeight':
1216
+ {
1217
+ const args = {};
1218
+ // Deserialize height
1219
+ args.height = paramsReader.readVarIntNum();
1220
+ // Call the method
1221
+ const getHeaderResult = await this.wallet.getHeaderForHeight(args, originator);
1222
+ // Serialize the result
1223
+ const responseWriter = new Utils.Writer();
1224
+ responseWriter.writeUInt8(0); // errorByte = 0
1225
+ const headerBytes = Utils.toArray(getHeaderResult.header, 'hex');
1226
+ responseWriter.write(headerBytes);
1227
+ return responseWriter.toArray();
1228
+ }
1229
+ case 'getNetwork':
1230
+ {
1231
+ // No parameters to deserialize
1232
+ // Call the method
1233
+ const getNetworkResult = await this.wallet.getNetwork({}, originator);
1234
+ // Serialize the result
1235
+ const responseWriter = new Utils.Writer();
1236
+ responseWriter.writeUInt8(0); // errorByte = 0
1237
+ responseWriter.writeUInt8(getNetworkResult.network === 'mainnet' ? 0 : 1);
1238
+ return responseWriter.toArray();
1239
+ }
1240
+ case 'getVersion':
1241
+ {
1242
+ // No parameters to deserialize
1243
+ // Call the method
1244
+ const getVersionResult = await this.wallet.getVersion({}, originator);
1245
+ // Serialize the result
1246
+ const responseWriter = new Utils.Writer();
1247
+ responseWriter.writeUInt8(0); // errorByte = 0
1248
+ const versionBytes = Utils.toArray(getVersionResult.version, 'utf8');
1249
+ responseWriter.write(versionBytes);
1250
+ return responseWriter.toArray();
1251
+ }
1252
+ case 'revealCounterpartyKeyLinkage':
1253
+ {
1254
+ const args = {};
1255
+ // Read privileged parameters
1256
+ const privilegedFlag = paramsReader.readInt8();
1257
+ if (privilegedFlag === -1) {
1258
+ args.privileged = undefined;
1259
+ }
1260
+ else {
1261
+ args.privileged = privilegedFlag === 1;
1262
+ }
1263
+ const privilegedReasonLength = paramsReader.readInt8();
1264
+ if (privilegedReasonLength === -1) {
1265
+ args.privilegedReason = undefined;
1266
+ }
1267
+ else {
1268
+ const privilegedReasonBytes = paramsReader.read(privilegedReasonLength);
1269
+ args.privilegedReason = Utils.toUTF8(privilegedReasonBytes);
1270
+ }
1271
+ // Read counterparty public key
1272
+ const counterpartyBytes = paramsReader.read(33);
1273
+ args.counterparty = Utils.toHex(counterpartyBytes);
1274
+ // Read verifier public key
1275
+ const verifierBytes = paramsReader.read(33);
1276
+ args.verifier = Utils.toHex(verifierBytes);
1277
+ // Call the method
1278
+ const revealResult = await this.wallet.revealCounterpartyKeyLinkage(args, originator);
1279
+ // Serialize the result
1280
+ const resultWriter = new Utils.Writer();
1281
+ // Write prover
1282
+ resultWriter.write(Utils.toArray(revealResult.prover, 'hex'));
1283
+ // Write verifier
1284
+ resultWriter.write(Utils.toArray(revealResult.verifier, 'hex'));
1285
+ // Write counterparty
1286
+ resultWriter.write(Utils.toArray(revealResult.counterparty, 'hex'));
1287
+ // Write revelationTime
1288
+ const revelationTimeBytes = Utils.toArray(revealResult.revelationTime, 'utf8');
1289
+ resultWriter.writeVarIntNum(revelationTimeBytes.length);
1290
+ resultWriter.write(revelationTimeBytes);
1291
+ // Write encryptedLinkage
1292
+ resultWriter.writeVarIntNum(revealResult.encryptedLinkage.length);
1293
+ resultWriter.write(revealResult.encryptedLinkage);
1294
+ // Write encryptedLinkageProof
1295
+ resultWriter.writeVarIntNum(revealResult.encryptedLinkageProof.length);
1296
+ resultWriter.write(revealResult.encryptedLinkageProof);
1297
+ // Return success code and result
1298
+ const responseWriter = new Utils.Writer();
1299
+ responseWriter.writeUInt8(0); // errorByte = 0
1300
+ responseWriter.write(resultWriter.toArray());
1301
+ return responseWriter.toArray();
1302
+ }
1303
+ case 'revealSpecificKeyLinkage':
1304
+ {
1305
+ // Deserialize key-related parameters and privileged parameters
1306
+ const args = this.decodeKeyRelatedParams(paramsReader);
1307
+ // Read verifier public key
1308
+ const verifierBytes = paramsReader.read(33);
1309
+ args.verifier = Utils.toHex(verifierBytes);
1310
+ // Call the method
1311
+ const revealResult = await this.wallet.revealSpecificKeyLinkage(args, originator);
1312
+ // Serialize the result
1313
+ const resultWriter = new Utils.Writer();
1314
+ // Write prover
1315
+ resultWriter.write(Utils.toArray(revealResult.prover, 'hex'));
1316
+ // Write verifier
1317
+ resultWriter.write(Utils.toArray(revealResult.verifier, 'hex'));
1318
+ // Write counterparty
1319
+ resultWriter.write(Utils.toArray(revealResult.counterparty, 'hex'));
1320
+ // Write securityLevel
1321
+ resultWriter.writeUInt8(revealResult.protocolID[0]);
1322
+ // Write protocol string
1323
+ const protocolBytesOut = Utils.toArray(revealResult.protocolID[1], 'utf8');
1324
+ resultWriter.writeVarIntNum(protocolBytesOut.length);
1325
+ resultWriter.write(protocolBytesOut);
1326
+ // Write keyID
1327
+ const keyIDBytesOut = Utils.toArray(revealResult.keyID, 'utf8');
1328
+ resultWriter.writeVarIntNum(keyIDBytesOut.length);
1329
+ resultWriter.write(keyIDBytesOut);
1330
+ // Write encryptedLinkage
1331
+ resultWriter.writeVarIntNum(revealResult.encryptedLinkage.length);
1332
+ resultWriter.write(revealResult.encryptedLinkage);
1333
+ // Write encryptedLinkageProof
1334
+ resultWriter.writeVarIntNum(revealResult.encryptedLinkageProof.length);
1335
+ resultWriter.write(revealResult.encryptedLinkageProof);
1336
+ // Write proofType
1337
+ resultWriter.writeUInt8(revealResult.proofType);
1338
+ // Return success code and result
1339
+ const responseWriter = new Utils.Writer();
1340
+ responseWriter.writeUInt8(0); // errorByte = 0
1341
+ responseWriter.write(resultWriter.toArray());
1342
+ return responseWriter.toArray();
1343
+ }
1344
+ case 'acquireCertificate':
1345
+ {
1346
+ const args = {};
1347
+ // Read args.type
1348
+ const typeBytes = paramsReader.read(32);
1349
+ args.type = Utils.toBase64(typeBytes);
1350
+ // args.certifier
1351
+ const certifierBytes = paramsReader.read(33);
1352
+ args.certifier = Utils.toHex(certifierBytes);
1353
+ // Read fields
1354
+ const fieldsLength = paramsReader.readVarIntNum();
1355
+ args.fields = {};
1356
+ for (let i = 0; i < fieldsLength; i++) {
1357
+ const fieldNameLength = paramsReader.readVarIntNum();
1358
+ const fieldNameBytes = paramsReader.read(fieldNameLength);
1359
+ const fieldName = Utils.toUTF8(fieldNameBytes);
1360
+ const fieldValueLength = paramsReader.readVarIntNum();
1361
+ const fieldValueBytes = paramsReader.read(fieldValueLength);
1362
+ const fieldValue = Utils.toUTF8(fieldValueBytes);
1363
+ args.fields[fieldName] = fieldValue;
1364
+ }
1365
+ // Read privileged parameters
1366
+ const privilegedFlag = paramsReader.readInt8();
1367
+ if (privilegedFlag === -1) {
1368
+ args.privileged = undefined;
1369
+ }
1370
+ else {
1371
+ args.privileged = privilegedFlag === 1;
1372
+ }
1373
+ const privilegedReasonLength = paramsReader.readInt8();
1374
+ if (privilegedReasonLength === -1) {
1375
+ args.privilegedReason = undefined;
1376
+ }
1377
+ else {
1378
+ const privilegedReasonBytes = paramsReader.read(privilegedReasonLength);
1379
+ args.privilegedReason = Utils.toUTF8(privilegedReasonBytes);
1380
+ }
1381
+ // Read acquisitionProtocol
1382
+ const acquisitionProtocolFlag = paramsReader.readUInt8();
1383
+ args.acquisitionProtocol = acquisitionProtocolFlag === 1 ? 'direct' : 'issuance';
1384
+ if (args.acquisitionProtocol === 'direct') {
1385
+ // args.serialNumber
1386
+ const serialNumberBytes = paramsReader.read(32);
1387
+ args.serialNumber = Utils.toBase64(serialNumberBytes);
1388
+ // args.revocationOutpoint
1389
+ args.revocationOutpoint = this.decodeOutpoint(paramsReader);
1390
+ // args.signature
1391
+ const signatureLength = paramsReader.readVarIntNum();
1392
+ const signatureBytes = paramsReader.read(signatureLength);
1393
+ args.signature = Utils.toHex(signatureBytes);
1394
+ // args.keyringRevealer
1395
+ const keyringRevealerIdentifier = paramsReader.readUInt8();
1396
+ if (keyringRevealerIdentifier === 11) {
1397
+ args.keyringRevealer = 'certifier';
1398
+ }
1399
+ else {
1400
+ const keyringRevealerBytes = [keyringRevealerIdentifier].concat(paramsReader.read(32));
1401
+ args.keyringRevealer = Utils.toHex(keyringRevealerBytes);
1402
+ }
1403
+ // args.keyringForSubject
1404
+ const keyringEntriesLength = paramsReader.readVarIntNum();
1405
+ args.keyringForSubject = {};
1406
+ for (let i = 0; i < keyringEntriesLength; i++) {
1407
+ const fieldKeyLength = paramsReader.readVarIntNum();
1408
+ const fieldKeyBytes = paramsReader.read(fieldKeyLength);
1409
+ const fieldKey = Utils.toUTF8(fieldKeyBytes);
1410
+ const fieldValueLength = paramsReader.readVarIntNum();
1411
+ const fieldValueBytes = paramsReader.read(fieldValueLength);
1412
+ const fieldValue = Utils.toBase64(fieldValueBytes);
1413
+ args.keyringForSubject[fieldKey] = fieldValue;
1414
+ }
1415
+ }
1416
+ else {
1417
+ // args.certifierUrl
1418
+ const certifierUrlLength = paramsReader.readVarIntNum();
1419
+ const certifierUrlBytes = paramsReader.read(certifierUrlLength);
1420
+ args.certifierUrl = Utils.toUTF8(certifierUrlBytes);
1421
+ }
1422
+ // Call the method
1423
+ const acquireResult = await this.wallet.acquireCertificate(args, originator);
1424
+ // Serialize the certificate (assuming Certificate class is available)
1425
+ const cert = new Certificate(acquireResult.type, acquireResult.serialNumber, acquireResult.subject, acquireResult.certifier, acquireResult.revocationOutpoint, acquireResult.fields, acquireResult.signature);
1426
+ const certBin = cert.toBin();
1427
+ // Return success code and certificate binary
1428
+ const responseWriter = new Utils.Writer();
1429
+ responseWriter.writeUInt8(0); // errorByte = 0
1430
+ responseWriter.write(certBin);
1431
+ return responseWriter.toArray();
1432
+ }
1433
+ case 'listCertificates':
1434
+ {
1435
+ const args = {};
1436
+ // Read certifiers
1437
+ const certifiersLength = paramsReader.readVarIntNum();
1438
+ args.certifiers = [];
1439
+ for (let i = 0; i < certifiersLength; i++) {
1440
+ const certifierBytes = paramsReader.read(33);
1441
+ args.certifiers.push(Utils.toHex(certifierBytes));
1442
+ }
1443
+ // Read types
1444
+ const typesLength = paramsReader.readVarIntNum();
1445
+ args.types = [];
1446
+ for (let i = 0; i < typesLength; i++) {
1447
+ const typeBytes = paramsReader.read(32);
1448
+ args.types.push(Utils.toBase64(typeBytes));
1449
+ }
1450
+ // Read limit and offset
1451
+ const limit = paramsReader.readVarIntNum();
1452
+ if (limit >= 0) {
1453
+ args.limit = limit;
1454
+ }
1455
+ else {
1456
+ args.limit = undefined;
1457
+ }
1458
+ const offset = paramsReader.readVarIntNum();
1459
+ if (offset >= 0) {
1460
+ args.offset = offset;
1461
+ }
1462
+ else {
1463
+ args.offset = undefined;
1464
+ }
1465
+ // Read privileged parameters
1466
+ const privilegedFlag = paramsReader.readInt8();
1467
+ if (privilegedFlag === -1) {
1468
+ args.privileged = undefined;
1469
+ }
1470
+ else {
1471
+ args.privileged = privilegedFlag === 1;
1472
+ }
1473
+ const privilegedReasonLength = paramsReader.readInt8();
1474
+ if (privilegedReasonLength === -1) {
1475
+ args.privilegedReason = undefined;
1476
+ }
1477
+ else {
1478
+ const privilegedReasonBytes = paramsReader.read(privilegedReasonLength);
1479
+ args.privilegedReason = Utils.toUTF8(privilegedReasonBytes);
1480
+ }
1481
+ // Call the method
1482
+ const listResult = await this.wallet.listCertificates(args, originator);
1483
+ // Serialize the result
1484
+ const resultWriter = new Utils.Writer();
1485
+ // totalCertificates
1486
+ resultWriter.writeVarIntNum(listResult.totalCertificates);
1487
+ // certificates
1488
+ for (const cert of listResult.certificates) {
1489
+ const certificate = new Certificate(cert.type, cert.serialNumber, cert.subject, cert.certifier, cert.revocationOutpoint, cert.fields, cert.signature);
1490
+ const certBin = certificate.toBin();
1491
+ // Write certificate binary length and data
1492
+ resultWriter.writeVarIntNum(certBin.length);
1493
+ resultWriter.write(certBin);
1494
+ }
1495
+ // Return the response
1496
+ const responseWriter = new Utils.Writer();
1497
+ responseWriter.writeUInt8(0); // errorByte = 0
1498
+ responseWriter.write(resultWriter.toArray());
1499
+ return responseWriter.toArray();
1500
+ }
1501
+ case 'proveCertificate':
1502
+ {
1503
+ const args = {};
1504
+ // Read certificate
1505
+ const cert = {};
1506
+ // Read type
1507
+ const typeBytes = paramsReader.read(32);
1508
+ cert.type = Utils.toBase64(typeBytes);
1509
+ // Read subject
1510
+ const subjectBytes = paramsReader.read(33);
1511
+ cert.subject = Utils.toHex(subjectBytes);
1512
+ // Read serialNumber
1513
+ const serialNumberBytes = paramsReader.read(32);
1514
+ cert.serialNumber = Utils.toBase64(serialNumberBytes);
1515
+ // Read certifier
1516
+ const certifierBytes = paramsReader.read(33);
1517
+ cert.certifier = Utils.toHex(certifierBytes);
1518
+ // Read revocationOutpoint
1519
+ cert.revocationOutpoint = this.decodeOutpoint(paramsReader);
1520
+ // Read signature
1521
+ const signatureLength = paramsReader.readVarIntNum();
1522
+ const signatureBytes = paramsReader.read(signatureLength);
1523
+ cert.signature = Utils.toHex(signatureBytes);
1524
+ // Read fields
1525
+ const fieldsLength = paramsReader.readVarIntNum();
1526
+ cert.fields = {};
1527
+ for (let i = 0; i < fieldsLength; i++) {
1528
+ const fieldNameLength = paramsReader.readVarIntNum();
1529
+ const fieldNameBytes = paramsReader.read(fieldNameLength);
1530
+ const fieldName = Utils.toUTF8(fieldNameBytes);
1531
+ const fieldValueLength = paramsReader.readVarIntNum();
1532
+ const fieldValueBytes = paramsReader.read(fieldValueLength);
1533
+ const fieldValue = Utils.toUTF8(fieldValueBytes);
1534
+ cert.fields[fieldName] = fieldValue;
1535
+ }
1536
+ args.certificate = cert;
1537
+ // Read fields to reveal
1538
+ const fieldsToRevealLength = paramsReader.readVarIntNum();
1539
+ args.fieldsToReveal = [];
1540
+ for (let i = 0; i < fieldsToRevealLength; i++) {
1541
+ const fieldNameLength = paramsReader.readVarIntNum();
1542
+ const fieldNameBytes = paramsReader.read(fieldNameLength);
1543
+ const fieldName = Utils.toUTF8(fieldNameBytes);
1544
+ args.fieldsToReveal.push(fieldName);
1545
+ }
1546
+ // Read verifier
1547
+ const verifierBytes = paramsReader.read(33);
1548
+ args.verifier = Utils.toHex(verifierBytes);
1549
+ // Read privileged parameters
1550
+ const privilegedFlag = paramsReader.readInt8();
1551
+ if (privilegedFlag === -1) {
1552
+ args.privileged = undefined;
1553
+ }
1554
+ else {
1555
+ args.privileged = privilegedFlag === 1;
1556
+ }
1557
+ const privilegedReasonLength = paramsReader.readInt8();
1558
+ if (privilegedReasonLength === -1) {
1559
+ args.privilegedReason = undefined;
1560
+ }
1561
+ else {
1562
+ const privilegedReasonBytes = paramsReader.read(privilegedReasonLength);
1563
+ args.privilegedReason = Utils.toUTF8(privilegedReasonBytes);
1564
+ }
1565
+ // Call the method
1566
+ const proveResult = await this.wallet.proveCertificate(args, originator);
1567
+ // Serialize keyringForVerifier
1568
+ const resultWriter = new Utils.Writer();
1569
+ const keyringEntries = Object.entries(proveResult.keyringForVerifier);
1570
+ resultWriter.writeVarIntNum(keyringEntries.length);
1571
+ for (const [fieldName, fieldValue] of keyringEntries) {
1572
+ const fieldNameBytes = Utils.toArray(fieldName, 'utf8');
1573
+ resultWriter.writeVarIntNum(fieldNameBytes.length);
1574
+ resultWriter.write(fieldNameBytes);
1575
+ const fieldValueBytes = Utils.toArray(fieldValue, 'base64');
1576
+ resultWriter.writeVarIntNum(fieldValueBytes.length);
1577
+ resultWriter.write(fieldValueBytes);
1578
+ }
1579
+ // Return the response
1580
+ const responseWriter = new Utils.Writer();
1581
+ responseWriter.writeUInt8(0); // errorByte = 0
1582
+ responseWriter.write(resultWriter.toArray());
1583
+ return responseWriter.toArray();
1584
+ }
1585
+ case 'relinquishCertificate':
1586
+ {
1587
+ const args = {};
1588
+ // Read type
1589
+ const typeBytes = paramsReader.read(32);
1590
+ args.type = Utils.toBase64(typeBytes);
1591
+ // Read serialNumber
1592
+ const serialNumberBytes = paramsReader.read(32);
1593
+ args.serialNumber = Utils.toBase64(serialNumberBytes);
1594
+ // Read certifier
1595
+ const certifierBytes = paramsReader.read(33);
1596
+ args.certifier = Utils.toHex(certifierBytes);
1597
+ // Call the method
1598
+ await this.wallet.relinquishCertificate(args, originator);
1599
+ // Return success code
1600
+ const responseWriter = new Utils.Writer();
1601
+ responseWriter.writeUInt8(0); // errorByte = 0
1602
+ return responseWriter.toArray();
1603
+ }
1604
+ case 'discoverByIdentityKey':
1605
+ {
1606
+ const args = {};
1607
+ // Read identityKey
1608
+ const identityKeyBytes = paramsReader.read(33);
1609
+ args.identityKey = Utils.toHex(identityKeyBytes);
1610
+ // Read limit and offset
1611
+ const limit = paramsReader.readVarIntNum();
1612
+ if (limit >= 0) {
1613
+ args.limit = limit;
1614
+ }
1615
+ else {
1616
+ args.limit = undefined;
1617
+ }
1618
+ const offset = paramsReader.readVarIntNum();
1619
+ if (offset >= 0) {
1620
+ args.offset = offset;
1621
+ }
1622
+ else {
1623
+ args.offset = undefined;
1624
+ }
1625
+ // Deserialize seekPermission
1626
+ const seekPermission = paramsReader.readInt8();
1627
+ if (seekPermission >= 0) {
1628
+ args.seekPermission = seekPermission === 1;
1629
+ }
1630
+ else {
1631
+ args.seekPermission = undefined;
1632
+ }
1633
+ // Call the method
1634
+ const discoverResult = await this.wallet.discoverByIdentityKey(args, originator);
1635
+ // Serialize the result
1636
+ const result = this.serializeDiscoveryResult(discoverResult);
1637
+ // Return the response
1638
+ const responseWriter = new Utils.Writer();
1639
+ responseWriter.writeUInt8(0); // errorByte = 0
1640
+ responseWriter.write(result);
1641
+ return responseWriter.toArray();
1642
+ }
1643
+ case 'discoverByAttributes':
1644
+ {
1645
+ const args = {};
1646
+ // Read attributes
1647
+ const attributesLength = paramsReader.readVarIntNum();
1648
+ args.attributes = {};
1649
+ for (let i = 0; i < attributesLength; i++) {
1650
+ const fieldKeyLength = paramsReader.readVarIntNum();
1651
+ const fieldKeyBytes = paramsReader.read(fieldKeyLength);
1652
+ const fieldKey = Utils.toUTF8(fieldKeyBytes);
1653
+ const fieldValueLength = paramsReader.readVarIntNum();
1654
+ const fieldValueBytes = paramsReader.read(fieldValueLength);
1655
+ const fieldValue = Utils.toUTF8(fieldValueBytes);
1656
+ args.attributes[fieldKey] = fieldValue;
1657
+ }
1658
+ // Read limit and offset
1659
+ const limit = paramsReader.readVarIntNum();
1660
+ if (limit >= 0) {
1661
+ args.limit = limit;
1662
+ }
1663
+ else {
1664
+ args.limit = undefined;
1665
+ }
1666
+ const offset = paramsReader.readVarIntNum();
1667
+ if (offset >= 0) {
1668
+ args.offset = offset;
1669
+ }
1670
+ else {
1671
+ args.offset = undefined;
1672
+ }
1673
+ // Deserialize seekPermission
1674
+ const seekPermission = paramsReader.readInt8();
1675
+ if (seekPermission >= 0) {
1676
+ args.seekPermission = seekPermission === 1;
1677
+ }
1678
+ else {
1679
+ args.seekPermission = undefined;
1680
+ }
1681
+ // Call the method
1682
+ const discoverResult = await this.wallet.discoverByAttributes(args, originator);
1683
+ // Serialize the result
1684
+ const result = this.serializeDiscoveryResult(discoverResult);
1685
+ // Return the response
1686
+ const responseWriter = new Utils.Writer();
1687
+ responseWriter.writeUInt8(0); // errorByte = 0
1688
+ responseWriter.write(result);
1689
+ return responseWriter.toArray();
1690
+ }
1691
+ default:
1692
+ throw new Error(`Method ${callName} not implemented`);
1693
+ }
1694
+ }
1695
+ catch (err) {
1696
+ const responseWriter = new Utils.Writer();
1697
+ responseWriter.writeUInt8((err).code || 1); // errorCode = 1 (generic error)
1698
+ // Serialize the error message
1699
+ const errorMessage = err.message || 'Unknown error';
1700
+ const errorMessageBytes = Utils.toArray(errorMessage, 'utf8');
1701
+ responseWriter.writeVarIntNum(errorMessageBytes.length);
1702
+ responseWriter.write(errorMessageBytes);
1703
+ // Serialize the stack trace
1704
+ const stackTrace = err.stack || '';
1705
+ const stackTraceBytes = Utils.toArray(stackTrace, 'utf8');
1706
+ responseWriter.writeVarIntNum(stackTraceBytes.length);
1707
+ responseWriter.write(stackTraceBytes);
1708
+ return responseWriter.toArray();
1709
+ }
1710
+ }
1711
+ decodeProtocolID(reader) {
1712
+ const securityLevel = reader.readUInt8();
1713
+ const protocolLength = reader.readVarIntNum();
1714
+ const protocolBytes = reader.read(protocolLength);
1715
+ const protocolString = Utils.toUTF8(protocolBytes);
1716
+ return [securityLevel, protocolString];
1717
+ }
1718
+ decodeString(reader) {
1719
+ const length = reader.readVarIntNum();
1720
+ const bytes = reader.read(length);
1721
+ return Utils.toUTF8(bytes);
1722
+ }
1723
+ decodeCounterparty(reader) {
1724
+ const counterpartyFlag = reader.readUInt8();
1725
+ if (counterpartyFlag === 11) {
1726
+ return 'self';
1727
+ }
1728
+ else if (counterpartyFlag === 12) {
1729
+ return 'anyone';
1730
+ }
1731
+ else if (counterpartyFlag === 0) {
1732
+ return undefined;
1733
+ }
1734
+ else {
1735
+ const counterpartyRemainingBytes = reader.read(32);
1736
+ return Utils.toHex([counterpartyFlag, ...counterpartyRemainingBytes]);
1737
+ }
1738
+ }
1739
+ serializeDiscoveryResult(discoverResult) {
1740
+ const resultWriter = new Utils.Writer();
1741
+ // totalCertificates
1742
+ resultWriter.writeVarIntNum(discoverResult.totalCertificates);
1743
+ // certificates
1744
+ for (const cert of discoverResult.certificates) {
1745
+ // Serialize certificate binary
1746
+ const certificate = new Certificate(cert.type, cert.serialNumber, cert.subject, cert.certifier, cert.revocationOutpoint, cert.fields, cert.signaturre);
1747
+ const certBin = certificate.toBin();
1748
+ // Write certificate binary length and data
1749
+ resultWriter.writeVarIntNum(certBin.length);
1750
+ resultWriter.write(certBin);
1751
+ // Serialize certifierInfo
1752
+ const nameBytes = Utils.toArray(cert.certifierInfo.name, 'utf8');
1753
+ resultWriter.writeVarIntNum(nameBytes.length);
1754
+ resultWriter.write(nameBytes);
1755
+ const iconUrlBytes = Utils.toArray(cert.certifierInfo.iconUrl, 'utf8');
1756
+ resultWriter.writeVarIntNum(iconUrlBytes.length);
1757
+ resultWriter.write(iconUrlBytes);
1758
+ const descriptionBytes = Utils.toArray(cert.certifierInfo.description, 'utf8');
1759
+ resultWriter.writeVarIntNum(descriptionBytes.length);
1760
+ resultWriter.write(descriptionBytes);
1761
+ resultWriter.writeUInt8(cert.certifierInfo.trust);
1762
+ // Serialize publiclyRevealedKeyring
1763
+ const publicKeyringEntries = Object.entries(cert.publiclyRevealedKeyring);
1764
+ resultWriter.writeVarIntNum(publicKeyringEntries.length);
1765
+ for (const [fieldName, fieldValue] of publicKeyringEntries) {
1766
+ const fieldNameBytes = Utils.toArray(fieldName, 'utf8');
1767
+ resultWriter.writeVarIntNum(fieldNameBytes.length);
1768
+ resultWriter.write(fieldNameBytes);
1769
+ const fieldValueBytes = Utils.toArray(fieldValue, 'base64');
1770
+ resultWriter.writeVarIntNum(fieldValueBytes.length);
1771
+ resultWriter.write(fieldValueBytes);
1772
+ }
1773
+ // Serialize decryptedFields
1774
+ const decryptedFieldEntries = Object.entries(cert.decryptedFields);
1775
+ resultWriter.writeVarIntNum(decryptedFieldEntries.length);
1776
+ for (const [fieldName, fieldValue] of decryptedFieldEntries) {
1777
+ const fieldNameBytes = Utils.toArray(fieldName, 'utf8');
1778
+ resultWriter.writeVarIntNum(fieldNameBytes.length);
1779
+ resultWriter.write(fieldNameBytes);
1780
+ const fieldValueBytes = Utils.toArray(fieldValue, 'utf8');
1781
+ resultWriter.writeVarIntNum(fieldValueBytes.length);
1782
+ resultWriter.write(fieldValueBytes);
1783
+ }
1784
+ }
1785
+ return resultWriter.toArray();
1786
+ }
1787
+ decodeKeyRelatedParams(paramsReader) {
1788
+ const args = {};
1789
+ // Read protocolID
1790
+ args.protocolID = this.decodeProtocolID(paramsReader);
1791
+ // Read keyID
1792
+ const keyIDLength = paramsReader.readVarIntNum();
1793
+ const keyIDBytes = paramsReader.read(keyIDLength);
1794
+ args.keyID = Utils.toUTF8(keyIDBytes);
1795
+ // Read counterparty
1796
+ args.counterparty = this.decodeCounterparty(paramsReader);
1797
+ // Read privileged parameters
1798
+ const privilegedFlag = paramsReader.readInt8();
1799
+ if (privilegedFlag === -1) {
1800
+ args.privileged = undefined;
1801
+ }
1802
+ else {
1803
+ args.privileged = privilegedFlag === 1;
1804
+ }
1805
+ const privilegedReasonLength = paramsReader.readInt8();
1806
+ if (privilegedReasonLength === -1) {
1807
+ args.privilegedReason = undefined;
1808
+ }
1809
+ else {
1810
+ const privilegedReasonBytes = paramsReader.read(privilegedReasonLength);
1811
+ args.privilegedReason = Utils.toUTF8(privilegedReasonBytes);
1812
+ }
1813
+ return args;
1814
+ }
1815
+ }
1816
+ //# sourceMappingURL=WalletWireProcessor.js.map