@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,215 @@
1
+ import { LockingScript, UnlockingScript, OP } from '../index.js';
2
+ import { Utils, Hash, TransactionSignature, Signature, PublicKey } from '../../primitives/index.js';
3
+ /**
4
+ * For a given piece of data to push onto the stack in script, creates the correct minimally-encoded script chunk,
5
+ * including the correct push operation.
6
+ *
7
+ * TODO: This should be made into a TS-SDK util (distinct from the `minimallyEncode` util)
8
+ */
9
+ const createMinimallyEncodedScriptChunk = (data) => {
10
+ if (data.length === 0) {
11
+ // Could have used OP_0.
12
+ return { op: 0 };
13
+ }
14
+ if (data.length === 1 && data[0] === 0) {
15
+ // Could have used OP_0.
16
+ return { op: 0 };
17
+ }
18
+ if (data.length === 1 && data[0] > 0 && data[0] <= 16) {
19
+ // Could have used OP_0 .. OP_16.
20
+ return { op: 0x50 + data[0] };
21
+ }
22
+ if (data.length === 1 && data[0] === 0x81) {
23
+ // Could have used OP_1NEGATE.
24
+ return { op: 0x4f };
25
+ }
26
+ if (data.length <= 75) {
27
+ // Could have used a direct push (opcode indicating number of bytes
28
+ // pushed + those bytes).
29
+ return { op: data.length, data };
30
+ }
31
+ if (data.length <= 255) {
32
+ // Could have used OP_PUSHDATA.
33
+ return { op: 0x4c, data };
34
+ }
35
+ if (data.length <= 65535) {
36
+ // Could have used OP_PUSHDATA2.
37
+ return { op: 0x4d, data };
38
+ }
39
+ return { op: 0x4e, data };
40
+ };
41
+ export default class PushDrop {
42
+ wallet;
43
+ /**
44
+ * Decodes a PushDrop script back into its token fields and the locking public key. If a signature was present, it will be the last field returned.
45
+ * Warning: Only works with a P2PK lock at the beginning of the script.
46
+ * @param script PushDrop script to decode back into token fields
47
+ * @returns An object containing PushDrop token fields and the locking public key. If a signature was included, it will be the last field.
48
+ */
49
+ static decode(script) {
50
+ const lockingPublicKey = PublicKey.fromString(Utils.toHex(script.chunks[0].data));
51
+ const fields = [];
52
+ for (let i = 2; i < script.chunks.length; i++) {
53
+ const nextOpcode = script.chunks[i + 1].op;
54
+ let chunk = script.chunks[i].data;
55
+ if (!chunk) {
56
+ if (script.chunks[i].op >= 80 && script.chunks[i].op <= 95) {
57
+ chunk = [script.chunks[i].op - 80];
58
+ }
59
+ else if (script.chunks[i].op === 0) {
60
+ chunk = [0];
61
+ }
62
+ else if (script.chunks[i].op === 0x4f) {
63
+ chunk = [0x81];
64
+ }
65
+ }
66
+ fields.push(chunk);
67
+ // If the next value is DROP or 2DROP then this is the final field
68
+ if (nextOpcode === OP.OP_DROP || nextOpcode === OP.OP_2DROP) {
69
+ break;
70
+ }
71
+ }
72
+ return {
73
+ fields,
74
+ lockingPublicKey
75
+ };
76
+ }
77
+ /**
78
+ * Constructs a new instance of the PushDrop class.
79
+ *
80
+ * @param {Wallet} wallet - The wallet interface used for creating signatures and accessing public keys.
81
+ */
82
+ constructor(wallet) {
83
+ this.wallet = wallet;
84
+ }
85
+ /**
86
+ * Creates a PushDrop locking script with arbitrary data fields and a public key lock.
87
+ *
88
+ * @param {number[][]} fields - The token fields to include in the locking script.
89
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID to use.
90
+ * @param {string} keyID - The key ID to use.
91
+ * @param {string} counterparty - The counterparty involved in the transaction, "self" or "anyone".
92
+ * @param {boolean} [forSelf=false] - Flag indicating if the lock is for the creator (default no).
93
+ * @param {boolean} [includeSignature=true] - Flag indicating if a signature should be included in the script (default yes).
94
+ * @returns {Promise<LockingScript>} The generated PushDrop locking script.
95
+ */
96
+ async lock(fields, protocolID, keyID, counterparty, forSelf = false, includeSignature = true, lockPosition = 'before') {
97
+ const { publicKey } = await this.wallet.getPublicKey({
98
+ protocolID,
99
+ keyID,
100
+ counterparty,
101
+ forSelf
102
+ });
103
+ const lockChunks = [];
104
+ const pushDropChunks = [];
105
+ lockChunks.push({ op: publicKey.length / 2, data: Utils.toArray(publicKey, 'hex') });
106
+ lockChunks.push({ op: OP.OP_CHECKSIG });
107
+ if (includeSignature) {
108
+ const dataToSign = fields.reduce((a, e) => [...a, ...e], []);
109
+ const { signature } = await this.wallet.createSignature({
110
+ data: dataToSign,
111
+ protocolID,
112
+ keyID,
113
+ counterparty
114
+ });
115
+ fields.push(signature);
116
+ }
117
+ for (const field of fields) {
118
+ pushDropChunks.push(createMinimallyEncodedScriptChunk(field));
119
+ }
120
+ let notYetDropped = fields.length;
121
+ while (notYetDropped > 1) {
122
+ pushDropChunks.push({ op: OP.OP_2DROP });
123
+ notYetDropped -= 2;
124
+ }
125
+ if (notYetDropped) {
126
+ pushDropChunks.push({ op: OP.OP_DROP });
127
+ }
128
+ if (lockPosition === 'before') {
129
+ return new LockingScript([
130
+ ...lockChunks,
131
+ ...pushDropChunks
132
+ ]);
133
+ }
134
+ else {
135
+ return new LockingScript([
136
+ ...pushDropChunks,
137
+ ...lockChunks
138
+ ]);
139
+ }
140
+ }
141
+ /**
142
+ * Creates an unlocking script for spending a PushDrop token output.
143
+ *
144
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID to use.
145
+ * @param {string} keyID - The key ID to use.
146
+ * @param {string} counterparty - The counterparty involved in the transaction, "self" or "anyone".
147
+ * @param {string} [sourceTXID] - The TXID of the source transaction.
148
+ * @param {number} [sourceSatoshis] - The number of satoshis in the source output.
149
+ * @param {LockingScript} [lockingScript] - The locking script of the source output.
150
+ * @param {'all' | 'none' | 'single'} [signOutputs='all'] - Specifies which outputs to sign.
151
+ * @param {boolean} [anyoneCanPay=false] - Specifies if the anyone-can-pay flag is set.
152
+ * @returns {Object} An object containing functions to sign the transaction and estimate the script length.
153
+ */
154
+ unlock(protocolID, keyID, counterparty, signOutputs = 'all', anyoneCanPay = false, sourceSatoshis, lockingScript) {
155
+ return {
156
+ sign: async (tx, inputIndex) => {
157
+ let signatureScope = TransactionSignature.SIGHASH_FORKID;
158
+ if (signOutputs === 'all') {
159
+ signatureScope |= TransactionSignature.SIGHASH_ALL;
160
+ }
161
+ if (signOutputs === 'none') {
162
+ signatureScope |= TransactionSignature.SIGHASH_NONE;
163
+ }
164
+ if (signOutputs === 'single') {
165
+ signatureScope |= TransactionSignature.SIGHASH_SINGLE;
166
+ }
167
+ if (anyoneCanPay) {
168
+ signatureScope |= TransactionSignature.SIGHASH_ANYONECANPAY;
169
+ }
170
+ const input = tx.inputs[inputIndex];
171
+ const otherInputs = tx.inputs.filter((_, index) => index !== inputIndex);
172
+ const sourceTXID = input.sourceTXID ? input.sourceTXID : input.sourceTransaction?.id('hex');
173
+ if (!sourceTXID) {
174
+ throw new Error('The input sourceTXID or sourceTransaction is required for transaction signing.');
175
+ }
176
+ sourceSatoshis ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].satoshis;
177
+ if (!sourceSatoshis) {
178
+ throw new Error('The sourceSatoshis or input sourceTransaction is required for transaction signing.');
179
+ }
180
+ lockingScript ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].lockingScript;
181
+ if (!lockingScript) {
182
+ throw new Error('The lockingScript or input sourceTransaction is required for transaction signing.');
183
+ }
184
+ const preimage = TransactionSignature.format({
185
+ sourceTXID,
186
+ sourceOutputIndex: input.sourceOutputIndex,
187
+ sourceSatoshis,
188
+ transactionVersion: tx.version,
189
+ otherInputs,
190
+ inputIndex,
191
+ outputs: tx.outputs,
192
+ inputSequence: input.sequence,
193
+ subscript: lockingScript,
194
+ lockTime: tx.lockTime,
195
+ scope: signatureScope
196
+ });
197
+ const preimageHash = Hash.sha256(preimage);
198
+ const { signature: bareSignature } = await this.wallet.createSignature({
199
+ data: preimageHash,
200
+ protocolID,
201
+ keyID,
202
+ counterparty
203
+ });
204
+ const signature = Signature.fromDER([...bareSignature]);
205
+ const txSignature = new TransactionSignature(signature.r, signature.s, signatureScope);
206
+ const sigForScript = txSignature.toChecksigFormat();
207
+ return new UnlockingScript([
208
+ { op: sigForScript.length, data: sigForScript }
209
+ ]);
210
+ },
211
+ estimateLength: async () => 73
212
+ };
213
+ }
214
+ }
215
+ //# sourceMappingURL=PushDrop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PushDrop.js","sourceRoot":"","sources":["../../../../../src/script/templates/PushDrop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,aAAa,EAAE,eAAe,EAAE,EAAE,EAAE,MAAM,aAAa,CA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IAAI,KAAK,CACb,mFAAmF,CACpF,CAAA;gBACH,CAAC;gBAED,MAAM,QAAQ,GAAG,oBAAoB,CAAC,MAAM,CAAC;oBAC3C,UAAU;oBACV,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;oBAC1C,cAAc;oBACd,kBAAkB,EAAE,EAAE,CAAC,OAAO;oBAC9B,WAAW;oBACX,UAAU;oBACV,OAAO,EAAE,EAAE,CAAC,OAAO;oBACnB,aAAa,EAAE,KAAK,CAAC,QAAQ;oBAC7B,SAAS,EAAE,aAAa;oBACxB,QAAQ,EAAE,EAAE,CAAC,QAAQ;oBACrB,KAAK,EAAE,cAAc;iBACtB,CAAC,CAAA;gBACF,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;gBAC1C,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC;oBACrE,IAAI,EAAE,YAAY;oBAClB,UAAU;oBACV,KAAK;oBACL,YAAY;iBACb,CAAC,CAAA;gBACF,MAAM,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAA;gBACvD,MAAM,WAAW,GAAG,IAAI,oBAAoB,CAC1C,SAAS,CAAC,CAAC,EACX,SAAS,CAAC,CAAC,EACX,cAAc,CACf,CAAA;gBACD,MAAM,YAAY,GAAG,WAAW,CAAC,gBAAgB,EAAE,CAAA;gBACnD,OAAO,IAAI,eAAe,CAAC;oBACzB,EAAE,EAAE,EAAE,YAAY,CAAC,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE;iBAChD,CAAC,CAAA;YACJ,CAAC;YACD,cAAc,EAAE,KAAK,IAAI,EAAE,CAAC,EAAE;SAC/B,CAAA;IACH,CAAC;CACF"}
@@ -1,3 +1,4 @@
1
1
  export { default as P2PKH } from './P2PKH.js';
