@bsv/sdk 1.1.33 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +537 -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 +3718 -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,1305 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const Certificate_js_1 = __importDefault(require("../../auth/Certificate.js"));
7
+ const index_js_1 = require("../../primitives/index.js");
8
+ const WalletWireCalls_js_1 = __importDefault(require("./WalletWireCalls.js"));
9
+ const WalletError_js_1 = require("../WalletError.js");
10
+ /**
11
+ * A way to make remote calls to a wallet over a wallet wire.
12
+ */
13
+ class WalletWireTransceiver {
14
+ constructor(wire) {
15
+ this.wire = wire;
16
+ }
17
+ async transmit(call, originator = '', params = []) {
18
+ const frameWriter = new index_js_1.Utils.Writer();
19
+ frameWriter.writeUInt8(WalletWireCalls_js_1.default[call]);
20
+ const originatorArray = index_js_1.Utils.toArray(originator, 'utf8');
21
+ frameWriter.writeUInt8(originatorArray.length);
22
+ frameWriter.write(originatorArray);
23
+ if (params.length > 0) {
24
+ frameWriter.write(params);
25
+ }
26
+ const frame = frameWriter.toArray();
27
+ const result = await this.wire.transmitToWallet(frame);
28
+ const resultReader = new index_js_1.Utils.Reader(result);
29
+ const errorByte = resultReader.readUInt8();
30
+ if (errorByte === 0) {
31
+ const resultFrame = resultReader.read();
32
+ return resultFrame;
33
+ }
34
+ else {
35
+ // Deserialize the error message length
36
+ const errorMessageLength = resultReader.readVarIntNum();
37
+ const errorMessageBytes = resultReader.read(errorMessageLength);
38
+ const errorMessage = index_js_1.Utils.toUTF8(errorMessageBytes);
39
+ // Deserialize the stack trace length
40
+ const stackTraceLength = resultReader.readVarIntNum();
41
+ const stackTraceBytes = resultReader.read(stackTraceLength);
42
+ const stackTrace = index_js_1.Utils.toUTF8(stackTraceBytes);
43
+ // Construct a custom wallet error
44
+ const e = new WalletError_js_1.WalletError(errorMessage, errorByte, stackTrace);
45
+ throw e;
46
+ }
47
+ }
48
+ async createAction(args, originator) {
49
+ const paramWriter = new index_js_1.Utils.Writer();
50
+ // Serialize description
51
+ const descriptionBytes = index_js_1.Utils.toArray(args.description, 'utf8');
52
+ paramWriter.writeVarIntNum(descriptionBytes.length);
53
+ paramWriter.write(descriptionBytes);
54
+ // input BEEF
55
+ if (args.inputBEEF) {
56
+ paramWriter.writeVarIntNum(args.inputBEEF.length);
57
+ paramWriter.write(args.inputBEEF);
58
+ }
59
+ else {
60
+ paramWriter.writeVarIntNum(-1);
61
+ }
62
+ // Serialize inputs
63
+ if (args.inputs) {
64
+ paramWriter.writeVarIntNum(args.inputs.length);
65
+ for (const input of args.inputs) {
66
+ // outpoint
67
+ paramWriter.write(this.encodeOutpoint(input.outpoint));
68
+ // unlockingScript / unlockingScriptLength
69
+ if (input.unlockingScript) {
70
+ const unlockingScriptBytes = index_js_1.Utils.toArray(input.unlockingScript, 'hex');
71
+ paramWriter.writeVarIntNum(unlockingScriptBytes.length);
72
+ paramWriter.write(unlockingScriptBytes);
73
+ }
74
+ else {
75
+ paramWriter.writeVarIntNum(-1);
76
+ paramWriter.writeVarIntNum(input.unlockingScriptLength);
77
+ }
78
+ // inputDescription
79
+ const inputDescriptionBytes = index_js_1.Utils.toArray(input.inputDescription, 'utf8');
80
+ paramWriter.writeVarIntNum(inputDescriptionBytes.length);
81
+ paramWriter.write(inputDescriptionBytes);
82
+ // sequenceNumber
83
+ if (typeof input.sequenceNumber === 'number') {
84
+ paramWriter.writeVarIntNum(input.sequenceNumber);
85
+ }
86
+ else {
87
+ paramWriter.writeVarIntNum(-1);
88
+ }
89
+ }
90
+ }
91
+ else {
92
+ paramWriter.writeVarIntNum(-1);
93
+ }
94
+ // Serialize outputs
95
+ if (args.outputs) {
96
+ paramWriter.writeVarIntNum(args.outputs.length);
97
+ for (const output of args.outputs) {
98
+ // lockingScript
99
+ const lockingScriptBytes = index_js_1.Utils.toArray(output.lockingScript, 'hex');
100
+ paramWriter.writeVarIntNum(lockingScriptBytes.length);
101
+ paramWriter.write(lockingScriptBytes);
102
+ // satoshis
103
+ paramWriter.writeVarIntNum(output.satoshis);
104
+ // outputDescription
105
+ const outputDescriptionBytes = index_js_1.Utils.toArray(output.outputDescription, 'utf8');
106
+ paramWriter.writeVarIntNum(outputDescriptionBytes.length);
107
+ paramWriter.write(outputDescriptionBytes);
108
+ // basket
109
+ if (output.basket) {
110
+ const basketBytes = index_js_1.Utils.toArray(output.basket, 'utf8');
111
+ paramWriter.writeVarIntNum(basketBytes.length);
112
+ paramWriter.write(basketBytes);
113
+ }
114
+ else {
115
+ paramWriter.writeVarIntNum(-1);
116
+ }
117
+ // customInstructions
118
+ if (output.customInstructions) {
119
+ const customInstructionsBytes = index_js_1.Utils.toArray(output.customInstructions, 'utf8');
120
+ paramWriter.writeVarIntNum(customInstructionsBytes.length);
121
+ paramWriter.write(customInstructionsBytes);
122
+ }
123
+ else {
124
+ paramWriter.writeVarIntNum(-1);
125
+ }
126
+ // tags
127
+ if (output.tags) {
128
+ paramWriter.writeVarIntNum(output.tags.length);
129
+ for (const tag of output.tags) {
130
+ const tagBytes = index_js_1.Utils.toArray(tag, 'utf8');
131
+ paramWriter.writeVarIntNum(tagBytes.length);
132
+ paramWriter.write(tagBytes);
133
+ }
134
+ }
135
+ else {
136
+ paramWriter.writeVarIntNum(-1);
137
+ }
138
+ }
139
+ }
140
+ else {
141
+ paramWriter.writeVarIntNum(-1);
142
+ }
143
+ // Serialize lockTime
144
+ if (typeof args.lockTime === 'number') {
145
+ paramWriter.writeVarIntNum(args.lockTime);
146
+ }
147
+ else {
148
+ paramWriter.writeVarIntNum(-1);
149
+ }
150
+ // Serialize version
151
+ if (typeof args.version === 'number') {
152
+ paramWriter.writeVarIntNum(args.version);
153
+ }
154
+ else {
155
+ paramWriter.writeVarIntNum(-1);
156
+ }
157
+ // Serialize labels
158
+ if (args.labels) {
159
+ paramWriter.writeVarIntNum(args.labels.length);
160
+ for (const label of args.labels) {
161
+ const labelBytes = index_js_1.Utils.toArray(label, 'utf8');
162
+ paramWriter.writeVarIntNum(labelBytes.length);
163
+ paramWriter.write(labelBytes);
164
+ }
165
+ }
166
+ else {
167
+ paramWriter.writeVarIntNum(-1);
168
+ }
169
+ // Serialize options
170
+ if (args.options) {
171
+ paramWriter.writeInt8(1); // options present
172
+ // signAndProcess
173
+ if (typeof args.options.signAndProcess === 'boolean') {
174
+ paramWriter.writeInt8(args.options.signAndProcess ? 1 : 0);
175
+ }
176
+ else {
177
+ paramWriter.writeInt8(-1);
178
+ }
179
+ // acceptDelayedBroadcast
180
+ if (typeof args.options.acceptDelayedBroadcast === 'boolean') {
181
+ paramWriter.writeInt8(args.options.acceptDelayedBroadcast ? 1 : 0);
182
+ }
183
+ else {
184
+ paramWriter.writeInt8(-1);
185
+ }
186
+ // trustSelf
187
+ if (args.options.trustSelf === 'known') {
188
+ paramWriter.writeInt8(1);
189
+ }
190
+ else {
191
+ paramWriter.writeInt8(-1);
192
+ }
193
+ // knownTxids
194
+ if (args.options.knownTxids) {
195
+ paramWriter.writeVarIntNum(args.options.knownTxids.length);
196
+ for (const txid of args.options.knownTxids) {
197
+ const txidBytes = index_js_1.Utils.toArray(txid, 'hex');
198
+ paramWriter.write(txidBytes);
199
+ }
200
+ }
201
+ else {
202
+ paramWriter.writeVarIntNum(-1);
203
+ }
204
+ // returnTXIDOnly
205
+ if (typeof args.options.returnTXIDOnly === 'boolean') {
206
+ paramWriter.writeInt8(args.options.returnTXIDOnly ? 1 : 0);
207
+ }
208
+ else {
209
+ paramWriter.writeInt8(-1);
210
+ }
211
+ // noSend
212
+ if (typeof args.options.noSend === 'boolean') {
213
+ paramWriter.writeInt8(args.options.noSend ? 1 : 0);
214
+ }
215
+ else {
216
+ paramWriter.writeInt8(-1);
217
+ }
218
+ // noSendChange
219
+ if (args.options.noSendChange) {
220
+ paramWriter.writeVarIntNum(args.options.noSendChange.length);
221
+ for (const outpoint of args.options.noSendChange) {
222
+ paramWriter.write(this.encodeOutpoint(outpoint));
223
+ }
224
+ }
225
+ else {
226
+ paramWriter.writeVarIntNum(-1);
227
+ }
228
+ // sendWith
229
+ if (args.options.sendWith) {
230
+ paramWriter.writeVarIntNum(args.options.sendWith.length);
231
+ for (const txid of args.options.sendWith) {
232
+ const txidBytes = index_js_1.Utils.toArray(txid, 'hex');
233
+ paramWriter.write(txidBytes);
234
+ }
235
+ }
236
+ else {
237
+ paramWriter.writeVarIntNum(-1);
238
+ }
239
+ // randomizeOutputs
240
+ if (typeof args.options.randomizeOutputs === 'boolean') {
241
+ paramWriter.writeInt8(args.options.randomizeOutputs ? 1 : 0);
242
+ }
243
+ else {
244
+ paramWriter.writeInt8(-1);
245
+ }
246
+ }
247
+ else {
248
+ paramWriter.writeInt8(0); // options not present
249
+ }
250
+ // Transmit and parse response
251
+ const result = await this.transmit('createAction', originator, paramWriter.toArray());
252
+ const resultReader = new index_js_1.Utils.Reader(result);
253
+ const response = {};
254
+ // Parse txid
255
+ const txidFlag = resultReader.readInt8();
256
+ if (txidFlag === 1) {
257
+ const txidBytes = resultReader.read(32);
258
+ response.txid = index_js_1.Utils.toHex(txidBytes);
259
+ }
260
+ // Parse tx
261
+ const txFlag = resultReader.readInt8();
262
+ if (txFlag === 1) {
263
+ const txLength = resultReader.readVarIntNum();
264
+ response.tx = resultReader.read(txLength);
265
+ }
266
+ // Parse noSendChange
267
+ const noSendChangeLength = resultReader.readVarIntNum();
268
+ if (noSendChangeLength >= 0) {
269
+ response.noSendChange = [];
270
+ for (let i = 0; i < noSendChangeLength; i++) {
271
+ const outpoint = this.readOutpoint(resultReader);
272
+ response.noSendChange.push(outpoint);
273
+ }
274
+ }
275
+ // Parse sendWithResults
276
+ const sendWithResultsLength = resultReader.readVarIntNum();
277
+ if (sendWithResultsLength >= 0) {
278
+ response.sendWithResults = [];
279
+ for (let i = 0; i < sendWithResultsLength; i++) {
280
+ const txidBytes = resultReader.read(32);
281
+ const txid = index_js_1.Utils.toHex(txidBytes);
282
+ const statusCode = resultReader.readInt8();
283
+ let status;
284
+ if (statusCode === 1)
285
+ status = 'unproven';
286
+ else if (statusCode === 2)
287
+ status = 'sending';
288
+ else if (statusCode === 3)
289
+ status = 'failed';
290
+ response.sendWithResults.push({ txid, status });
291
+ }
292
+ }
293
+ // Parse signableTransaction
294
+ const signableTransactionFlag = resultReader.readInt8();
295
+ if (signableTransactionFlag === 1) {
296
+ const txLength = resultReader.readVarIntNum();
297
+ const tx = resultReader.read(txLength);
298
+ const referenceLength = resultReader.readVarIntNum();
299
+ const referenceBytes = resultReader.read(referenceLength);
300
+ response.signableTransaction = {
301
+ tx,
302
+ reference: index_js_1.Utils.toBase64(referenceBytes)
303
+ };
304
+ }
305
+ return response;
306
+ }
307
+ async signAction(args, originator) {
308
+ const paramWriter = new index_js_1.Utils.Writer();
309
+ // Serialize spends
310
+ const spendIndexes = Object.keys(args.spends);
311
+ paramWriter.writeVarIntNum(spendIndexes.length);
312
+ for (const index of spendIndexes) {
313
+ paramWriter.writeVarIntNum(Number(index));
314
+ const spend = args.spends[Number(index)];
315
+ // unlockingScript
316
+ const unlockingScriptBytes = index_js_1.Utils.toArray(spend.unlockingScript, 'hex');
317
+ paramWriter.writeVarIntNum(unlockingScriptBytes.length);
318
+ paramWriter.write(unlockingScriptBytes);
319
+ // sequenceNumber
320
+ if (typeof spend.sequenceNumber === 'number') {
321
+ paramWriter.writeVarIntNum(spend.sequenceNumber);
322
+ }
323
+ else {
324
+ paramWriter.writeVarIntNum(-1);
325
+ }
326
+ }
327
+ // Serialize reference
328
+ const referenceBytes = index_js_1.Utils.toArray(args.reference, 'base64');
329
+ paramWriter.writeVarIntNum(referenceBytes.length);
330
+ paramWriter.write(referenceBytes);
331
+ // Serialize options
332
+ if (args.options) {
333
+ paramWriter.writeInt8(1); // options present
334
+ // acceptDelayedBroadcast
335
+ if (typeof args.options.acceptDelayedBroadcast === 'boolean') {
336
+ paramWriter.writeInt8(args.options.acceptDelayedBroadcast ? 1 : 0);
337
+ }
338
+ else {
339
+ paramWriter.writeInt8(-1);
340
+ }
341
+ // returnTXIDOnly
342
+ if (typeof args.options.returnTXIDOnly === 'boolean') {
343
+ paramWriter.writeInt8(args.options.returnTXIDOnly ? 1 : 0);
344
+ }
345
+ else {
346
+ paramWriter.writeInt8(-1);
347
+ }
348
+ // noSend
349
+ if (typeof args.options.noSend === 'boolean') {
350
+ paramWriter.writeInt8(args.options.noSend ? 1 : 0);
351
+ }
352
+ else {
353
+ paramWriter.writeInt8(-1);
354
+ }
355
+ // sendWith
356
+ if (args.options.sendWith) {
357
+ paramWriter.writeVarIntNum(args.options.sendWith.length);
358
+ for (const txid of args.options.sendWith) {
359
+ const txidBytes = index_js_1.Utils.toArray(txid, 'hex');
360
+ paramWriter.write(txidBytes);
361
+ }
362
+ }
363
+ else {
364
+ paramWriter.writeVarIntNum(-1);
365
+ }
366
+ }
367
+ else {
368
+ paramWriter.writeInt8(0); // options not present
369
+ }
370
+ // Transmit and parse response
371
+ const result = await this.transmit('signAction', originator, paramWriter.toArray());
372
+ const resultReader = new index_js_1.Utils.Reader(result);
373
+ const response = {};
374
+ // Parse txid
375
+ const txidFlag = resultReader.readInt8();
376
+ if (txidFlag === 1) {
377
+ const txidBytes = resultReader.read(32);
378
+ response.txid = index_js_1.Utils.toHex(txidBytes);
379
+ }
380
+ // Parse tx
381
+ const txFlag = resultReader.readInt8();
382
+ if (txFlag === 1) {
383
+ const txLength = resultReader.readVarIntNum();
384
+ response.tx = resultReader.read(txLength);
385
+ }
386
+ // Parse sendWithResults
387
+ const sendWithResultsLength = resultReader.readVarIntNum();
388
+ if (sendWithResultsLength >= 0) {
389
+ response.sendWithResults = [];
390
+ for (let i = 0; i < sendWithResultsLength; i++) {
391
+ const txidBytes = resultReader.read(32);
392
+ const txid = index_js_1.Utils.toHex(txidBytes);
393
+ const statusCode = resultReader.readInt8();
394
+ let status;
395
+ if (statusCode === 1)
396
+ status = 'unproven';
397
+ else if (statusCode === 2)
398
+ status = 'sending';
399
+ else if (statusCode === 3)
400
+ status = 'failed';
401
+ response.sendWithResults.push({ txid, status });
402
+ }
403
+ }
404
+ return response;
405
+ }
406
+ async abortAction(args, originator) {
407
+ await this.transmit('abortAction', originator, index_js_1.Utils.toArray(args.reference, 'base64'));
408
+ return { aborted: true };
409
+ }
410
+ async listActions(args, originator) {
411
+ const paramWriter = new index_js_1.Utils.Writer();
412
+ // Serialize labels
413
+ paramWriter.writeVarIntNum(args.labels.length);
414
+ for (const label of args.labels) {
415
+ const labelBytes = index_js_1.Utils.toArray(label, 'utf8');
416
+ paramWriter.writeVarIntNum(labelBytes.length);
417
+ paramWriter.write(labelBytes);
418
+ }
419
+ // Serialize labelQueryMode
420
+ if (args.labelQueryMode === 'any') {
421
+ paramWriter.writeInt8(1);
422
+ }
423
+ else if (args.labelQueryMode === 'all') {
424
+ paramWriter.writeInt8(2);
425
+ }
426
+ else {
427
+ paramWriter.writeInt8(-1);
428
+ }
429
+ // Serialize include options
430
+ const includeOptions = [
431
+ args.includeLabels,
432
+ args.includeInputs,
433
+ args.includeInputSourceLockingScripts,
434
+ args.includeInputUnlockingScripts,
435
+ args.includeOutputs,
436
+ args.includeOutputLockingScripts
437
+ ];
438
+ for (const option of includeOptions) {
439
+ if (typeof option === 'boolean') {
440
+ paramWriter.writeInt8(option ? 1 : 0);
441
+ }
442
+ else {
443
+ paramWriter.writeInt8(-1);
444
+ }
445
+ }
446
+ // Serialize limit and offset
447
+ if (typeof args.limit === 'number') {
448
+ paramWriter.writeVarIntNum(args.limit);
449
+ }
450
+ else {
451
+ paramWriter.writeVarIntNum(-1);
452
+ }
453
+ if (typeof args.offset === 'number') {
454
+ paramWriter.writeVarIntNum(args.offset);
455
+ }
456
+ else {
457
+ paramWriter.writeVarIntNum(-1);
458
+ }
459
+ // Serialize seekPermission
460
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
461
+ // Transmit and parse response
462
+ const result = await this.transmit('listActions', originator, paramWriter.toArray());
463
+ const resultReader = new index_js_1.Utils.Reader(result);
464
+ const totalActions = resultReader.readVarIntNum();
465
+ const actions = [];
466
+ for (let i = 0; i < totalActions; i++) {
467
+ // Parse action fields
468
+ const txidBytes = resultReader.read(32);
469
+ const txid = index_js_1.Utils.toHex(txidBytes);
470
+ const satoshis = resultReader.readVarIntNum();
471
+ const statusCode = resultReader.readInt8();
472
+ let status;
473
+ switch (statusCode) {
474
+ case 1:
475
+ status = 'completed';
476
+ break;
477
+ case 2:
478
+ status = 'unprocessed';
479
+ break;
480
+ case 3:
481
+ status = 'sending';
482
+ break;
483
+ case 4:
484
+ status = 'unproven';
485
+ break;
486
+ case 5:
487
+ status = 'unsigned';
488
+ break;
489
+ case 6:
490
+ status = 'nosend';
491
+ break;
492
+ case 7:
493
+ status = 'nonfinal';
494
+ break;
495
+ default:
496
+ throw new Error(`Unknown status code: ${statusCode}`);
497
+ }
498
+ const isOutgoing = resultReader.readInt8() === 1;
499
+ const descriptionLength = resultReader.readVarIntNum();
500
+ const descriptionBytes = resultReader.read(descriptionLength);
501
+ const description = index_js_1.Utils.toUTF8(descriptionBytes);
502
+ const action = {
503
+ txid,
504
+ satoshis,
505
+ status,
506
+ isOutgoing,
507
+ description,
508
+ version: 0,
509
+ lockTime: 0
510
+ };
511
+ // Parse labels
512
+ const labelsLength = resultReader.readVarIntNum();
513
+ if (labelsLength >= 0) {
514
+ action.labels = [];
515
+ for (let j = 0; j < labelsLength; j++) {
516
+ const labelLength = resultReader.readVarIntNum();
517
+ const labelBytes = resultReader.read(labelLength);
518
+ action.labels.push(index_js_1.Utils.toUTF8(labelBytes));
519
+ }
520
+ }
521
+ // Parse version and lockTime
522
+ action.version = resultReader.readVarIntNum();
523
+ action.lockTime = resultReader.readVarIntNum();
524
+ // Parse inputs
525
+ const inputsLength = resultReader.readVarIntNum();
526
+ if (inputsLength >= 0) {
527
+ action.inputs = [];
528
+ for (let k = 0; k < inputsLength; k++) {
529
+ const sourceOutpoint = this.readOutpoint(resultReader);
530
+ const sourceSatoshis = resultReader.readVarIntNum();
531
+ // sourceLockingScript
532
+ const sourceLockingScriptLength = resultReader.readVarIntNum();
533
+ let sourceLockingScript;
534
+ if (sourceLockingScriptLength >= 0) {
535
+ const sourceLockingScriptBytes = resultReader.read(sourceLockingScriptLength);
536
+ sourceLockingScript = index_js_1.Utils.toHex(sourceLockingScriptBytes);
537
+ }
538
+ // unlockingScript
539
+ const unlockingScriptLength = resultReader.readVarIntNum();
540
+ let unlockingScript;
541
+ if (unlockingScriptLength >= 0) {
542
+ const unlockingScriptBytes = resultReader.read(unlockingScriptLength);
543
+ unlockingScript = index_js_1.Utils.toHex(unlockingScriptBytes);
544
+ }
545
+ // inputDescription
546
+ const inputDescriptionLength = resultReader.readVarIntNum();
547
+ const inputDescriptionBytes = resultReader.read(inputDescriptionLength);
548
+ const inputDescription = index_js_1.Utils.toUTF8(inputDescriptionBytes);
549
+ // sequenceNumber
550
+ const sequenceNumber = resultReader.readVarIntNum();
551
+ action.inputs.push({
552
+ sourceOutpoint,
553
+ sourceSatoshis,
554
+ sourceLockingScript,
555
+ unlockingScript,
556
+ inputDescription,
557
+ sequenceNumber
558
+ });
559
+ }
560
+ }
561
+ // Parse outputs
562
+ const outputsLength = resultReader.readVarIntNum();
563
+ if (outputsLength >= 0) {
564
+ action.outputs = [];
565
+ for (let l = 0; l < outputsLength; l++) {
566
+ const outputIndex = resultReader.readVarIntNum();
567
+ const satoshis = resultReader.readVarIntNum();
568
+ // lockingScript
569
+ const lockingScriptLength = resultReader.readVarIntNum();
570
+ let lockingScript;
571
+ if (lockingScriptLength >= 0) {
572
+ const lockingScriptBytes = resultReader.read(lockingScriptLength);
573
+ lockingScript = index_js_1.Utils.toHex(lockingScriptBytes);
574
+ }
575
+ const spendable = resultReader.readInt8() === 1;
576
+ // outputDescription
577
+ const outputDescriptionLength = resultReader.readVarIntNum();
578
+ const outputDescriptionBytes = resultReader.read(outputDescriptionLength);
579
+ const outputDescription = index_js_1.Utils.toUTF8(outputDescriptionBytes);
580
+ // basket
581
+ const basketLength = resultReader.readVarIntNum();
582
+ let basket;
583
+ if (basketLength >= 0) {
584
+ const basketBytes = resultReader.read(basketLength);
585
+ basket = index_js_1.Utils.toUTF8(basketBytes);
586
+ }
587
+ // tags
588
+ const tagsLength = resultReader.readVarIntNum();
589
+ const tags = [];
590
+ if (tagsLength >= 0) {
591
+ for (let m = 0; m < tagsLength; m++) {
592
+ const tagLength = resultReader.readVarIntNum();
593
+ const tagBytes = resultReader.read(tagLength);
594
+ tags.push(index_js_1.Utils.toUTF8(tagBytes));
595
+ }
596
+ }
597
+ // customInstructions
598
+ const customInstructionsLength = resultReader.readVarIntNum();
599
+ let customInstructions;
600
+ if (customInstructionsLength >= 0) {
601
+ const customInstructionsBytes = resultReader.read(customInstructionsLength);
602
+ customInstructions = index_js_1.Utils.toUTF8(customInstructionsBytes);
603
+ }
604
+ action.outputs.push({
605
+ outputIndex,
606
+ satoshis,
607
+ lockingScript,
608
+ spendable,
609
+ outputDescription,
610
+ basket,
611
+ tags,
612
+ customInstructions
613
+ });
614
+ }
615
+ }
616
+ actions.push(action);
617
+ }
618
+ return {
619
+ totalActions,
620
+ actions
621
+ };
622
+ }
623
+ async internalizeAction(args, originator) {
624
+ const paramWriter = new index_js_1.Utils.Writer();
625
+ paramWriter.writeVarIntNum(args.tx.length);
626
+ paramWriter.write(args.tx);
627
+ paramWriter.writeVarIntNum(args.outputs.length);
628
+ for (const out of args.outputs) {
629
+ paramWriter.writeVarIntNum(out.outputIndex);
630
+ if (out.protocol === 'wallet payment') {
631
+ paramWriter.writeUInt8(1);
632
+ paramWriter.write(index_js_1.Utils.toArray(out.paymentRemittance.senderIdentityKey, 'hex'));
633
+ const derivationPrefixAsArray = index_js_1.Utils.toArray(out.paymentRemittance.derivationPrefix, 'base64');
634
+ paramWriter.writeVarIntNum(derivationPrefixAsArray.length);
635
+ paramWriter.write(derivationPrefixAsArray);
636
+ const derivationSuffixAsArray = index_js_1.Utils.toArray(out.paymentRemittance.derivationSuffix, 'base64');
637
+ paramWriter.writeVarIntNum(derivationSuffixAsArray.length);
638
+ paramWriter.write(derivationSuffixAsArray);
639
+ }
640
+ else {
641
+ paramWriter.writeUInt8(2);
642
+ const basketAsArray = index_js_1.Utils.toArray(out.insertionRemittance.basket, 'utf8');
643
+ paramWriter.writeVarIntNum(basketAsArray.length);
644
+ paramWriter.write(basketAsArray);
645
+ if (typeof out.insertionRemittance.customInstructions) {
646
+ const customInstructionsAsArray = index_js_1.Utils.toArray(out.insertionRemittance.customInstructions, 'utf8');
647
+ paramWriter.writeVarIntNum(customInstructionsAsArray.length);
648
+ paramWriter.write(customInstructionsAsArray);
649
+ }
650
+ else {
651
+ paramWriter.writeVarIntNum(-1);
652
+ }
653
+ if (typeof out.insertionRemittance.tags === 'object') {
654
+ paramWriter.writeVarIntNum(out.insertionRemittance.tags.length);
655
+ for (const tag of out.insertionRemittance.tags) {
656
+ const tagAsArray = index_js_1.Utils.toArray(tag, 'utf8');
657
+ paramWriter.writeVarIntNum(tagAsArray.length);
658
+ paramWriter.write(tagAsArray);
659
+ }
660
+ }
661
+ else {
662
+ paramWriter.writeVarIntNum(0);
663
+ }
664
+ }
665
+ }
666
+ if (typeof args.labels === 'object') {
667
+ paramWriter.writeVarIntNum(args.labels.length);
668
+ for (const l of args.labels) {
669
+ const labelAsArray = index_js_1.Utils.toArray(l, 'utf8');
670
+ paramWriter.writeVarIntNum(labelAsArray.length);
671
+ paramWriter.write(labelAsArray);
672
+ }
673
+ }
674
+ else {
675
+ paramWriter.writeVarIntNum(-1);
676
+ }
677
+ const descriptionAsArray = index_js_1.Utils.toArray(args.description);
678
+ paramWriter.writeVarIntNum(descriptionAsArray.length);
679
+ paramWriter.write(descriptionAsArray);
680
+ // Serialize seekPermission
681
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
682
+ await this.transmit('internalizeAction', originator, paramWriter.toArray());
683
+ return { accepted: true };
684
+ }
685
+ async listOutputs(args, originator) {
686
+ const paramWriter = new index_js_1.Utils.Writer();
687
+ const basketAsArray = index_js_1.Utils.toArray(args.basket, 'utf8');
688
+ paramWriter.writeVarIntNum(basketAsArray.length);
689
+ paramWriter.write(basketAsArray);
690
+ if (typeof args.tags === 'object') {
691
+ paramWriter.writeVarIntNum(args.tags.length);
692
+ for (const tag of args.tags) {
693
+ const tagAsArray = index_js_1.Utils.toArray(tag, 'utf8');
694
+ paramWriter.writeVarIntNum(tagAsArray.length);
695
+ paramWriter.write(tagAsArray);
696
+ }
697
+ }
698
+ else {
699
+ paramWriter.writeVarIntNum(0);
700
+ }
701
+ if (args.tagQueryMode === 'all') {
702
+ paramWriter.writeInt8(1);
703
+ }
704
+ else if (args.tagQueryMode === 'any') {
705
+ paramWriter.writeInt8(2);
706
+ }
707
+ else {
708
+ paramWriter.writeInt8(-1);
709
+ }
710
+ if (args.include === 'locking scripts') {
711
+ paramWriter.writeInt8(1);
712
+ }
713
+ else if (args.include === 'entire transactions') {
714
+ paramWriter.writeInt8(2);
715
+ }
716
+ else {
717
+ paramWriter.writeInt8(-1);
718
+ }
719
+ if (typeof args.includeCustomInstructions === 'boolean') {
720
+ paramWriter.writeInt8(args.includeCustomInstructions ? 1 : 0);
721
+ }
722
+ else {
723
+ paramWriter.writeInt8(-1);
724
+ }
725
+ if (typeof args.includeTags === 'boolean') {
726
+ paramWriter.writeInt8(args.includeTags ? 1 : 0);
727
+ }
728
+ else {
729
+ paramWriter.writeInt8(-1);
730
+ }
731
+ if (typeof args.includeLabels === 'boolean') {
732
+ paramWriter.writeInt8(args.includeLabels ? 1 : 0);
733
+ }
734
+ else {
735
+ paramWriter.writeInt8(-1);
736
+ }
737
+ if (typeof args.limit === 'number') {
738
+ paramWriter.writeVarIntNum(args.limit);
739
+ }
740
+ else {
741
+ paramWriter.writeVarIntNum(-1);
742
+ }
743
+ if (typeof args.offset === 'number') {
744
+ paramWriter.writeVarIntNum(args.offset);
745
+ }
746
+ else {
747
+ paramWriter.writeVarIntNum(-1);
748
+ }
749
+ // Serialize seekPermission
750
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
751
+ const result = await this.transmit('listOutputs', originator, paramWriter.toArray());
752
+ const resultReader = new index_js_1.Utils.Reader(result);
753
+ const totalOutputs = resultReader.readVarIntNum();
754
+ const beefLength = resultReader.readVarIntNum();
755
+ let BEEF = undefined;
756
+ if (beefLength >= 0) {
757
+ BEEF = resultReader.read(beefLength);
758
+ }
759
+ const outputs = [];
760
+ for (let i = 0; i < totalOutputs; i++) {
761
+ const outpoint = this.readOutpoint(resultReader);
762
+ const satoshis = resultReader.readVarIntNum();
763
+ const output = {
764
+ spendable: true,
765
+ outpoint,
766
+ satoshis
767
+ };
768
+ const scriptLength = resultReader.readVarIntNum();
769
+ if (scriptLength >= 0) {
770
+ output.lockingScript = index_js_1.Utils.toHex(resultReader.read(scriptLength));
771
+ }
772
+ const customInstructionsLength = resultReader.readVarIntNum();
773
+ if (customInstructionsLength >= 0) {
774
+ output.customInstructions = index_js_1.Utils.toUTF8(resultReader.read(customInstructionsLength));
775
+ }
776
+ const tagsLength = resultReader.readVarIntNum();
777
+ if (tagsLength !== -1) {
778
+ const tags = [];
779
+ for (let i = 0; i < tagsLength; i++) {
780
+ const tagLength = resultReader.readVarIntNum();
781
+ tags.push(index_js_1.Utils.toUTF8(resultReader.read(tagLength)));
782
+ }
783
+ output.tags = tags;
784
+ }
785
+ const labelsLength = resultReader.readVarIntNum();
786
+ if (labelsLength !== -1) {
787
+ const labels = [];
788
+ for (let i = 0; i < labelsLength; i++) {
789
+ const labelLength = resultReader.readVarIntNum();
790
+ labels.push(index_js_1.Utils.toUTF8(resultReader.read(labelLength)));
791
+ }
792
+ output.labels = labels;
793
+ }
794
+ outputs.push(output);
795
+ }
796
+ return {
797
+ totalOutputs,
798
+ BEEF,
799
+ outputs
800
+ };
801
+ }
802
+ async relinquishOutput(args, originator) {
803
+ const paramWriter = new index_js_1.Utils.Writer();
804
+ const basketAsArray = index_js_1.Utils.toArray(args.basket, 'utf8');
805
+ paramWriter.writeVarIntNum(basketAsArray.length);
806
+ paramWriter.write(basketAsArray);
807
+ paramWriter.write(this.encodeOutpoint(args.output));
808
+ await this.transmit('relinquishOutput', originator, paramWriter.toArray());
809
+ return { relinquished: true };
810
+ }
811
+ encodeOutpoint(outpoint) {
812
+ const writer = new index_js_1.Utils.Writer();
813
+ const [txid, index] = outpoint.split('.');
814
+ writer.write(index_js_1.Utils.toArray(txid, 'hex'));
815
+ writer.writeVarIntNum(Number(index));
816
+ return writer.toArray();
817
+ }
818
+ readOutpoint(reader) {
819
+ const txid = index_js_1.Utils.toHex(reader.read(32));
820
+ const index = reader.readVarIntNum();
821
+ return `${txid}.${index}`;
822
+ }
823
+ async getPublicKey(args, originator) {
824
+ const paramWriter = new index_js_1.Utils.Writer();
825
+ paramWriter.writeUInt8(args.identityKey ? 1 : 0);
826
+ if (!args.identityKey) {
827
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
828
+ if (typeof args.forSelf === 'boolean') {
829
+ paramWriter.writeInt8(args.forSelf ? 1 : 0);
830
+ }
831
+ else {
832
+ paramWriter.writeInt8(-1);
833
+ }
834
+ }
835
+ else {
836
+ paramWriter.write(this.encodePrivilegedParams(args.privileged, args.privilegedReason));
837
+ }
838
+ // Serialize seekPermission
839
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
840
+ const result = await this.transmit('getPublicKey', originator, paramWriter.toArray());
841
+ return {
842
+ publicKey: index_js_1.Utils.toHex(result)
843
+ };
844
+ }
845
+ async revealCounterpartyKeyLinkage(args, originator) {
846
+ const paramWriter = new index_js_1.Utils.Writer();
847
+ paramWriter.write(this.encodePrivilegedParams(args.privileged, args.privilegedReason));
848
+ paramWriter.write(index_js_1.Utils.toArray(args.counterparty, 'hex'));
849
+ paramWriter.write(index_js_1.Utils.toArray(args.verifier, 'hex'));
850
+ const result = await this.transmit('revealCounterpartyKeyLinkage', originator, paramWriter.toArray());
851
+ const resultReader = new index_js_1.Utils.Reader(result);
852
+ const prover = index_js_1.Utils.toHex(resultReader.read(33));
853
+ const verifier = index_js_1.Utils.toHex(resultReader.read(33));
854
+ const counterparty = index_js_1.Utils.toHex(resultReader.read(33));
855
+ const revelationTimeLength = resultReader.readVarIntNum();
856
+ const revelationTime = index_js_1.Utils.toUTF8(resultReader.read(revelationTimeLength));
857
+ const encryptedLinkageLength = resultReader.readVarIntNum();
858
+ const encryptedLinkage = resultReader.read(encryptedLinkageLength);
859
+ const encryptedLinkageProofLength = resultReader.readVarIntNum();
860
+ const encryptedLinkageProof = resultReader.read(encryptedLinkageProofLength);
861
+ return {
862
+ prover,
863
+ verifier,
864
+ counterparty,
865
+ revelationTime,
866
+ encryptedLinkage,
867
+ encryptedLinkageProof
868
+ };
869
+ }
870
+ async revealSpecificKeyLinkage(args, originator) {
871
+ const paramWriter = new index_js_1.Utils.Writer();
872
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
873
+ paramWriter.write(index_js_1.Utils.toArray(args.verifier, 'hex'));
874
+ const result = await this.transmit('revealSpecificKeyLinkage', originator, paramWriter.toArray());
875
+ const resultReader = new index_js_1.Utils.Reader(result);
876
+ const prover = index_js_1.Utils.toHex(resultReader.read(33));
877
+ const verifier = index_js_1.Utils.toHex(resultReader.read(33));
878
+ const counterparty = index_js_1.Utils.toHex(resultReader.read(33));
879
+ const securityLevel = resultReader.readUInt8();
880
+ const protocolLength = resultReader.readVarIntNum();
881
+ const protocol = index_js_1.Utils.toUTF8(resultReader.read(protocolLength));
882
+ const keyIDLength = resultReader.readVarIntNum();
883
+ const keyID = index_js_1.Utils.toUTF8(resultReader.read(keyIDLength));
884
+ const encryptedLinkageLength = resultReader.readVarIntNum();
885
+ const encryptedLinkage = resultReader.read(encryptedLinkageLength);
886
+ const encryptedLinkageProofLength = resultReader.readVarIntNum();
887
+ const encryptedLinkageProof = resultReader.read(encryptedLinkageProofLength);
888
+ const proofType = resultReader.readUInt8();
889
+ return {
890
+ prover,
891
+ verifier,
892
+ counterparty,
893
+ protocolID: [securityLevel, protocol],
894
+ keyID,
895
+ encryptedLinkage,
896
+ encryptedLinkageProof,
897
+ proofType
898
+ };
899
+ }
900
+ async encrypt(args, originator) {
901
+ const paramWriter = new index_js_1.Utils.Writer();
902
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
903
+ paramWriter.writeVarIntNum(args.plaintext.length);
904
+ paramWriter.write(args.plaintext);
905
+ // Serialize seekPermission
906
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
907
+ return {
908
+ ciphertext: await this.transmit('encrypt', originator, paramWriter.toArray())
909
+ };
910
+ }
911
+ async decrypt(args, originator) {
912
+ const paramWriter = new index_js_1.Utils.Writer();
913
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
914
+ paramWriter.writeVarIntNum(args.ciphertext.length);
915
+ paramWriter.write(args.ciphertext);
916
+ // Serialize seekPermission
917
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
918
+ return {
919
+ plaintext: await this.transmit('decrypt', originator, paramWriter.toArray())
920
+ };
921
+ }
922
+ async createHmac(args, originator) {
923
+ const paramWriter = new index_js_1.Utils.Writer();
924
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
925
+ paramWriter.writeVarIntNum(args.data.length);
926
+ paramWriter.write(args.data);
927
+ // Serialize seekPermission
928
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
929
+ return {
930
+ hmac: await this.transmit('createHmac', originator, paramWriter.toArray())
931
+ };
932
+ }
933
+ async verifyHmac(args, originator) {
934
+ const paramWriter = new index_js_1.Utils.Writer();
935
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
936
+ paramWriter.write(args.hmac);
937
+ paramWriter.writeVarIntNum(args.data.length);
938
+ paramWriter.write(args.data);
939
+ // Serialize seekPermission
940
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
941
+ await this.transmit('verifyHmac', originator, paramWriter.toArray());
942
+ return { valid: true };
943
+ }
944
+ async createSignature(args, originator) {
945
+ const paramWriter = new index_js_1.Utils.Writer();
946
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
947
+ if (typeof args.data === 'object') {
948
+ paramWriter.writeUInt8(1);
949
+ paramWriter.writeVarIntNum(args.data.length);
950
+ paramWriter.write(args.data);
951
+ }
952
+ else {
953
+ paramWriter.writeUInt8(2);
954
+ paramWriter.write(args.hashToDirectlySign);
955
+ }
956
+ // Serialize seekPermission
957
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
958
+ return {
959
+ signature: await this.transmit('createSignature', originator, paramWriter.toArray())
960
+ };
961
+ }
962
+ async verifySignature(args, originator) {
963
+ const paramWriter = new index_js_1.Utils.Writer();
964
+ paramWriter.write(this.encodeKeyRelatedParams(args.protocolID, args.keyID, args.counterparty, args.privileged, args.privilegedReason));
965
+ if (typeof args.forSelf === 'boolean') {
966
+ paramWriter.writeInt8(args.forSelf ? 1 : 0);
967
+ }
968
+ else {
969
+ paramWriter.writeInt8(-1);
970
+ }
971
+ paramWriter.writeVarIntNum(args.signature.length);
972
+ paramWriter.write(args.signature);
973
+ if (typeof args.data === 'object') {
974
+ paramWriter.writeUInt8(1);
975
+ paramWriter.writeVarIntNum(args.data.length);
976
+ paramWriter.write(args.data);
977
+ }
978
+ else {
979
+ paramWriter.writeUInt8(2);
980
+ paramWriter.write(args.hashToDirectlyVerify);
981
+ }
982
+ // Serialize seekPermission
983
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
984
+ await this.transmit('verifySignature', originator, paramWriter.toArray());
985
+ return { valid: true };
986
+ }
987
+ encodeKeyRelatedParams(protocolID, keyID, counterparty, privileged, privilegedReason) {
988
+ const paramWriter = new index_js_1.Utils.Writer();
989
+ paramWriter.writeUInt8(protocolID[0]);
990
+ const protocolAsArray = index_js_1.Utils.toArray(protocolID[1], 'utf8');
991
+ paramWriter.writeVarIntNum(protocolAsArray.length);
992
+ paramWriter.write(protocolAsArray);
993
+ const keyIDAsArray = index_js_1.Utils.toArray(keyID, 'utf8');
994
+ paramWriter.writeVarIntNum(keyIDAsArray.length);
995
+ paramWriter.write(keyIDAsArray);
996
+ if (typeof counterparty !== 'string') {
997
+ paramWriter.writeUInt8(0);
998
+ }
999
+ else if (counterparty === 'self') {
1000
+ paramWriter.writeUInt8(11);
1001
+ }
1002
+ else if (counterparty === 'anyone') {
1003
+ paramWriter.writeUInt8(12);
1004
+ }
1005
+ else {
1006
+ paramWriter.write(index_js_1.Utils.toArray(counterparty, 'hex'));
1007
+ }
1008
+ paramWriter.write(this.encodePrivilegedParams(privileged, privilegedReason));
1009
+ return paramWriter.toArray();
1010
+ }
1011
+ async acquireCertificate(args, originator) {
1012
+ const paramWriter = new index_js_1.Utils.Writer();
1013
+ paramWriter.write(index_js_1.Utils.toArray(args.type, 'base64'));
1014
+ paramWriter.write(index_js_1.Utils.toArray(args.certifier, 'hex'));
1015
+ const fieldEntries = Object.entries(args.fields);
1016
+ paramWriter.writeVarIntNum(fieldEntries.length);
1017
+ for (const [key, value] of fieldEntries) {
1018
+ const keyAsArray = index_js_1.Utils.toArray(key, 'utf8');
1019
+ const valueAsArray = index_js_1.Utils.toArray(value, 'utf8');
1020
+ paramWriter.writeVarIntNum(keyAsArray.length);
1021
+ paramWriter.write(keyAsArray);
1022
+ paramWriter.writeVarIntNum(valueAsArray.length);
1023
+ paramWriter.write(valueAsArray);
1024
+ }
1025
+ paramWriter.write(this.encodePrivilegedParams(args.privileged, args.privilegedReason));
1026
+ paramWriter.writeUInt8(args.acquisitionProtocol === 'direct' ? 1 : 2);
1027
+ if (args.acquisitionProtocol === 'direct') {
1028
+ paramWriter.write(index_js_1.Utils.toArray(args.serialNumber, 'base64'));
1029
+ paramWriter.write(this.encodeOutpoint(args.revocationOutpoint));
1030
+ const signatureAsArray = index_js_1.Utils.toArray(args.signature, 'hex');
1031
+ paramWriter.writeVarIntNum(signatureAsArray.length);
1032
+ paramWriter.write(signatureAsArray);
1033
+ const keyringRevealerAsArray = args.keyringRevealer !== 'certifier'
1034
+ ? index_js_1.Utils.toArray(args.keyringRevealer, 'hex')
1035
+ : [11];
1036
+ paramWriter.write(keyringRevealerAsArray);
1037
+ const keyringKeys = Object.keys(args.keyringForSubject);
1038
+ paramWriter.writeVarIntNum(keyringKeys.length);
1039
+ for (let i = 0; i < keyringKeys.length; i++) {
1040
+ const keyringKeysAsArray = index_js_1.Utils.toArray(keyringKeys[i], 'utf8');
1041
+ paramWriter.writeVarIntNum(keyringKeysAsArray.length);
1042
+ paramWriter.write(keyringKeysAsArray);
1043
+ const keyringForSubjectAsArray = index_js_1.Utils.toArray(args.keyringForSubject[keyringKeys[i]], 'base64');
1044
+ paramWriter.writeVarIntNum(keyringForSubjectAsArray.length);
1045
+ paramWriter.write(keyringForSubjectAsArray);
1046
+ }
1047
+ }
1048
+ else {
1049
+ const certifierUrlAsArray = index_js_1.Utils.toArray(args.certifierUrl, 'utf8');
1050
+ paramWriter.writeVarIntNum(certifierUrlAsArray.length);
1051
+ paramWriter.write(certifierUrlAsArray);
1052
+ }
1053
+ const result = await this.transmit('acquireCertificate', originator, paramWriter.toArray());
1054
+ const cert = Certificate_js_1.default.fromBin(result);
1055
+ return {
1056
+ ...cert,
1057
+ signature: cert.signature
1058
+ };
1059
+ }
1060
+ encodePrivilegedParams(privileged, privilegedReason) {
1061
+ const paramWriter = new index_js_1.Utils.Writer();
1062
+ if (typeof privileged === 'boolean') {
1063
+ paramWriter.writeInt8(privileged ? 1 : 0);
1064
+ }
1065
+ else {
1066
+ paramWriter.writeInt8(-1);
1067
+ }
1068
+ if (typeof privilegedReason === 'string') {
1069
+ const privilegedReasonAsArray = index_js_1.Utils.toArray(privilegedReason, 'utf8');
1070
+ paramWriter.writeInt8(privilegedReasonAsArray.length);
1071
+ paramWriter.write(privilegedReasonAsArray);
1072
+ }
1073
+ else {
1074
+ paramWriter.writeInt8(-1);
1075
+ }
1076
+ return paramWriter.toArray();
1077
+ }
1078
+ async listCertificates(args, originator) {
1079
+ const paramWriter = new index_js_1.Utils.Writer();
1080
+ paramWriter.writeVarIntNum(args.certifiers.length);
1081
+ for (let i = 0; i < args.certifiers.length; i++) {
1082
+ paramWriter.write(index_js_1.Utils.toArray(args.certifiers[i], 'hex'));
1083
+ }
1084
+ paramWriter.writeVarIntNum(args.types.length);
1085
+ for (let i = 0; i < args.types.length; i++) {
1086
+ paramWriter.write(index_js_1.Utils.toArray(args.types[i], 'base64'));
1087
+ }
1088
+ if (typeof args.limit === 'number') {
1089
+ paramWriter.writeVarIntNum(args.limit);
1090
+ }
1091
+ else {
1092
+ paramWriter.writeVarIntNum(-1);
1093
+ }
1094
+ if (typeof args.offset === 'number') {
1095
+ paramWriter.writeVarIntNum(args.offset);
1096
+ }
1097
+ else {
1098
+ paramWriter.writeVarIntNum(-1);
1099
+ }
1100
+ paramWriter.write(this.encodePrivilegedParams(args.privileged, args.privilegedReason));
1101
+ const result = await this.transmit('listCertificates', originator, paramWriter.toArray());
1102
+ const resultReader = new index_js_1.Utils.Reader(result);
1103
+ const totalCertificates = resultReader.readVarIntNum();
1104
+ const certificates = [];
1105
+ for (let i = 0; i < totalCertificates; i++) {
1106
+ const certificateLength = resultReader.readVarIntNum();
1107
+ const certificateBin = resultReader.read(certificateLength);
1108
+ const cert = Certificate_js_1.default.fromBin(certificateBin);
1109
+ certificates.push({
1110
+ ...cert,
1111
+ signature: cert.signature
1112
+ });
1113
+ }
1114
+ return {
1115
+ totalCertificates,
1116
+ certificates
1117
+ };
1118
+ }
1119
+ async proveCertificate(args, originator) {
1120
+ const paramWriter = new index_js_1.Utils.Writer();
1121
+ const typeAsArray = index_js_1.Utils.toArray(args.certificate.type, 'base64');
1122
+ paramWriter.write(typeAsArray);
1123
+ const subjectAsArray = index_js_1.Utils.toArray(args.certificate.subject, 'hex');
1124
+ paramWriter.write(subjectAsArray);
1125
+ const serialNumberAsArray = index_js_1.Utils.toArray(args.certificate.serialNumber, 'base64');
1126
+ paramWriter.write(serialNumberAsArray);
1127
+ const certifierAsArray = index_js_1.Utils.toArray(args.certificate.certifier, 'hex');
1128
+ paramWriter.write(certifierAsArray);
1129
+ const revocationOutpointAsArray = this.encodeOutpoint(args.certificate.revocationOutpoint);
1130
+ paramWriter.write(revocationOutpointAsArray);
1131
+ const signatureAsArray = index_js_1.Utils.toArray(args.certificate.signature, 'hex');
1132
+ paramWriter.writeVarIntNum(signatureAsArray.length);
1133
+ paramWriter.write(signatureAsArray);
1134
+ const fieldEntries = Object.entries(args.certificate.fields);
1135
+ paramWriter.writeVarIntNum(fieldEntries.length);
1136
+ for (const [key, value] of fieldEntries) {
1137
+ const keyAsArray = index_js_1.Utils.toArray(key, 'utf8');
1138
+ const valueAsArray = index_js_1.Utils.toArray(value, 'utf8');
1139
+ paramWriter.writeVarIntNum(keyAsArray.length);
1140
+ paramWriter.write(keyAsArray);
1141
+ paramWriter.writeVarIntNum(valueAsArray.length);
1142
+ paramWriter.write(valueAsArray);
1143
+ }
1144
+ paramWriter.writeVarIntNum(args.fieldsToReveal.length);
1145
+ for (const field of args.fieldsToReveal) {
1146
+ const fieldAsArray = index_js_1.Utils.toArray(field, 'utf8');
1147
+ paramWriter.writeVarIntNum(fieldAsArray.length);
1148
+ paramWriter.write(fieldAsArray);
1149
+ }
1150
+ paramWriter.write(index_js_1.Utils.toArray(args.verifier, 'hex'));
1151
+ paramWriter.write(this.encodePrivilegedParams(args.privileged, args.privilegedReason));
1152
+ const result = await this.transmit('proveCertificate', originator, paramWriter.toArray());
1153
+ const resultReader = new index_js_1.Utils.Reader(result);
1154
+ const numFields = resultReader.readVarIntNum();
1155
+ const keyringForVerifier = {};
1156
+ for (let i = 0; i < numFields; i++) {
1157
+ const fieldKeyLength = resultReader.readVarIntNum();
1158
+ const fieldKey = index_js_1.Utils.toUTF8(resultReader.read(fieldKeyLength));
1159
+ const fieldValueLength = resultReader.readVarIntNum();
1160
+ keyringForVerifier[fieldKey] = index_js_1.Utils.toBase64(resultReader.read(fieldValueLength));
1161
+ }
1162
+ return {
1163
+ keyringForVerifier
1164
+ };
1165
+ }
1166
+ async relinquishCertificate(args, originator) {
1167
+ const paramWriter = new index_js_1.Utils.Writer();
1168
+ const typeAsArray = index_js_1.Utils.toArray(args.type, 'base64');
1169
+ paramWriter.write(typeAsArray);
1170
+ const serialNumberAsArray = index_js_1.Utils.toArray(args.serialNumber, 'base64');
1171
+ paramWriter.write(serialNumberAsArray);
1172
+ const certifierAsArray = index_js_1.Utils.toArray(args.certifier, 'hex');
1173
+ paramWriter.write(certifierAsArray);
1174
+ await this.transmit('relinquishCertificate', originator, paramWriter.toArray());
1175
+ return { relinquished: true };
1176
+ }
1177
+ parseDiscoveryResult(result) {
1178
+ const resultReader = new index_js_1.Utils.Reader(result);
1179
+ const totalCertificates = resultReader.readVarIntNum();
1180
+ const certificates = [];
1181
+ for (let i = 0; i < totalCertificates; i++) {
1182
+ const certBinLen = resultReader.readVarIntNum();
1183
+ const certBin = resultReader.read(certBinLen);
1184
+ const cert = Certificate_js_1.default.fromBin(certBin);
1185
+ const nameLength = resultReader.readVarIntNum();
1186
+ const name = index_js_1.Utils.toUTF8(resultReader.read(nameLength));
1187
+ const iconUrlLength = resultReader.readVarIntNum();
1188
+ const iconUrl = index_js_1.Utils.toUTF8(resultReader.read(iconUrlLength));
1189
+ const descriptionLength = resultReader.readVarIntNum();
1190
+ const description = index_js_1.Utils.toUTF8(resultReader.read(descriptionLength));
1191
+ const trust = resultReader.readUInt8();
1192
+ const publiclyRevealedKeyring = {};
1193
+ const numPublicKeyringEntries = resultReader.readVarIntNum();
1194
+ for (let j = 0; j < numPublicKeyringEntries; j++) {
1195
+ const fieldKeyLen = resultReader.readVarIntNum();
1196
+ const fieldKey = index_js_1.Utils.toUTF8(resultReader.read(fieldKeyLen));
1197
+ const fieldValueLen = resultReader.readVarIntNum();
1198
+ publiclyRevealedKeyring[fieldKey] = resultReader.read(fieldValueLen);
1199
+ }
1200
+ const decryptedFields = {};
1201
+ const numDecryptedFields = resultReader.readVarIntNum();
1202
+ for (let k = 0; k < numDecryptedFields; k++) {
1203
+ const fieldKeyLen = resultReader.readVarIntNum();
1204
+ const fieldKey = index_js_1.Utils.toUTF8(resultReader.read(fieldKeyLen));
1205
+ const fieldValueLen = resultReader.readVarIntNum();
1206
+ decryptedFields[fieldKey] = index_js_1.Utils.toUTF8(resultReader.read(fieldValueLen));
1207
+ }
1208
+ certificates.push({
1209
+ ...cert,
1210
+ signature: cert.signature,
1211
+ certifierInfo: { iconUrl, name, description, trust },
1212
+ publiclyRevealedKeyring,
1213
+ decryptedFields
1214
+ });
1215
+ }
1216
+ return {
1217
+ totalCertificates,
1218
+ certificates
1219
+ };
1220
+ }
1221
+ async discoverByIdentityKey(args, originator) {
1222
+ const paramWriter = new index_js_1.Utils.Writer();
1223
+ paramWriter.write(index_js_1.Utils.toArray(args.identityKey, 'hex'));
1224
+ if (typeof args.limit === 'number') {
1225
+ paramWriter.writeVarIntNum(args.limit);
1226
+ }
1227
+ else {
1228
+ paramWriter.writeVarIntNum(-1);
1229
+ }
1230
+ if (typeof args.offset === 'number') {
1231
+ paramWriter.writeVarIntNum(args.offset);
1232
+ }
1233
+ else {
1234
+ paramWriter.writeVarIntNum(-1);
1235
+ }
1236
+ // Serialize seekPermission
1237
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
1238
+ const result = await this.transmit('discoverByIdentityKey', originator, paramWriter.toArray());
1239
+ return this.parseDiscoveryResult(result);
1240
+ }
1241
+ async discoverByAttributes(args, originator) {
1242
+ const paramWriter = new index_js_1.Utils.Writer();
1243
+ const attributeKeys = Object.keys(args.attributes);
1244
+ paramWriter.writeVarIntNum(attributeKeys.length);
1245
+ for (let i = 0; i < attributeKeys.length; i++) {
1246
+ paramWriter.writeVarIntNum(attributeKeys[i].length);
1247
+ paramWriter.write(index_js_1.Utils.toArray(attributeKeys[i], 'utf8'));
1248
+ paramWriter.writeVarIntNum(args.attributes[attributeKeys[i]].length);
1249
+ paramWriter.write(index_js_1.Utils.toArray(args.attributes[attributeKeys[i]], 'utf8'));
1250
+ }
1251
+ if (typeof args.limit === 'number') {
1252
+ paramWriter.writeVarIntNum(args.limit);
1253
+ }
1254
+ else {
1255
+ paramWriter.writeVarIntNum(-1);
1256
+ }
1257
+ if (typeof args.offset === 'number') {
1258
+ paramWriter.writeVarIntNum(args.offset);
1259
+ }
1260
+ else {
1261
+ paramWriter.writeVarIntNum(-1);
1262
+ }
1263
+ // Serialize seekPermission
1264
+ paramWriter.writeInt8(typeof args.seekPermission === 'boolean' ? args.seekPermission ? 1 : 0 : -1);
1265
+ const result = await this.transmit('discoverByAttributes', originator, paramWriter.toArray());
1266
+ return this.parseDiscoveryResult(result);
1267
+ }
1268
+ async isAuthenticated(args, originator) {
1269
+ const result = await this.transmit('isAuthenticated', originator);
1270
+ return { authenticated: !!result[0] };
1271
+ }
1272
+ async waitForAuthentication(args, originator) {
1273
+ await this.transmit('waitForAuthentication', originator);
1274
+ return { authenticated: true };
1275
+ }
1276
+ async getHeight(args, originator) {
1277
+ const result = await this.transmit('getHeight', originator);
1278
+ const resultReader = new index_js_1.Utils.Reader(result);
1279
+ return {
1280
+ height: resultReader.readVarIntNum()
1281
+ };
1282
+ }
1283
+ async getHeaderForHeight(args, originator) {
1284
+ const paramWriter = new index_js_1.Utils.Writer();
1285
+ paramWriter.writeVarIntNum(args.height);
1286
+ const header = await this.transmit('getHeaderForHeight', originator, paramWriter.toArray());
1287
+ return {
1288
+ header: index_js_1.Utils.toHex(header)
1289
+ };
1290
+ }
1291
+ async getNetwork(args, originator) {
1292
+ const net = await this.transmit('getNetwork', originator);
1293
+ return {
1294
+ network: net[0] === 0 ? 'mainnet' : 'testnet'
1295
+ };
1296
+ }
1297
+ async getVersion(args, originator) {
1298
+ const version = await this.transmit('getVersion', originator);
1299
+ return {
1300
+ version: index_js_1.Utils.toUTF8(version)
1301
+ };
1302
+ }
1303
+ }
1304
+ exports.default = WalletWireTransceiver;
1305
+ //# sourceMappingURL=WalletWireTransceiver.js.map