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