@chainflip/bitcoin 1.0.1 → 1.0.2

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.cjs CHANGED
@@ -40,7 +40,10 @@ function parseBase58Address(address, network) {
40
40
  }
41
41
  function encodeBase58Address(data, network, type) {
42
42
  const version = (type === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[network];
43
- const payload = new Uint8Array([version, ..._bytes.hexToBytes.call(void 0, data)]);
43
+ const payload = new Uint8Array([
44
+ version,
45
+ ...typeof data === "string" ? _bytes.hexToBytes.call(void 0, data) : data
46
+ ]);
44
47
  const checksum = _sha256.sha256.call(void 0, _sha256.sha256.call(void 0, payload)).slice(0, 4);
45
48
  const address = base58.encode(Buffer.concat([payload, checksum]));
46
49
  return address;
@@ -168,9 +171,9 @@ var segwitVersions = {
168
171
  P2WSH: 0,
169
172
  Taproot: 1
170
173
  };
171
- function encodeSegwitAddress(hexData, kind, network) {
174
+ function encodeSegwitAddress(byteData, kind, network) {
172
175
  const variant = kind === "Taproot" ? "bech32m" : "bech32";
173
- const bytes = _bytes.hexToBytes.call(void 0, hexData);
176
+ const bytes = typeof byteData === "string" ? _bytes.hexToBytes.call(void 0, byteData) : new Uint8Array(byteData);
174
177
  const version = segwitVersions[kind];
175
178
  _assertion.assert.call(void 0, bytes.length >= 2 && bytes.length <= 40, "Invalid address");
176
179
  _assertion.assert.call(void 0, version !== 0 || bytes.length === 20 || bytes.length === 32, "Invalid address");
@@ -191,7 +194,10 @@ var encodeAddress = (data, kind, cfOrBtcnetwork) => {
191
194
  const btcNetwork = networkMap[cfOrBtcnetwork];
192
195
  _assertion.assert.call(void 0, btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
193
196
  _assertion.assert.call(void 0, data.length % 2 === 0, "bytes must have an even number of characters");
194
- _assertion.assert.call(void 0, /^(0x)?[0-9a-f]*$/.test(data), "bytes must be hex-encoded with a 0x prefix");
197
+ _assertion.assert.call(void 0,
198
+ typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data),
199
+ "bytes are not a valid hex string"
200
+ );
195
201
  switch (kind) {
196
202
  case "P2PKH":
197
203
  case "P2SH":
package/dist/index.d.cts CHANGED
@@ -1,7 +1,8 @@
1
1
  type ChainflipNetwork = 'mainnet' | 'perseverance' | 'sisyphos' | 'backspin';
2
2
  type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
3
+ type Bytelike = Uint8Array | number[] | `0x${string}`;
3
4
  type AddressType = 'P2WPKH' | 'P2SH' | 'P2PKH' | 'P2WSH' | 'Taproot';
4
- declare const encodeAddress: (data: `0x${string}`, kind: AddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
5
+ declare const encodeAddress: (data: Bytelike, kind: AddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
5
6
  declare const isValidAddressForNetwork: (address: string, network: BitcoinNetwork) => boolean;
6
7
 
7
8
  export { encodeAddress, isValidAddressForNetwork };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  type ChainflipNetwork = 'mainnet' | 'perseverance' | 'sisyphos' | 'backspin';
2
2
  type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
3
+ type Bytelike = Uint8Array | number[] | `0x${string}`;
3
4
  type AddressType = 'P2WPKH' | 'P2SH' | 'P2PKH' | 'P2WSH' | 'Taproot';
4
- declare const encodeAddress: (data: `0x${string}`, kind: AddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
5
+ declare const encodeAddress: (data: Bytelike, kind: AddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
5
6
  declare const isValidAddressForNetwork: (address: string, network: BitcoinNetwork) => boolean;
6
7
 
7
8
  export { encodeAddress, isValidAddressForNetwork };
package/dist/index.mjs CHANGED
@@ -40,7 +40,10 @@ function parseBase58Address(address, network) {
40
40
  }
41
41
  function encodeBase58Address(data, network, type) {
42
42
  const version = (type === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[network];
43
- const payload = new Uint8Array([version, ...hexToBytes(data)]);
43
+ const payload = new Uint8Array([
44
+ version,
45
+ ...typeof data === "string" ? hexToBytes(data) : data
46
+ ]);
44
47
  const checksum = sha256(sha256(payload)).slice(0, 4);
45
48
  const address = base58.encode(Buffer.concat([payload, checksum]));
46
49
  return address;
@@ -168,9 +171,9 @@ var segwitVersions = {
168
171
  P2WSH: 0,
169
172
  Taproot: 1
170
173
  };
171
- function encodeSegwitAddress(hexData, kind, network) {
174
+ function encodeSegwitAddress(byteData, kind, network) {
172
175
  const variant = kind === "Taproot" ? "bech32m" : "bech32";
173
- const bytes = hexToBytes(hexData);
176
+ const bytes = typeof byteData === "string" ? hexToBytes(byteData) : new Uint8Array(byteData);
174
177
  const version = segwitVersions[kind];
175
178
  assert(bytes.length >= 2 && bytes.length <= 40, "Invalid address");
176
179
  assert(version !== 0 || bytes.length === 20 || bytes.length === 32, "Invalid address");
@@ -191,7 +194,10 @@ var encodeAddress = (data, kind, cfOrBtcnetwork) => {
191
194
  const btcNetwork = networkMap[cfOrBtcnetwork];
192
195
  assert(btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
193
196
  assert(data.length % 2 === 0, "bytes must have an even number of characters");
194
- assert(/^(0x)?[0-9a-f]*$/.test(data), "bytes must be hex-encoded with a 0x prefix");
197
+ assert(
198
+ typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data),
199
+ "bytes are not a valid hex string"
200
+ );
195
201
  switch (kind) {
196
202
  case "P2PKH":
197
203
  case "P2SH":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainflip/bitcoin",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "repository": "https://github.com/chainflip-io/chainflip-product-toolkit.git",
6
6
  "publishConfig": {