Skip to content

Commit ddaf76f

Browse files
committed
feat: implement auto coin select
Adds and implements auto coin select functionality. Bumps version to 0.0.52. Adds coinSelectPreference to example in README.md.
1 parent 77308a7 commit ddaf76f

File tree

10 files changed

+615
-59
lines changed

10 files changed

+615
-59
lines changed

README.md

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import { Wallet, generateMnemonic } from 'beignet';
102102
import net from 'net'
103103
import tls from 'tls'
104104
import { TStorage } from './wallet';
105+
import { ECoinSelectPreference } from "./transaction";
105106

106107
// Generate a mnemonic phrase
107108
const mnemonic = generateMnemonic();
@@ -111,10 +112,10 @@ const passphrase = 'passphrase';
111112

112113
// Connect to custom electrum server
113114
const servers: TServer = {
114-
host: '35.233.47.252',
115-
ssl: 18484,
116-
tcp: 18483,
117-
protocol: EProtocol.ssl,
115+
host: '35.233.47.252',
116+
ssl: 18484,
117+
tcp: 18483,
118+
protocol: EProtocol.ssl,
118119
};
119120

120121
// Use a specific network (Defaults to mainnet)
@@ -128,43 +129,47 @@ const addressTypesToMonitor = [EAddressType.p2tr, EAddressType.p2wpkh];
128129

129130
// Subscribe to server messages (TOnMessage)
130131
const onMessage: TOnMessage = (id, data) => {
131-
console.log(id);
132-
console.dir(data, { depth: null });
132+
console.log(id);
133+
console.dir(data, { depth: null });
133134
}
134135

135136
// Disable startup messages. Messages resume once startup is complete. (Defaults to false)
136137
const disableMessagesOnCreate = true;
137138

138139
// Persist sessions by getting and setting data from storage
139140
const storage: TStorage = {
140-
async getData<K extends keyof IWalletData>(
141-
key: string
142-
): Promise<Result<IWalletData[K]>> {
143-
// Add your logic here
144-
},
145-
async setData<K extends keyof IWalletData>(
146-
key: string,
147-
value: IWalletData[K]
148-
): Promise<Result<boolean>> {
149-
// Add your logic here
150-
}
141+
async getData<K extends keyof IWalletData>(
142+
key: string
143+
): Promise<Result<IWalletData[K]>> {
144+
// Add your logic here
145+
},
146+
async setData<K extends keyof IWalletData>(
147+
key: string,
148+
value: IWalletData[K]
149+
): Promise<Result<boolean>> {
150+
// Add your logic here
151+
}
151152
};
152153

154+
// Set the auto coin selection preference. (Defaults to ECoinSelectPreference.consolidate)
155+
const coinSelectPreference = ECoinSelectPreference.small;
156+
153157
// Create a wallet instance
154158
const createWalletRes = await Wallet.create({
155-
mnemonic,
156-
passphrase,
157-
electrumOptions: {
158-
servers,
159-
net,
160-
tls
161-
},
162-
network,
163-
onMessage,
164-
storage,
165-
addressType,
166-
addressTypesToMonitor,
167-
disableMessagesOnCreate
159+
mnemonic,
160+
passphrase,
161+
electrumOptions: {
162+
servers,
163+
net,
164+
tls
165+
},
166+
network,
167+
onMessage,
168+
storage,
169+
addressType,
170+
addressTypesToMonitor,
171+
disableMessagesOnCreate,
172+
coinSelectPreference
168173
});
169174
if (createWalletRes.isErr()) return;
170175
const wallet = createWalletRes.value;
@@ -174,18 +179,18 @@ const utxos = wallet.listUtxos();
174179

175180
// Send sats to multiple outputs
176181
const txs = [
177-
{ address: 'address1', amount: 1000 },
178-
{ address: 'address2', amount: 2000 },
179-
{ address: 'address3', amount: 3000 },
182+
{ address: 'address1', amount: 1000 },
183+
{ address: 'address2', amount: 2000 },
184+
{ address: 'address3', amount: 3000 },
180185
];
181186
const sendManyRes = await wallet.sendMany({ txs });
182187

183188
// Sweep from a private key
184189
const sweepPrivateKeyRes = await wallet.sweepPrivateKey({
185-
privateKey: 'privateKey',
186-
toAddress: 'toAddress',
187-
satsPerByte: 5,
188-
broadcast: false
190+
privateKey: 'privateKey',
191+
toAddress: 'toAddress',
192+
satsPerByte: 5,
193+
broadcast: false
189194
});
190195

191196
// Get tx history for a given address. { tx_hash: string; height: number; }[]

example/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const servers = {
7777
host: '35.233.47.252',
7878
ssl: 18484,
7979
tcp: 18483,
80-
protocol: EProtocol.ssl
80+
protocol: EProtocol.tcp
8181
}
8282
]
8383
};

example/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { EAvailableNetworks, generateMnemonic, Wallet } from '../src';
1+
import {
2+
EAvailableNetworks,
3+
ECoinSelectPreference,
4+
generateMnemonic,
5+
Wallet
6+
} from '../src';
27
import { getData, onMessage, servers, setData } from './helpers';
38
import * as repl from 'repl';
49
import net from 'net';
@@ -26,7 +31,8 @@ const runExample = async (mnemonic = generateMnemonic()): Promise<void> => {
2631
lookBehind: 5,
2732
lookAheadChange: 5,
2833
lookBehindChange: 5
29-
}
34+
},
35+
coinSelectPreference: ECoinSelectPreference.small
3036
});
3137
if (createWalletResponse.isErr()) return;
3238
const wallet = createWalletResponse.value;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "beignet",
3-
"version": "0.0.51",
3+
"version": "0.0.52",
44
"description": "A self-custodial, JS Bitcoin wallet management library.",
55
"main": "dist/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)