mainnet-js 0.4.32 → 0.4.36

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 (53) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/main/transaction/Wif.d.ts +6 -4
  3. package/dist/main/transaction/Wif.js +27 -14
  4. package/dist/main/transaction/Wif.js.map +1 -1
  5. package/dist/main/transaction/allocateFee.d.ts +7 -0
  6. package/dist/main/transaction/allocateFee.js +118 -0
  7. package/dist/main/transaction/allocateFee.js.map +1 -0
  8. package/dist/main/util/index.d.ts +1 -0
  9. package/dist/main/util/index.js +4 -1
  10. package/dist/main/util/index.js.map +1 -1
  11. package/dist/main/wallet/Slp.js +4 -1
  12. package/dist/main/wallet/Slp.js.map +1 -1
  13. package/dist/main/wallet/Wif.d.ts +1 -1
  14. package/dist/main/wallet/Wif.js +28 -4
  15. package/dist/main/wallet/Wif.js.map +1 -1
  16. package/dist/main/wallet/enum.d.ts +9 -0
  17. package/dist/main/wallet/enum.js +11 -1
  18. package/dist/main/wallet/enum.js.map +1 -1
  19. package/dist/main/wallet/interface.d.ts +2 -1
  20. package/dist/mainnet-0.4.36.js +2 -0
  21. package/dist/{mainnet-0.4.32.js.LICENSE.txt → mainnet-0.4.36.js.LICENSE.txt} +0 -0
  22. package/dist/module/transaction/Wif.d.ts +6 -4
  23. package/dist/module/transaction/Wif.js +27 -14
  24. package/dist/module/transaction/Wif.js.map +1 -1
  25. package/dist/module/transaction/allocateFee.d.ts +7 -0
  26. package/dist/module/transaction/allocateFee.js +110 -0
  27. package/dist/module/transaction/allocateFee.js.map +1 -0
  28. package/dist/module/util/index.d.ts +1 -0
  29. package/dist/module/util/index.js +1 -0
  30. package/dist/module/util/index.js.map +1 -1
  31. package/dist/module/wallet/Slp.js +4 -1
  32. package/dist/module/wallet/Slp.js.map +1 -1
  33. package/dist/module/wallet/Wif.d.ts +1 -1
  34. package/dist/module/wallet/Wif.js +29 -5
  35. package/dist/module/wallet/Wif.js.map +1 -1
  36. package/dist/module/wallet/enum.d.ts +9 -0
  37. package/dist/module/wallet/enum.js +10 -0
  38. package/dist/module/wallet/enum.js.map +1 -1
  39. package/dist/module/wallet/interface.d.ts +2 -1
  40. package/dist/tsconfig.browser.tsbuildinfo +1 -1
  41. package/dist/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +1 -1
  43. package/src/Wallet.test.ts +44 -0
  44. package/src/transaction/Wif.ts +39 -15
  45. package/src/transaction/allocateFee.test.ts +309 -0
  46. package/src/transaction/allocateFee.ts +132 -0
  47. package/src/util/index.ts +1 -0
  48. package/src/wallet/Slp.ts +5 -1
  49. package/src/wallet/Wif.test.ts +20 -0
  50. package/src/wallet/Wif.ts +38 -5
  51. package/src/wallet/enum.ts +10 -0
  52. package/src/wallet/interface.ts +2 -1
  53. package/dist/mainnet-0.4.32.js +0 -2