2
2
  export { default as RPuzzle } from './RPuzzle.js';
3
+ export { default as PushDrop } from './PushDrop.js';
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/script/templates/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/script/templates/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAA;AACjD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAA"}
@@ -13,7 +13,7 @@ export function defaultHttpClient() {
13
13
  };
14
14
  if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
15
15
  // Use fetch in a browser environment
16
- return new FetchHttpClient(window.fetch);
16
+ return new FetchHttpClient(window.fetch.bind(window));
17
17
  }
18
18
  else if (typeof require !== 'undefined') {
19
19
  // Use Node.js https module
@@ -1 +1 @@
1
- {"version":3,"file":"DefaultHttpClient.js","sourceRoot":"","sources":["../../../../../src/transaction/http/DefaultHttpClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAEtD;;;;GAIG;AACH,MAAM,UAAU,iBAAiB;IAC/B,MAAM,YAAY,GAAe;QAC/B,KAAK,CAAC,OAAO,CAAE,GAAG,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;QAChE,CAAC;KACF,CAAA;IAED,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;QACxE,qCAAqC;QACrC,OAAO,IAAI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CAAC;YACX,OAAO,YAAY,CAAA;QACrB,CAAC;IACH,CAAC;SAAM,CAAC;QACN,OAAO,YAAY,CAAA;IACrB,CAAC;AACH,CAAC"}
@@ -0,0 +1,174 @@
1
+ import { PrivateKey, PublicKey } from '../primitives/index.js';
2
+ import KeyDeriver from './KeyDeriver.js';
3
+ /**
4
+ * A cached version of KeyDeriver that caches the results of key derivation methods.
5
+ * This is useful for optimizing performance when the same keys are derived multiple times.
6
+ * It supports configurable cache size with sane defaults and maintains cache entries using LRU (Least Recently Used) eviction policy.
7
+ */
8
+ export default class CachedKeyDeriver {
9
+ keyDeriver;
10
+ cache;
11
+ maxCacheSize;
12
+ /**
13
+ * Initializes the CachedKeyDeriver instance with a root private key and optional cache settings.
14
+ * @param {PrivateKey | 'anyone'} rootKey - The root private key or the string 'anyone'.
15
+ * @param {Object} [options] - Optional settings for the cache.
16
+ * @param {number} [options.maxCacheSize=1000] - The maximum number of entries to store in the cache.
17
+ */
18
+ constructor(rootKey, options) {
19
+ this.keyDeriver = new KeyDeriver(rootKey);
20
+ this.cache = new Map();
21
+ this.maxCacheSize = options?.maxCacheSize || 1000;
22
+ }
23
+ /**
24
+ * Derives a public key based on protocol ID, key ID, and counterparty.
25
+ * Caches the result for future calls with the same parameters.
26
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
27
+ * @param {string} keyID - The key identifier.
28
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
29
+ * @param {boolean} [forSelf=false] - Whether deriving for self.
30
+ * @returns {PublicKey} - The derived public key.
31
+ */
32
+ derivePublicKey(protocolID, keyID, counterparty, forSelf = false) {
33
+ const cacheKey = this.generateCacheKey('derivePublicKey', protocolID, keyID, counterparty, forSelf);
34
+ if (this.cache.has(cacheKey)) {
35
+ return this.cacheGet(cacheKey);
36
+ }
37
+ else {
38
+ const result = this.keyDeriver.derivePublicKey(protocolID, keyID, counterparty, forSelf);
39
+ this.cacheSet(cacheKey, result);
40
+ return result;
41
+ }
42
+ }
43
+ /**
44
+ * Derives a private key based on protocol ID, key ID, and counterparty.
45
+ * Caches the result for future calls with the same parameters.
46
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
47
+ * @param {string} keyID - The key identifier.
48
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
49
+ * @returns {PrivateKey} - The derived private key.
50
+ */
51
+ derivePrivateKey(protocolID, keyID, counterparty) {
52
+ const cacheKey = this.generateCacheKey('derivePrivateKey', protocolID, keyID, counterparty);
53
+ if (this.cache.has(cacheKey)) {
54
+ return this.cacheGet(cacheKey);
55
+ }
56
+ else {
57
+ const result = this.keyDeriver.derivePrivateKey(protocolID, keyID, counterparty);
58
+ this.cacheSet(cacheKey, result);
59
+ return result;
60
+ }
61
+ }
62
+ /**
63
+ * Derives a symmetric key based on protocol ID, key ID, and counterparty.
64
+ * Caches the result for future calls with the same parameters.
65
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
66
+ * @param {string} keyID - The key identifier.
67
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
68
+ * @returns {SymmetricKey} - The derived symmetric key.
69
+ * @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'.
70
+ */
71
+ deriveSymmetricKey(protocolID, keyID, counterparty) {
72
+ const cacheKey = this.generateCacheKey('deriveSymmetricKey', protocolID, keyID, counterparty);
73
+ if (this.cache.has(cacheKey)) {
74
+ return this.cacheGet(cacheKey);
75
+ }
76
+ else {
77
+ const result = this.keyDeriver.deriveSymmetricKey(protocolID, keyID, counterparty);
78
+ this.cacheSet(cacheKey, result);
79
+ return result;
80
+ }
81
+ }
82
+ /**
83
+ * Reveals the shared secret between the root key and the counterparty.
84
+ * Caches the result for future calls with the same parameters.
85
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
86
+ * @returns {number[]} - The shared secret as a number array.
87
+ * @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'.
88
+ */
89
+ revealCounterpartySecret(counterparty) {
90
+ const cacheKey = this.generateCacheKey('revealCounterpartySecret', counterparty);
91
+ if (this.cache.has(cacheKey)) {
92
+ return this.cacheGet(cacheKey);
93
+ }
94
+ else {
95
+ const result = this.keyDeriver.revealCounterpartySecret(counterparty);
96
+ this.cacheSet(cacheKey, result);
97
+ return result;
98
+ }
99
+ }
100
+ /**
101
+ * Reveals the specific key association for a given protocol ID, key ID, and counterparty.
102
+ * Caches the result for future calls with the same parameters.
103
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
104
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
105
+ * @param {string} keyID - The key identifier.
106
+ * @returns {number[]} - The specific key association as a number array.
107
+ */
108
+ revealSpecificSecret(counterparty, protocolID, keyID) {
109
+ const cacheKey = this.generateCacheKey('revealSpecificSecret', counterparty, protocolID, keyID);
110
+ if (this.cache.has(cacheKey)) {
111
+ return this.cacheGet(cacheKey);
112
+ }
113
+ else {
114
+ const result = this.keyDeriver.revealSpecificSecret(counterparty, protocolID, keyID);
115
+ this.cacheSet(cacheKey, result);
116
+ return result;
117
+ }
118
+ }
119
+ /**
120
+ * Generates a unique cache key based on the method name and input parameters.
121
+ * @param {string} methodName - The name of the method.
122
+ * @param {...any} args - The arguments passed to the method.
123
+ * @returns {string} - The generated cache key.
124
+ */
125
+ generateCacheKey(methodName, ...args) {
126
+ const serializedArgs = args.map((arg) => this.serializeArgument(arg)).join('|');
127
+ return `${methodName}|${serializedArgs}`;
128
+ }
129
+ /**
130
+ * Serializes an argument to a string for use in a cache key.
131
+ * @param {any} arg - The argument to serialize.
132
+ * @returns {string} - The serialized argument.
133
+ */
134
+ serializeArgument(arg) {
135
+ if (arg instanceof PublicKey || arg instanceof PrivateKey) {
136
+ return arg.toString();
137
+ }
138
+ else if (Array.isArray(arg)) {
139
+ return arg.map((item) => this.serializeArgument(item)).join(',');
140
+ }
141
+ else if (typeof arg === 'object' && arg !== null) {
142
+ return JSON.stringify(arg);
143
+ }
144
+ else {
145
+ return String(arg);
146
+ }
147
+ }
148
+ /**
149
+ * Retrieves an item from the cache and updates its position to reflect recent use.
150
+ * @param {string} cacheKey - The key of the cached item.
151
+ * @returns {any} - The cached value.
152
+ */
153
+ cacheGet(cacheKey) {
154
+ const value = this.cache.get(cacheKey);
155
+ // Update the entry to reflect recent use
156
+ this.cache.delete(cacheKey);
157
+ this.cache.set(cacheKey, value);
158
+ return value;
159
+ }
160
+ /**
161
+ * Adds an item to the cache and evicts the least recently used item if necessary.
162
+ * @param {string} cacheKey - The key of the item to cache.
163
+ * @param {any} value - The value to cache.
164
+ */
165
+ cacheSet(cacheKey, value) {
166
+ if (this.cache.size >= this.maxCacheSize) {
167
+ // Evict the least recently used item (first item in Map)
168
+ const firstKey = this.cache.keys().next().value;
169
+ this.cache.delete(firstKey);
170
+ }
171
+ this.cache.set(cacheKey, value);
172
+ }
173
+ }
174
+ //# sourceMappingURL=CachedKeyDeriver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CachedKeyDeriver.js","sourceRoot":"","sources":["../../../../src/wallet/CachedKeyDeriver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAgB,MAAM,wBAAwB,CAAA;AAC5E,OAAO,UAAU,MAAM,iBAAiB,CAAA;AAExC;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,gBAAgB;IAClB,UAAU,CAAY;IACtB,KAAK,CAAkB;IACvB,YAAY,CAAQ;IAErC;;;;;SAKK;IACL,YAAY,OAA8B,EAAE,OAAmC;QAC7E,IAAI,CAAC,UAAU,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,CAAA;QACzC,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EA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Q,CAAC,QAAgB;QAC/B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QACtC,yCAAyC;QACzC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;QAC3B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;QAC/B,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;SAIK;IACG,QAAQ,CAAC,QAAgB,EAAE,KAAU;QAC3C,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACzC,yDAAyD;YACzD,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;YAC/C,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;QAC7B,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;IACjC,CAAC;CACF"}
@@ -0,0 +1,172 @@
1
+ import { PrivateKey, PublicKey, SymmetricKey, Hash, Utils } from '../primitives/index.js';
2
+ /**
3
+ * Class responsible for deriving various types of keys using a root private key.
4
+ * It supports deriving public and private keys, symmetric keys, and revealing key linkages.
5
+ */
6
+ export default class KeyDeriver {
7
+ rootKey;
8
+ /**
9
+ * Initializes the KeyDeriver instance with a root private key.
10
+ * @param {PrivateKey | 'anyone'} rootKey - The root private key or the string 'anyone'.
11
+ */
12
+ constructor(rootKey) {
13
+ if (rootKey === 'anyone') {
14
+ this.rootKey = new PrivateKey(1);
15
+ }
16
+ else {
17
+ this.rootKey = rootKey;
18
+ }
19
+ }
20
+ /**
21
+ * Derives a public key based on protocol ID, key ID, and counterparty.
22
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
23
+ * @param {string} keyID - The key identifier.
24
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
25
+ * @param {boolean} [forSelf=false] - Whether deriving for self.
26
+ * @returns {PublicKey} - The derived public key.
27
+ */
28
+ derivePublicKey(protocolID, keyID, counterparty, forSelf = false) {
29
+ counterparty = this.normalizeCounterparty(counterparty);
30
+ if (forSelf) {
31
+ return this.rootKey.deriveChild(counterparty, this.computeInvoiceNumber(protocolID, keyID)).toPublicKey();
32
+ }
33
+ else {
34
+ return counterparty.deriveChild(this.rootKey, this.computeInvoiceNumber(protocolID, keyID));
35
+ }
36
+ }
37
+ /**
38
+ * Derives a private key based on protocol ID, key ID, and counterparty.
39
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
40
+ * @param {string} keyID - The key identifier.
41
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
42
+ * @returns {PrivateKey} - The derived private key.
43
+ */
44
+ derivePrivateKey(protocolID, keyID, counterparty) {
45
+ counterparty = this.normalizeCounterparty(counterparty);
46
+ return this.rootKey.deriveChild(counterparty, this.computeInvoiceNumber(protocolID, keyID));
47
+ }
48
+ /**
49
+ * Derives a symmetric key based on protocol ID, key ID, and counterparty.
50
+ * Note: Symmetric keys should not be derivable by everyone due to security risks.
51
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
52
+ * @param {string} keyID - The key identifier.
53
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
54
+ * @returns {SymmetricKey} - The derived symmetric key.
55
+ * @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'.
56
+ */
57
+ deriveSymmetricKey(protocolID, keyID, counterparty) {
58
+ if (counterparty === 'anyone') {
59
+ throw new Error('Symmetric keys (such as encryption keys or HMAC keys) should not be derivable by everyone, because messages would be decryptable by anyone who knows the identity public key of the user, and HMACs would be similarly forgeable.');
60
+ }
61
+ counterparty = this.normalizeCounterparty(counterparty);
62
+ const derivedPublicKey = this.derivePublicKey(protocolID, keyID, counterparty);
63
+ const derivedPrivateKey = this.derivePrivateKey(protocolID, keyID, counterparty);
64
+ return new SymmetricKey(derivedPrivateKey.deriveSharedSecret(derivedPublicKey).x.toArray());
65
+ }
66
+ /**
67
+ * Reveals the shared secret between the root key and the counterparty.
68
+ * Note: This should not be used for 'self'.
69
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
70
+ * @returns {number[]} - The shared secret as a number array.
71
+ * @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'.
72
+ */
73
+ revealCounterpartySecret(counterparty) {
74
+ if (counterparty === 'self') {
75
+ throw new Error('Counterparty secrets cannot be revealed for counterparty=self.');
76
+ }
77
+ counterparty = this.normalizeCounterparty(counterparty);
78
+ // Double-check to ensure not revealing the secret for 'self'
79
+ const self = this.rootKey.toPublicKey();
80
+ const keyDerivedBySelf = this.rootKey.deriveChild(self, 'test').toHex();
81
+ const keyDerivedByCounterparty = this.rootKey.deriveChild(counterparty, 'test').toHex();
82
+ if (keyDerivedBySelf === keyDerivedByCounterparty) {
83
+ throw new Error('Counterparty secrets cannot be revealed for counterparty=self.');
84
+ }
85
+ return this.rootKey.deriveSharedSecret(counterparty).encode(true);
86
+ }
87
+ /**
88
+ * Reveals the specific key association for a given protocol ID, key ID, and counterparty.
89
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
90
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
91
+ * @param {string} keyID - The key identifier.
92
+ * @returns {number[]} - The specific key association as a number array.
93
+ */
94
+ revealSpecificSecret(counterparty, protocolID, keyID) {
95
+ counterparty = this.normalizeCounterparty(counterparty);
96
+ const sharedSecret = this.rootKey.deriveSharedSecret(counterparty);
97
+ const invoiceNumberBin = Utils.toArray(this.computeInvoiceNumber(protocolID, keyID), 'utf8');
98
+ return Hash.sha256hmac(sharedSecret.encode(true), invoiceNumberBin);
99
+ }
100
+ /**
101
+ * Normalizes the counterparty to a public key.
102
+ * @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
103
+ * @returns {PublicKey} - The normalized counterparty public key.
104
+ * @throws {Error} - Throws an error if the counterparty is invalid.
105
+ */
106
+ normalizeCounterparty(counterparty) {
107
+ if (!counterparty) {
108
+ throw new Error('counterparty must be self, anyone or a public key!');
109
+ }
110
+ else if (counterparty === 'self') {
111
+ return this.rootKey.toPublicKey();
112
+ }
113
+ else if (counterparty === 'anyone') {
114
+ return new PrivateKey(1).toPublicKey();
115
+ }
116
+ else if (typeof counterparty === 'string') {
117
+ return PublicKey.fromString(counterparty);
118
+ }
119
+ else {
120
+ return counterparty;
121
+ }
122
+ }
123
+ /**
124
+ * Computes the invoice number based on the protocol ID and key ID.
125
+ * @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
126
+ * @param {string} keyID - The key identifier.
127
+ * @returns {string} - The computed invoice number.
128
+ * @throws {Error} - Throws an error if protocol ID or key ID are invalid.
129
+ */
130
+ computeInvoiceNumber(protocolID, keyID) {
131
+ const securityLevel = protocolID[0];
132
+ if (!Number.isInteger(securityLevel) || securityLevel < 0 || securityLevel > 2) {
133
+ throw new Error('Protocol security level must be 0, 1, or 2');
134
+ }
135
+ const protocolName = protocolID[1].toLowerCase().trim();
136
+ if (keyID.length > 800) {
137
+ throw new Error('Key IDs must be 800 characters or less');
138
+ }
139
+ if (keyID.length < 1) {
140
+ throw new Error('Key IDs must be 1 character or more');
141
+ }
142
+ if (protocolName.length > 400) {
143
+ // Specific linkage revelation is the only protocol ID that can contain another protocol ID.
144
+ // Therefore, we allow it to be long enough to encapsulate the target protocol
145
+ if (protocolName.startsWith('specific linkage revelation ')) {
146
+ // The format is: 'specific linkage revelation x YYYYY'
147
+ // Where: x is the security level and YYYYY is the target protocol
148
+ // Thus, the max acceptable length is 30 + 400 = 430 bytes
149
+ if (protocolName.length > 430) {
150
+ throw new Error('Specific linkage revelation protocol names must be 430 characters or less');
151
+ }
152
+ }
153
+ else {
154
+ throw new Error('Protocol names must be 400 characters or less');
155
+ }
156
+ }
157
+ if (protocolName.length < 5) {
158
+ throw new Error('Protocol names must be 5 characters or more');
159
+ }
160
+ if (protocolName.includes(' ')) {
161
+ throw new Error('Protocol names cannot contain multiple consecutive spaces (" ")');
162
+ }
163
+ if (!/^[a-z0-9 ]+$/g.test(protocolName)) {
164
+ throw new Error('Protocol names can only contain letters, numbers and spaces');
165
+ }
166
+ if (protocolName.endsWith(' protocol')) {
167
+ throw new Error('No need to end your protocol name with " protocol"');
168
+ }
169
+ return `${securityLevel}-${protocolName}-${keyID}`;
170
+ }
171
+ }
172
+ //# sourceMappingURL=KeyDeriver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyDeriver.js","sourceRoot":"","sources":["../../../../src/wallet/KeyDeriver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAEzF;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,OAAO,CAAY;IAEnB;;;SAGK;IACL,YAAY,OAA8B;QACxC,IAAI,OAAO,KAAK,QAAQ,EAAE,CAAC;YACzB,IAAI,CAAC,OAAO,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;QAClC,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACxB,CAAC;IACH,CAAC;IAED;;;;;;;SAOK;IACL,eAAe,CAAC,UAA+B,EAAE,KAAa,EAAE,YAAoD,EAAE,UAAmB,KAAK;QAC5I,YAAY,GAAG,IAAI,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAA;QACvD,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,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