@meshsdk/bitcoin 1.9.0-beta.50 → 1.9.0-beta.53

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.
package/dist/index.d.cts CHANGED
@@ -198,44 +198,49 @@ declare class BrowserWallet implements IBitcoinWallet {
198
198
  submitTx(signedTx: string): Promise<string>;
199
199
  }
200
200
 
201
- type CreateBitcoinEmbeddedWalletOptions = {
202
- networkId: 0 | 1;
201
+ type CreateWalletOptions = {
202
+ testnet: boolean;
203
203
  key: {
204
204
  type: "mnemonic";
205
205
  words: string[];
206
206
  };
207
+ path?: string;
207
208
  provider?: IBitcoinProvider;
208
209
  };
209
210
  /**
210
- * 0': Indicates the Bitcoin mainnet.
211
- * 1': Indicates the Bitcoin testnet.
212
- *
213
211
  * EmbeddedWallet is a class that provides a simple interface to interact with Bitcoin wallets.
214
- *
215
- * @params options - The options to create an EmbeddedWallet.
216
- * networkId - The network ID of the wallet.
217
- * key - The key to create the wallet.
218
- * provider - The Bitcoin provider to interact with the Bitcoin network.
219
212
  */
220
213
  declare class EmbeddedWallet {
221
- private readonly _networkId;
222
- private readonly _BIP32Interface;
223
- private readonly _p2wpkh;
214
+ private readonly _network;
215
+ private readonly _wallet;
224
216
  private readonly _provider?;
225
- constructor(options: CreateBitcoinEmbeddedWalletOptions);
217
+ constructor(options: CreateWalletOptions);
226
218
  /**
227
- * Get wallet network ID.
219
+ * Returns the wallet's SegWit (P2WPKH) address and associated public key.
228
220
  *
229
- * @returns network ID
221
+ * @returns {Address} The wallet address object including address, public key, and metadata.
222
+ * @throws {Error} If internal address or public key is not properly initialized.
223
+ */
224
+ getAddress(): Address;
225
+ /**
226
+ * Returns the hex-encoded public key of the wallet.
227
+ *
228
+ * @returns {string} The public key in hexadecimal format.
230
229
  */
231
- getNetworkId(): 0 | 1;
232
- getPaymentAddress(): Address;
233
230
  getPublicKey(): string;
231
+ /**
232
+ * Returns the network identifier of the wallet.
233
+ * 0': Indicates the Bitcoin mainnet.
234
+ * 1': Indicates the Bitcoin testnet.
235
+ *
236
+ * @returns {0 | 1} The Bitcoin network ID.
237
+ */
238
+ getNetworkId(): 0 | 1;
234
239
  /**
235
240
  * Get UTXOs for the wallet address.
236
241
  * @returns An array of UTXOs.
237
242
  */
238
- getUtxos(): Promise<UTxO[]>;
243
+ getUTxOs(): Promise<UTxO[]>;
239
244
  /**
240
245
  * Signs a given message using the wallet's private key.
241
246
  *
@@ -250,12 +255,13 @@ declare class EmbeddedWallet {
250
255
  */
251
256
  signTx(payload: bitcoin.Transaction): string;
252
257
  /**
253
- * Generate mnemonic or private key
258
+ * Generates a mnemonic phrase and returns it as an array of words.
254
259
  *
255
- * @param privateKey return private key if true
256
- * @returns a transaction hash
260
+ * @param {number} [strength=128] - The strength of the mnemonic in bits (must be a multiple of 32 between 128 and 256).
261
+ * @returns {string[]} An array of words representing the generated mnemonic.
262
+ * @throws {Error} If the strength is not valid.
257
263
  */
258
264
  static brew(strength?: number): string[];
259
265
  }
260
266
 
261
- export { type Address, BlockstreamProvider, BrowserWallet, type CreateBitcoinEmbeddedWalletOptions, ECPair, EmbeddedWallet, type IBitcoinWallet, type UTxO, bip32 };
267
+ export { type Address, BlockstreamProvider, BrowserWallet, type CreateWalletOptions, ECPair, EmbeddedWallet, type IBitcoinWallet, type UTxO, bip32 };
package/dist/index.d.ts CHANGED
@@ -198,44 +198,49 @@ declare class BrowserWallet implements IBitcoinWallet {
198
198
  submitTx(signedTx: string): Promise<string>;
199
199
  }
200
200
 
201
- type CreateBitcoinEmbeddedWalletOptions = {
202
- networkId: 0 | 1;
201
+ type CreateWalletOptions = {
202
+ testnet: boolean;
203
203
  key: {
204
204
  type: "mnemonic";
205
205
  words: string[];
206
206
  };
207
+ path?: string;
207
208
  provider?: IBitcoinProvider;
208
209
  };
209
210
  /**
210
- * 0': Indicates the Bitcoin mainnet.
211
- * 1': Indicates the Bitcoin testnet.
212
- *
213
211
  * EmbeddedWallet is a class that provides a simple interface to interact with Bitcoin wallets.
214
- *
215
- * @params options - The options to create an EmbeddedWallet.
216
- * networkId - The network ID of the wallet.
217
- * key - The key to create the wallet.
218
- * provider - The Bitcoin provider to interact with the Bitcoin network.
219
212
  */
220
213
  declare class EmbeddedWallet {
221
- private readonly _networkId;
222
- private readonly _BIP32Interface;
223
- private readonly _p2wpkh;
214
+ private readonly _network;
215
+ private readonly _wallet;
224
216
  private readonly _provider?;
225
- constructor(options: CreateBitcoinEmbeddedWalletOptions);
217
+ constructor(options: CreateWalletOptions);
226
218
  /**
227
- * Get wallet network ID.
219
+ * Returns the wallet's SegWit (P2WPKH) address and associated public key.
228
220
  *
229
- * @returns network ID
221
+ * @returns {Address} The wallet address object including address, public key, and metadata.
222
+ * @throws {Error} If internal address or public key is not properly initialized.
223
+ */
224
+ getAddress(): Address;
225
+ /**
226
+ * Returns the hex-encoded public key of the wallet.
227
+ *
228
+ * @returns {string} The public key in hexadecimal format.
230
229
  */
231
- getNetworkId(): 0 | 1;
232
- getPaymentAddress(): Address;
233
230
  getPublicKey(): string;
231
+ /**
232
+ * Returns the network identifier of the wallet.
233
+ * 0': Indicates the Bitcoin mainnet.
234
+ * 1': Indicates the Bitcoin testnet.
235
+ *
236
+ * @returns {0 | 1} The Bitcoin network ID.
237
+ */
238
+ getNetworkId(): 0 | 1;
234
239
  /**
235
240
  * Get UTXOs for the wallet address.
236
241
  * @returns An array of UTXOs.
237
242
  */
238
- getUtxos(): Promise<UTxO[]>;
243
+ getUTxOs(): Promise<UTxO[]>;
239
244
  /**
240
245
  * Signs a given message using the wallet's private key.
241
246
  *
@@ -250,12 +255,13 @@ declare class EmbeddedWallet {
250
255
  */
251
256
  signTx(payload: bitcoin.Transaction): string;
252
257
  /**
253
- * Generate mnemonic or private key
258
+ * Generates a mnemonic phrase and returns it as an array of words.
254
259
  *
255
- * @param privateKey return private key if true
256
- * @returns a transaction hash
260
+ * @param {number} [strength=128] - The strength of the mnemonic in bits (must be a multiple of 32 between 128 and 256).
261
+ * @returns {string[]} An array of words representing the generated mnemonic.
262
+ * @throws {Error} If the strength is not valid.
257
263
  */
258
264
  static brew(strength?: number): string[];
259
265
  }
260
266
 
261
- export { type Address, BlockstreamProvider, BrowserWallet, type CreateBitcoinEmbeddedWalletOptions, ECPair, EmbeddedWallet, type IBitcoinWallet, type UTxO, bip32 };
267
+ export { type Address, BlockstreamProvider, BrowserWallet, type CreateWalletOptions, ECPair, EmbeddedWallet, type IBitcoinWallet, type UTxO, bip32 };
package/dist/index.js CHANGED
@@ -3533,24 +3533,23 @@ var axios_default = axios;
3533
3533
  var Axios2 = axios_default.Axios, AxiosError2 = axios_default.AxiosError, CanceledError2 = axios_default.CanceledError, isCancel2 = axios_default.isCancel, CancelToken2 = axios_default.CancelToken, VERSION2 = axios_default.VERSION, all2 = axios_default.all, Cancel = axios_default.Cancel, isAxiosError2 = axios_default.isAxiosError, spread2 = axios_default.spread, toFormData2 = axios_default.toFormData, AxiosHeaders2 = axios_default.AxiosHeaders, HttpStatusCode2 = axios_default.HttpStatusCode, formToJSON = axios_default.formToJSON, getAdapter = axios_default.getAdapter, mergeConfig2 = axios_default.mergeConfig;
3534
3534
  // src/providers/common.ts
3535
3535
  var parseHttpError = function(error) {
3536
- if (axios_default.isAxiosError(error)) {
3537
- if (error.response) {
3538
- return JSON.stringify({
3539
- data: error.response.data,
3540
- headers: error.response.headers,
3541
- status: error.response.status
3542
- });
3543
- } else if (error.request && !_instanceof(error.request, XMLHttpRequest)) {
3544
- return JSON.stringify(error.request);
3545
- } else {
3546
- return JSON.stringify({
3547
- code: error.code,
3548
- message: error.message
3549
- });
3550
- }
3551
- } else {
3536
+ if (!axios_default.isAxiosError(error)) {
3552
3537
  return JSON.stringify(error);
3553
3538
  }
3539
+ if (error.response) {
3540
+ return JSON.stringify({
3541
+ data: error.response.data,
3542
+ headers: error.response.headers,
3543
+ status: error.response.status
3544
+ });
3545
+ }
3546
+ if (error.request && !_instanceof(error.request, XMLHttpRequest)) {
3547
+ return JSON.stringify(error.request);
3548
+ }
3549
+ return JSON.stringify({
3550
+ code: error.code,
3551
+ message: error.message
3552
+ });
3554
3553
  };
3555
3554
  // src/providers/blockstream.ts
3556
3555
  var BlockstreamProvider = /*#__PURE__*/ function() {
@@ -4235,58 +4234,68 @@ var WalletStaticMethods = /*#__PURE__*/ function() {
4235
4234
  return WalletStaticMethods;
4236
4235
  }();
4237
4236
  // src/wallets/embedded/index.ts
4237
+ import { mnemonicToSeedSync, validateMnemonic } from "bip39";
4238
4238
  var EmbeddedWallet = /*#__PURE__*/ function() {
4239
4239
  "use strict";
4240
4240
  function EmbeddedWallet(options) {
4241
4241
  _class_call_check(this, EmbeddedWallet);
4242
- this._networkId = options.networkId;
4243
- var bIP32Interface = WalletStaticMethods2.fromMnemonic(options.key.words.join(" "), options.networkId);
4244
- this._BIP32Interface = bIP32Interface;
4245
- this._p2wpkh = bitcoin.payments.p2wpkh({
4246
- pubkey: this._BIP32Interface.publicKey,
4247
- network: this._networkId === 0 ? bitcoin.networks.bitcoin : bitcoin.networks.testnet
4248
- });
4242
+ this._network = options.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
4243
+ var _options_path;
4244
+ this._wallet = _derive(options.key.words, (_options_path = options.path) !== null && _options_path !== void 0 ? _options_path : "m/84'/0'/0'/0/0", this._network);
4249
4245
  this._provider = options.provider;
4250
4246
  }
4251
4247
  _create_class(EmbeddedWallet, [
4252
4248
  {
4253
4249
  /**
4254
- * Get wallet network ID.
4250
+ * Returns the wallet's SegWit (P2WPKH) address and associated public key.
4255
4251
  *
4256
- * @returns network ID
4257
- */ key: "getNetworkId",
4258
- value: function getNetworkId() {
4259
- return this._networkId;
4260
- }
4261
- },
4262
- {
4263
- /*
4264
- * Get wallet address.
4265
- *
4266
- * @returns wallet address
4267
- */ key: "getPaymentAddress",
4268
- value: function getPaymentAddress() {
4269
- var address = {
4270
- address: this._p2wpkh.address,
4271
- publicKey: this._BIP32Interface.publicKey.toString("hex"),
4252
+ * @returns {Address} The wallet address object including address, public key, and metadata.
4253
+ * @throws {Error} If internal address or public key is not properly initialized.
4254
+ */ key: "getAddress",
4255
+ value: function getAddress() {
4256
+ var p2wpkh = bitcoin.payments.p2wpkh({
4257
+ pubkey: this._wallet.publicKey,
4258
+ network: this._network
4259
+ });
4260
+ if (!(p2wpkh === null || p2wpkh === void 0 ? void 0 : p2wpkh.address)) {
4261
+ throw new Error("Address is not initialized.");
4262
+ }
4263
+ return {
4264
+ address: p2wpkh.address,
4265
+ publicKey: this._wallet.publicKey.toString("hex"),
4272
4266
  purpose: "payment",
4273
4267
  addressType: "p2wpkh"
4274
4268
  };
4275
- return address;
4276
4269
  }
4277
4270
  },
4278
4271
  {
4279
- key: "getPublicKey",
4272
+ /**
4273
+ * Returns the hex-encoded public key of the wallet.
4274
+ *
4275
+ * @returns {string} The public key in hexadecimal format.
4276
+ */ key: "getPublicKey",
4280
4277
  value: function getPublicKey() {
4281
- return this._BIP32Interface.publicKey.toString("hex");
4278
+ return this._wallet.publicKey.toString("hex");
4279
+ }
4280
+ },
4281
+ {
4282
+ /**
4283
+ * Returns the network identifier of the wallet.
4284
+ * 0': Indicates the Bitcoin mainnet.
4285
+ * 1': Indicates the Bitcoin testnet.
4286
+ *
4287
+ * @returns {0 | 1} The Bitcoin network ID.
4288
+ */ key: "getNetworkId",
4289
+ value: function getNetworkId() {
4290
+ return this._network === bitcoin.networks.testnet ? 1 : 0;
4282
4291
  }
4283
4292
  },
4284
4293
  {
4285
- key: "getUtxos",
4294
+ key: "getUTxOs",
4286
4295
  value: /**
4287
4296
  * Get UTXOs for the wallet address.
4288
4297
  * @returns An array of UTXOs.
4289
- */ function getUtxos() {
4298
+ */ function getUTxOs() {
4290
4299
  var _this = this;
4291
4300
  return _async_to_generator(function() {
4292
4301
  var _this__provider, address;
@@ -4294,7 +4303,7 @@ var EmbeddedWallet = /*#__PURE__*/ function() {
4294
4303
  switch(_state.label){
4295
4304
  case 0:
4296
4305
  console.log("getUtxos");
4297
- address = _this.getPaymentAddress();
4306
+ address = _this.getAddress();
4298
4307
  if (_this._provider === void 0) {
4299
4308
  throw new Error("`provider` is not defined. Provide a BitcoinProvider.");
4300
4309
  }
@@ -4337,12 +4346,22 @@ var EmbeddedWallet = /*#__PURE__*/ function() {
4337
4346
  {
4338
4347
  key: "brew",
4339
4348
  value: /**
4340
- * Generate mnemonic or private key
4349
+ * Generates a mnemonic phrase and returns it as an array of words.
4341
4350
  *
4342
- * @param privateKey return private key if true
4343
- * @returns a transaction hash
4351
+ * @param {number} [strength=128] - The strength of the mnemonic in bits (must be a multiple of 32 between 128 and 256).
4352
+ * @returns {string[]} An array of words representing the generated mnemonic.
4353
+ * @throws {Error} If the strength is not valid.
4344
4354
  */ function brew() {
4345
4355
  var strength = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 128;
4356
+ if (![
4357
+ 128,
4358
+ 160,
4359
+ 192,
4360
+ 224,
4361
+ 256
4362
+ ].includes(strength)) {
4363
+ throw new Error("Invalid strength. Must be one of: 128, 160, 192, 224, 256.");
4364
+ }
4346
4365
  var mnemonic = bip39.generateMnemonic(strength);
4347
4366
  return mnemonic.split(" ");
4348
4367
  }
@@ -4350,34 +4369,16 @@ var EmbeddedWallet = /*#__PURE__*/ function() {
4350
4369
  ]);
4351
4370
  return EmbeddedWallet;
4352
4371
  }();
4353
- var WalletStaticMethods2 = /*#__PURE__*/ function() {
4354
- "use strict";
4355
- function _WalletStaticMethods() {
4356
- _class_call_check(this, _WalletStaticMethods);
4372
+ function _derive(words) {
4373
+ var path = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "m/84'/0'/0'/0/0", network = arguments.length > 2 ? arguments[2] : void 0;
4374
+ var mnemonic = words.join(" ");
4375
+ if (!validateMnemonic(mnemonic)) {
4376
+ throw new Error("Invalid mnemonic provided.");
4357
4377
  }
4358
- _create_class(_WalletStaticMethods, null, [
4359
- {
4360
- key: "fromMnemonic",
4361
- value: function fromMnemonic(mnemonic, networkId) {
4362
- var accountIndex = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 0, change = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 0, addressIndex = arguments.length > 4 && arguments[4] !== void 0 ? arguments[4] : 0;
4363
- var seed = bip39.mnemonicToSeedSync(mnemonic);
4364
- var root = bip32.fromSeed(seed, _WalletStaticMethods.getNetwork(networkId));
4365
- var purpose = 84;
4366
- var coinType = networkId;
4367
- var path = "m/".concat(purpose, "'/").concat(coinType, "'/").concat(accountIndex, "'/").concat(change, "/").concat(addressIndex);
4368
- var child = root.derivePath(path);
4369
- return child;
4370
- }
4371
- },
4372
- {
4373
- key: "getNetwork",
4374
- value: // todo: this is the confusing part in our sdk, since cardano 0 is testnet. we need to use enums or string instead for clearer
4375
- function getNetwork(network) {
4376
- return network === 0 ? bitcoin.networks.bitcoin : bitcoin.networks.testnet;
4377
- }
4378
- }
4379
- ]);
4380
- return _WalletStaticMethods;
4381
- }();
4378
+ var seed = mnemonicToSeedSync(mnemonic);
4379
+ var root = bip32.fromSeed(seed, network);
4380
+ var child = root.derivePath(path);
4381
+ return child;
4382
+ }
4382
4383
  export { BlockstreamProvider, BrowserWallet, ECPair, EmbeddedWallet, bip32, bip39, bitcoin };
4383
4384
  //# sourceMappingURL=index.js.map