@@ -0,0 +1,309 @@
1
+ import { allocateFee, sortSendRequests } from "./allocateFee";
2
+ import { FeePaidByEnum } from "../wallet/enum";
3
+ import { asSendRequestObject } from "../util/asSendRequestObject";
4
+ import { SendRequest } from "..";
5
+ import { RegTestWallet } from "../wallet/Wif";
6
+
7
+ test("Should get the regtest wallet balance", async () => {
8
+ // Build Alice's wallet from Wallet Import Format string, send some sats
9
+ if (!process.env.ADDRESS) {
10
+ throw Error("Attempted to pass an empty address");
11
+ } else {
12
+ const funder = await RegTestWallet.fromId(
13
+ `wif:regtest:${process.env.PRIVATE_WIF!}`
14
+ );
15
+ const alice = await RegTestWallet.newRandom();
16
+ const bob = await RegTestWallet.newRandom();
17
+ const charlie = await RegTestWallet.newRandom();
18
+ const dave = await RegTestWallet.newRandom();
19
+ const edward = await RegTestWallet.newRandom();
20
+ await funder.send([
21
+ {
22
+ cashaddr: alice.cashaddr!,
23
+ value: 4500,
24
+ unit: "satoshis",
25
+ },
26
+ ]);
27
+ await alice.send(
28
+ [
29
+ {
30
+ cashaddr: bob.cashaddr!,
31
+ unit: "sat",
32
+ value: 549,
33
+ },
34
+ {
35
+ cashaddr: charlie.cashaddr!,
36
+ unit: "sat",
37
+ value: 550,
38
+ },
39
+ {
40
+ cashaddr: dave.cashaddr!,
41
+ unit: "sat",
42
+ value: 551,
43
+ },
44
+ {
45
+ cashaddr: edward.cashaddr!,
46
+ unit: "sat",
47
+ value: 2552,
48
+ },
49
+ ],
50
+ { feePaidBy: FeePaidByEnum.changeThenAny }
51
+ );
52
+ expect(await alice.getBalance("sat")).toBe(0);
53
+ expect(await bob.getBalance("sat")).toBe(0);
54
+ expect(await charlie.getBalance("sat")).toBe(550);
55
+ expect(await dave.getBalance("sat")).toBe(551);
56
+ expect(await edward.getBalance("sat")).toBe(2552);
57
+ }
58
+ });
59
+
60
+ test("Expect first in to subtract fee from first", async () => {
61
+ let to = [
62
+ ["alice", 2000, "sat"],
63
+ ["bob", 2000, "sat"],
64
+ ];
65
+ let fee = 1;
66
+ let requests = asSendRequestObject(to) as SendRequest[];
67
+ let allocatedInputs = await allocateFee(
68
+ requests,
69
+ fee,
70
+ FeePaidByEnum.first,
71
+ BigInt(0)
72
+ );
73
+ expect(allocatedInputs[0].value).toBeLessThan(2000);
74
+ expect(allocatedInputs[1].value).toBe(2000);
75
+ });
76
+
77
+ test("Expect last to subtract fee from last", async () => {
78
+ let to = [
79
+ ["alice", 2000, "sat"],
80
+ ["bob", 2000, "sat"],
81
+ ];
82
+ let fee = 1;
83
+ let requests = asSendRequestObject(to) as SendRequest[];
84
+ let allocatedInputs = allocateFee(
85
+ requests,
86
+ fee,
87
+ FeePaidByEnum.last,
88
+ BigInt(0)
89
+ );
90
+
91
+ expect(allocatedInputs[0].value).toBe(2000);
92
+ expect(allocatedInputs[1].value).toBeLessThan(2000);
93
+ });
94
+
95
+ test("Expect all to allocate fees equally", async () => {
96
+ let to = [
97
+ ["alice", 2000, "sat"],
98
+ ["bob", 2000, "sat"],
99
+ ["charlie", 2000, "sat"],
100
+ ];
101
+ let fee = 3;
102
+ let requests = asSendRequestObject(to) as SendRequest[];
103
+
104
+ let allocatedInputs = allocateFee(
105
+ requests,
106
+ fee,
107
+ FeePaidByEnum.any,
108
+ BigInt(0)
109
+ );
110
+
111
+ expect(allocatedInputs[0].value).toBe(1999);
112
+ expect(allocatedInputs[1].value).toBe(1999);
113
+ expect(allocatedInputs[2].value).toBe(1999);
114
+ });
115
+
116
+ test("Expect all to allocate fees equally, taking dust result", async () => {
117
+ let to = [
118
+ ["alice", 2000, "sat"],
119
+ ["bob", 547, "sat"],
120
+ ["charlie", 2000, "sat"],
121
+ ];
122
+ let fee = 300;
123
+ let requests = asSendRequestObject(to) as SendRequest[];
124
+
125
+ let allocatedInputs = allocateFee(
126
+ requests,
127
+ fee,
128
+ FeePaidByEnum.any,
129
+ BigInt(0)
130
+ );
131
+
132
+ expect(allocatedInputs[0].value).toBe(2000);
133
+ expect(allocatedInputs[1].value).toBe(2000);
134
+ expect(allocatedInputs.length).toBe(2);
135
+ });
136
+
137
+ test("Expect all to allocate fees equally, taking dust result output, dividing remainder", async () => {
138
+ let to = [
139
+ ["alice", 2000, "sat"],
140
+ ["bob", 547, "sat"],
141
+ ["charlie", 2000, "sat"],
142
+ ];
143
+ let fee = 647;
144
+ let requests = asSendRequestObject(to) as SendRequest[];
145
+
146
+ let allocatedInputs = allocateFee(
147
+ requests,
148
+ fee,
149
+ FeePaidByEnum.any,
150
+ BigInt(0)
151
+ );
152
+
153
+ expect(allocatedInputs[0].value).toBe(1950);
154
+ expect(allocatedInputs[1].value).toBe(1950);
155
+ expect(allocatedInputs.length).toBe(2);
156
+ });
157
+
158
+ test("Expect an odd fee to be applied to have remainder applied to first receipt", async () => {
159
+ let to = [
160
+ ["alice", 2000, "sat"],
161
+ ["bob", 2000, "sat"],
162
+ ["charlie", 2000, "sat"],
163
+ ];
164
+ let fee = 301;
165
+ let requests = asSendRequestObject(to) as SendRequest[];
166
+
167
+ let allocatedInputs = allocateFee(
168
+ requests,
169
+ fee,
170
+ FeePaidByEnum.any,
171
+ BigInt(0)
172
+ );
173
+
174
+ expect(allocatedInputs[0].value).toBe(1899);
175
+ expect(allocatedInputs[1].value).toBe(1900);
176
+ expect(allocatedInputs[2].value).toBe(1900);
177
+ });
178
+
179
+ test("Expect insufficient funds to error", async () => {
180
+ expect.assertions(1);
181
+ try {
182
+ let to = [
183
+ ["alice", 2000, "sat"],
184
+ ["bob", 2000, "sat"],
185
+ ["charlie", 2000, "sat"],
186
+ ];
187
+ let fee = 7000;
188
+ let requests = asSendRequestObject(to) as SendRequest[];
189
+ let allocatedInputs = allocateFee(
190
+ requests,
191
+ fee,
192
+ FeePaidByEnum.changeThenAny,
193
+ BigInt(999)
194
+ );
195
+ } catch (e: any) {
196
+ expect(e.message).toBe("Insufficient funds for transaction given fee");
197
+ }
198
+ });
199
+
200
+ test("Expect dust amounts to error", async () => {
201
+ expect.assertions(1);
202
+ try {
203
+ let to = [
204
+ ["alice", 2000, "sat"],
205
+ ["bob", 2000, "sat"],
206
+ ["charlie", 2000, "sat"],
207
+ ];
208
+ let fee = 1500;
209
+ let requests = asSendRequestObject(to) as SendRequest[];
210
+ let allocatedInputs = allocateFee(
211
+ requests,
212
+ fee,
213
+ FeePaidByEnum.first,
214
+ BigInt(0)
215
+ );
216
+ } catch (e: any) {
217
+ expect(e.message).toBe("Fee strategy would result in dust output");
218
+ }
219
+ });
220
+
221
+ test("Expect near-dust amounts not to error", async () => {
222
+ let to = [
223
+ ["alice", 1000, "sat"],
224
+ ["bob", 1000, "sat"],
225
+ ["charlie", 1000, "sat"],
226
+ ];
227
+ let fee = 1362;
228
+ let requests = asSendRequestObject(to) as SendRequest[];
229
+ let result = allocateFee(requests, fee, FeePaidByEnum.any, BigInt(0));
230
+ expect(result[0].value).toBe(546);
231
+ expect(result[1].value).toBe(546);
232
+ expect(result[2].value).toBe(546);
233
+ });
234
+
235
+ test("Expect `any` to not consume change", async () => {
236
+ let to = [
237
+ ["alice", 1000, "sat"],
238
+ ["bob", 1000, "sat"],
239
+ ["charlie", 1000, "sat"],
240
+ ];
241
+ let fee = 1362;
242
+ let requests = asSendRequestObject(to) as SendRequest[];
243
+ let result = allocateFee(requests, fee, FeePaidByEnum.any, BigInt(1362));
244
+ expect(result[0].value).toBe(546);
245
+ expect(result[1].value).toBe(546);
246
+ expect(result[2].value).toBe(546);
247
+ });
248
+
249
+ test("Expect `change,any` to consume only change", async () => {
250
+ let to = [
251
+ ["alice", 1000, "sat"],
252
+ ["bob", 1000, "sat"],
253
+ ["charlie", 1000, "sat"],
254
+ ];
255
+ let fee = 1362;
256
+ let requests = asSendRequestObject(to) as SendRequest[];
257
+ let result = allocateFee(
258
+ requests,
259
+ fee,
260
+ FeePaidByEnum.changeThenAny,
261
+ BigInt(1362)
262
+ );
263
+ expect(result[0].value).toBe(1000);
264
+ expect(result[1].value).toBe(1000);
265
+ expect(result[2].value).toBe(1000);
266
+ });
267
+
268
+ test("Expect `change,any` to use both", async () => {
269
+ let to = [
270
+ ["alice", 1000, "sat"],
271
+ ["bob", 1000, "sat"],
272
+ ["charlie", 1000, "sat"],
273
+ ];
274
+ let fee = 1362 * 2;
275
+ let requests = asSendRequestObject(to) as SendRequest[];
276
+ let result = allocateFee(
277
+ requests,
278
+ fee,
279
+ FeePaidByEnum.changeThenAny,
280
+ BigInt(1362)
281
+ );
282
+ expect(result[0].value).toBe(546);
283
+ expect(result[1].value).toBe(546);
284
+ expect(result[2].value).toBe(546);
285
+ });
286
+
287
+ test("Expect sortSendRequests to sort by lowest value first", async () => {
288
+ let to = [
289
+ ["alice", 2000, "sat"],
290
+ ["bob", 547, "sat"],
291
+ ["charlie", 1, "sat"],
292
+ ["dave", 4, "sat"],
293
+ ["edward", 6, "sat"],
294
+ ["fred", 2000, "sat"],
295
+ ["greg", 2000, "sat"],
296
+ ["harry", 2000, "sat"],
297
+ ];
298
+ let fee = 1;
299
+ let requests = asSendRequestObject(to) as SendRequest[];
300
+
301
+ let result = sortSendRequests(requests);
302
+ expect(result[0].value).toBe(1);
303
+ expect(result[1].value).toBe(4);
304
+ expect(result[2].value).toBe(6);
305
+ expect(result[3].value).toBe(547);
306
+ expect(result[4].value).toBe(2000);
307
+ expect(result[5].value).toBe(2000);
308
+ expect(result.length).toBe(8);
309
+ });
@@ -0,0 +1,132 @@
1
+ import { OpReturnData, SendRequest } from "../wallet/model";
2
+ import { FeePaidByEnum } from "../wallet/enum";
3
+ import { DUST_UTXO_THRESHOLD } from "../constant";
4
+
5
+ export function checkFeeForDust(value: number) {
6
+ if (value < DUST_UTXO_THRESHOLD) {
7
+ throw Error("Fee strategy would result in dust output");
8
+ }
9
+ }
10
+
11
+ export function checkSatsAvailable(
12
+ sendRequestArray: Array<SendRequest>,
13
+ fee: number
14
+ ) {
15
+ let amountAvailable = sendRequestArray.reduce(function (sum, r) {
16
+ return sum + (r.value - DUST_UTXO_THRESHOLD);
17
+ }, 0);
18
+ if (amountAvailable < fee) {
19
+ throw Error("Insufficient funds for transaction given fee");
20
+ }
21
+ }
22
+
23
+ export function checkForOpReturn(
24
+ output: SendRequest | OpReturnData
25
+ ): SendRequest {
26
+ if (output instanceof OpReturnData) {
27
+ throw Error("Cannot specify fee to be paid by OpReturnData");
28
+ }
29
+ return output;
30
+ }
31
+
32
+ export function sortSendRequests(sendRequestArray: Array<SendRequest>) {
33
+ return sendRequestArray.sort(
34
+ (a: SendRequest, b: SendRequest) => a.value - b.value
35
+ );
36
+ }
37
+
38
+ function distributeFees(requests: Array<SendRequest>, fee: number) {
39
+ checkSatsAvailable(requests, fee);
40
+ fee = Number(fee);
41
+ for (let r = 0; r < requests.length; r++) {
42
+ if (fee > 0) {
43
+ checkForOpReturn(requests[r]);
44
+ let perRequestFee = Math.floor(fee / (requests.length - r));
45
+ perRequestFee += fee % (requests.length - r);
46
+ if (requests[r].value - perRequestFee < DUST_UTXO_THRESHOLD) {
47
+ fee -= requests[r].value;
48
+ requests[r].value = 0;
49
+ } else {
50
+ fee -= perRequestFee;
51
+ requests[r].value -= perRequestFee;
52
+ }
53
+ }
54
+ }
55
+ return requests.filter((r) => r.value >= DUST_UTXO_THRESHOLD);
56
+ }
57
+
58
+ function firstPays(requests: Array<SendRequest | OpReturnData>, fee: number) {
59
+ let payer = requests.shift()!;
60
+ payer = checkForOpReturn(payer);
61
+ payer.value = payer.value! - fee;
62
+ checkFeeForDust(payer.value);
63
+ requests.unshift(payer);
64
+ return requests;
65
+ }
66
+ function lastPays(requests: Array<SendRequest | OpReturnData>, fee: number) {
67
+ let payer = requests.pop()!;
68
+ payer = checkForOpReturn(payer);
69
+ payer.value = payer.value! - fee;
70
+ checkFeeForDust(payer.value);
71
+ requests.push(payer);
72
+ return requests;
73
+ }
74
+ function anyPays(requests: Array<SendRequest | OpReturnData>, fee: number) {
75
+ for (let r of requests) {
76
+ checkForOpReturn(r);
77
+ }
78
+ requests = sortSendRequests(requests as Array<SendRequest>);
79
+ requests = distributeFees(requests as Array<SendRequest>, fee);
80
+ return requests;
81
+ }
82
+
83
+ function changeThenFallback(
84
+ requests: Array<SendRequest | OpReturnData>,
85
+ fee: number,
86
+ change: bigint,
87
+ fallbackFn: Function
88
+ ) {
89
+ if (BigInt(fee) > change) {
90
+ let outstandingFee = BigInt(fee) - change;
91
+ requests = fallbackFn(requests, outstandingFee);
92
+ }
93
+ return requests;
94
+ }
95
+
96
+ export function allocateFee(
97
+ requests: Array<SendRequest | OpReturnData>,
98
+ fee: number,
99
+ feePaidBy: FeePaidByEnum,
100
+ change: bigint
101
+ ): Array<SendRequest> {
102
+ if (requests.length > 0) {
103
+ switch (feePaidBy) {
104
+ case FeePaidByEnum.change:
105
+ // handled by default
106
+ break;
107
+ case FeePaidByEnum.changeThenFirst:
108
+ requests = changeThenFallback(requests, fee, change, firstPays);
109
+ break;
110
+ case FeePaidByEnum.changeThenLast:
111
+ requests = changeThenFallback(requests, fee, change, lastPays);
112
+ break;
113
+ case FeePaidByEnum.changeThenAny:
114
+ requests = changeThenFallback(requests, fee, change, anyPays);
115
+ break;
116
+ case FeePaidByEnum.first:
117
+ requests = firstPays(requests, fee);
118
+ break;
119
+ case FeePaidByEnum.last:
120
+ requests = lastPays(requests, fee);
121
+ break;
122
+ case FeePaidByEnum.any:
123
+ requests = anyPays(requests, fee);
124
+ break;
125
+ default:
126
+ throw Error("FeePaidBy option not recognized");
127
+ }
128
+ return requests as Array<SendRequest>;
129
+ } else {
130
+ throw Error("Attempted to specify feePaidBy on zero length SendRequest");
131
+ }
132
+ }
package/src/util/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { amountInSatoshi } from "./amountInSatoshi";
2
2
  export { asSendRequestObject } from "./asSendRequestObject";
3
3
  export { atob, btoa } from "./base64";
4
+ export { binToHex, hexToBin } from "@bitauth/libauth";
4
5
  export { convert, convertObject } from "./convert";
5
6
  export { delay } from "./delay";
6
7
  export { derivedNetwork } from "./deriveNetwork";
package/src/wallet/Slp.ts CHANGED
@@ -53,6 +53,7 @@ import { GsppProvider } from "../slp/GsppProvider";
53
53
  import { delay } from "../util/delay";
54
54
  import { Util } from "./Util";
55
55
  import { Mainnet } from "../index";
56
+ import { FeePaidByEnum } from "./enum";
56
57
 
57
58
  /**
58
59
  * Class to manage an slp enabled wallet.
@@ -680,6 +681,7 @@ export class Slp {
680
681
  privateKey: this.wallet.privateKey,
681
682
  relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
682
683
  slpOutputs: slpOutputsResult.SlpOutputs,
684
+ feePaidBy: FeePaidByEnum.change,
683
685
  });
684
686
 
685
687
  const bchSpendAmount = slpOutputsResult.BchSendRequests.map(
@@ -689,7 +691,8 @@ export class Slp {
689
691
  let fundingUtxos = await getSuitableUtxos(
690
692
  fundingBchUtxos,
691
693
  BigInt(bchSpendAmount) + BigInt(feeEstimate),
692
- bestHeight
694
+ bestHeight,
695
+ FeePaidByEnum.change
693
696
  );
694
697
 
695
698
  if (fundingUtxos.length === 0) {
@@ -704,6 +707,7 @@ export class Slp {
704
707
  privateKey: this.wallet.privateKey,
705
708
  relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
706
709
  slpOutputs: slpOutputsResult.SlpOutputs,
710
+ feePaidBy: FeePaidByEnum.change,
707
711
  });
708
712
 
709
713
  const encodedTransaction = await buildEncodedTransaction(
@@ -501,6 +501,26 @@ describe(`Watch only Wallets`, () => {
501
501
  }
502
502
  });
503
503
 
504
+ test("Should encode and submit a transaction", async () => {
505
+ const aliceWif = `wif:regtest:${process.env.PRIVATE_WIF!}`;
506
+ const aliceWallet = await RegTestWallet.fromId(aliceWif);
507
+ const bobWallet = await RegTestWallet.newRandom();
508
+
509
+ const encodedTransaction = await aliceWallet.encodeTransaction([
510
+ {
511
+ cashaddr: bobWallet.cashaddr!,
512
+ value: 2000,
513
+ unit: "satoshis",
514
+ },
515
+ ]);
516
+ expect(encodedTransaction.length).toBeGreaterThan(0);
517
+
518
+ const txId = await aliceWallet.submitTransaction(encodedTransaction, true);
519
+ expect(txId.length).toBeGreaterThan(0);
520
+
521
+ expect(await bobWallet.getBalance("sat")).toBe(2000);
522
+ });
523
+
504
524
  test("Should get last transaction", async () => {
505
525
  const aliceWif = `wif:regtest:${process.env.PRIVATE_WIF!}`;
506
526
  const aliceWallet = await RegTestWallet.fromId(aliceWif);
package/src/wallet/Wif.ts CHANGED
@@ -31,7 +31,7 @@ import { networkPrefixMap } from "../enum";
31
31
  import { PrivateKeyI, UtxoI } from "../interface";
32
32
 
33
33
  import { BaseWallet } from "./Base";
34
- import { WalletTypeEnum } from "./enum";
34
+ import { FeePaidByEnum, WalletTypeEnum } from "./enum";
35
35
  import {
36
36
  CancelWatchFn,
37
37
  SendRequestOptionsI,
@@ -764,6 +764,13 @@ export class Wallet extends BaseWallet {
764
764
  this._slpSemiAware = true;
765
765
  }
766
766
 
767
+ let feePaidBy;
768
+ if (params.options && params.options.feePaidBy) {
769
+ feePaidBy = params.options.feePaidBy;
770
+ } else {
771
+ feePaidBy = FeePaidByEnum.change;
772
+ }
773
+
767
774
  // get inputs
768
775
  let utxos: UtxoI[];
769
776
  if (params.options && params.options.utxoIds) {
@@ -787,7 +794,12 @@ export class Wallet extends BaseWallet {
787
794
  .fill(0)
788
795
  .map(() => sendRequest);
789
796
 
790
- const fundingUtxos = await getSuitableUtxos(utxos, undefined, bestHeight);
797
+ const fundingUtxos = await getSuitableUtxos(
798
+ utxos,
799
+ undefined,
800
+ bestHeight,
801
+ feePaidBy
802
+ );
791
803
  const relayFeePerByteInSatoshi = await getRelayFeeCache(this.provider!);
792
804
  const fee = await getFeeAmount({
793
805
  utxos: fundingUtxos,
@@ -795,6 +807,7 @@ export class Wallet extends BaseWallet {
795
807
  privateKey: this.privateKey,
796
808
  relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
797
809
  slpOutputs: [],
810
+ feePaidBy: feePaidBy,
798
811
  });
799
812
  const spendableAmount = await sumUtxoValue(fundingUtxos);
800
813
 
@@ -917,7 +930,7 @@ export class Wallet extends BaseWallet {
917
930
  }
918
931
 
919
932
  /**
920
- * encodeTransaction given a list of sendRequests, options and estimate fees.
933
+ * encodeTransaction Encode and sign a transaction given a list of sendRequests, options and estimate fees.
921
934
  * @param {SendRequest[]} sendRequests SendRequests
922
935
  * @param {boolean} discardChange=false
923
936
  * @param {SendRequestOptionsI} options Options of the send requests
@@ -950,6 +963,20 @@ export class Wallet extends BaseWallet {
950
963
  this._slpSemiAware = true;
951
964
  }
952
965
 
966
+ let feePaidBy;
967
+ if (options && options.feePaidBy) {
968
+ feePaidBy = options.feePaidBy;
969
+ } else {
970
+ feePaidBy = FeePaidByEnum.change;
971
+ }
972
+
973
+ let changeAddress;
974
+ if (options && options.changeAddress) {
975
+ changeAddress = options.changeAddress;
976
+ } else {
977
+ changeAddress = "";
978
+ }
979
+
953
980
  // get inputs from options or query all inputs
954
981
  let utxos: UtxoI[];
955
982
  if (options && options.utxoIds) {
@@ -977,12 +1004,14 @@ export class Wallet extends BaseWallet {
977
1004
  privateKey: this.privateKey,
978
1005
  relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
979
1006
  slpOutputs: [],
1007
+ feePaidBy: feePaidBy,
980
1008
  });
981
1009
 
982
1010
  const fundingUtxos = await getSuitableUtxos(
983
1011
  utxos,
984
1012
  BigInt(spendAmount) + BigInt(feeEstimate),
985
- bestHeight
1013
+ bestHeight,
1014
+ feePaidBy
986
1015
  );
987
1016
  if (fundingUtxos.length === 0) {
988
1017
  throw Error(
@@ -995,13 +1024,17 @@ export class Wallet extends BaseWallet {
995
1024
  privateKey: this.privateKey,
996
1025
  relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
997
1026
  slpOutputs: [],
1027
+ feePaidBy: feePaidBy,
998
1028
  });
999
1029
  const encodedTransaction = await buildEncodedTransaction(
1000
1030
  fundingUtxos,
1001
1031
  sendRequests,
1002
1032
  this.privateKey,
1003
1033
  fee,
1004
- discardChange
1034
+ discardChange,
1035
+ [],
1036
+ feePaidBy,
1037
+ changeAddress
1005
1038
  );
1006
1039
 
1007
1040
  return encodedTransaction;
@@ -5,3 +5,13 @@ export enum WalletTypeEnum {
5
5
  Watch = "watch",
6
6
  PrivateKey = "privkey",
7
7
  }
8
+
9
+ export enum FeePaidByEnum {
10
+ change = "change",
11
+ first = "firstOutput",
12
+ any = "anyOutputs",
13
+ last = "lastOutput",
14
+ changeThenFirst = "changeThenFirst",
15
+ changeThenAny = "changeThenAny",
16
+ changeThenLast = "changeThenLast",
17
+ }
@@ -1,4 +1,4 @@
1
- import { WalletTypeEnum } from "./enum";
1
+ import { WalletTypeEnum, FeePaidByEnum } from "./enum";
2
2
  import { NetworkEnum } from "../enum";
3
3
  import { ImageI } from "../qr/interface";
4
4
  import { ElectrumRawTransaction } from "../network/interface";
@@ -50,6 +50,7 @@ export interface SendRequestOptionsI {
50
50
  slpSemiAware?: boolean; // a flag which requires an utxo to have more than 546 sats to be spendable and counted in the balance
51
51
  queryBalance?: boolean;
52
52
  awaitTransactionPropagation?: boolean;
53
+ feePaidBy?: FeePaidByEnum;
53
54
  }
54
55
 
55
56
  export interface MnemonicI {