@bitpay-labs/crypto-wallet-core 11.7.0 → 11.7.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 BitPay, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitpay-labs/crypto-wallet-core",
3
- "version": "11.7.0",
3
+ "version": "11.7.2",
4
4
  "description": "A multi-currency support library for address derivation, private key creation, and transaction creation",
5
5
  "main": "./ts_build/src/index.js",
6
6
  "types": "./ts_build/src/index.d.ts",
@@ -45,10 +45,10 @@
45
45
  "author": "Micah Riggan",
46
46
  "license": "MIT",
47
47
  "dependencies": {
48
- "@bitpay-labs/bitcore-lib": "^11.7.0",
49
- "@bitpay-labs/bitcore-lib-cash": "^11.7.0",
50
- "@bitpay-labs/bitcore-lib-doge": "^11.7.0",
51
- "@bitpay-labs/bitcore-lib-ltc": "^11.7.0",
48
+ "@bitpay-labs/bitcore-lib": "^11.7.2",
49
+ "@bitpay-labs/bitcore-lib-cash": "^11.7.2",
50
+ "@bitpay-labs/bitcore-lib-doge": "^11.7.2",
51
+ "@bitpay-labs/bitcore-lib-ltc": "^11.7.2",
52
52
  "@solana-program/compute-budget": "0.7.0",
53
53
  "@solana-program/memo": "0.7.0",
54
54
  "@solana-program/system": "0.7.0",
@@ -70,5 +70,5 @@
70
70
  "nyc": "^17.1.0",
71
71
  "typescript": "5.7.3"
72
72
  },
73
- "gitHead": "6c5a1566aa725431db579f3c89a5c9f186bf5a30"
73
+ "gitHead": "d7f26cd2fae506cba9bb8965c5a196b13d8f0586"
74
74
  }
@@ -33,6 +33,35 @@ export abstract class AbstractBitcoreLibDeriver implements IDeriver {
33
33
  pubKey = new this.bitcoreLib.PublicKey(pubKey);
34
34
  return new this.bitcoreLib.Address(pubKey, network, addressType).toString();
35
35
  }
36
+
37
+ getPublicKey(network: string, privKey: Buffer): string {
38
+ if (!Buffer.isBuffer(privKey)) {
39
+ throw new Error('Expected privKey to be a Buffer');
40
+ }
41
+ // Force compressed pubkey (buffer does not encode compression flag)
42
+ const bn = this.bitcoreLib.crypto.BN.fromBuffer(privKey);
43
+ const key = new this.bitcoreLib.PrivateKey({ bn, network, compressed: true });
44
+ return key.publicKey.toString();
45
+ }
46
+
47
+ /**
48
+ * @returns {Buffer} raw secpk1 private key buffer (32 bytes, big-endian)
49
+ * @throws {Error} If privKey is not a Buffer (planned forwards compatibility) or string. Propagates all other errors
50
+ */
51
+ privateKeyToBuffer(privKey: Buffer | string): Buffer {
52
+ if (Buffer.isBuffer(privKey)) return privKey; // forward compatibility
53
+ if (typeof privKey !== 'string') throw new Error(`Expected key to be a string, got ${typeof privKey}`);
54
+
55
+ const key = new this.bitcoreLib.PrivateKey(privKey);
56
+ return key.toBuffer();
57
+ }
58
+
59
+ bufferToPrivateKey_TEMP(buf: Buffer, network: string): string {
60
+ // force compressed WIF without mutating instances
61
+ const bn = this.bitcoreLib.crypto.BN.fromBuffer(buf);
62
+ const key = new this.bitcoreLib.PrivateKey({ bn, network, compressed: true });
63
+ return key.toWIF();
64
+ }
36
65
  }
37
66
  export class BtcDeriver extends AbstractBitcoreLibDeriver {
38
67
  bitcoreLib = BitcoreLib;
@@ -55,4 +55,32 @@ export class EthDeriver implements IDeriver {
55
55
  pubKey = new BitcoreLib.PublicKey(pubKey, network); // network not needed here since ETH doesn't differentiate addresses by network.
56
56
  return this.addressFromPublicKeyBuffer(pubKey.toBuffer());
57
57
  }
58
+
59
+ getPublicKey(network: string, privKey: Buffer): string {
60
+ if (!Buffer.isBuffer(privKey)) {
61
+ throw new Error('Expected privKey to be a Buffer');
62
+ }
63
+ const bn = BitcoreLib.crypto.BN.fromBuffer(privKey);
64
+ const key = new BitcoreLib.PrivateKey({ bn, network, compressed: true });
65
+ return key.publicKey.toString('hex');
66
+ }
67
+
68
+ /**
69
+ * @param {Buffer | string} privKey - expects hex-encoded string, as returned from EthDeriver.derivePrivateKey
70
+ * @returns {Buffer}
71
+ * @throws {Error} If privKey is not a Buffer (planned forwards compatibility) or string. Propagates all other errors
72
+ */
73
+ privateKeyToBuffer(privKey: Buffer | string): Buffer {
74
+ if (Buffer.isBuffer(privKey)) return privKey;
75
+ if (typeof privKey !== 'string') throw new Error(`Expected string, got ${typeof privKey}`);
76
+ if (privKey.startsWith('0x')) {
77
+ privKey = privKey.slice(2);
78
+ };
79
+ // Expects to match return from derivePrivateKey's privKey.
80
+ return Buffer.from(privKey, 'hex');
81
+ }
82
+
83
+ bufferToPrivateKey_TEMP(buf: Buffer, _network: string): string {
84
+ return buf.toString('hex');
85
+ }
58
86
  }
@@ -105,6 +105,13 @@ export class DeriverProxy {
105
105
  return this.get(chain).getAddress(network, pubKey, addressType);
106
106
  }
107
107
 
108
+ /**
109
+ * Caller responsible for cleaning up privKey buffer
110
+ */
111
+ getPublicKey(chain, network, privKey: Buffer) {
112
+ return this.get(chain).getPublicKey(network, privKey);
113
+ }
114
+
108
115
  pathFor(chain, network, account = 0) {
109
116
  const normalizedChain = chain.toUpperCase();
110
117
  const accountStr = `${account}'`;
@@ -119,6 +126,14 @@ export class DeriverProxy {
119
126
  return Paths.BTC.default + accountStr;
120
127
  }
121
128
  }
129
+
130
+ privateKeyToBuffer(chain, privateKey: Buffer | string): Buffer {
131
+ return this.get(chain).privateKeyToBuffer(privateKey);
132
+ }
133
+
134
+ bufferToPrivateKey_TEMP(chain: string, network: string, buf: Buffer): string {
135
+ return this.get(chain).bufferToPrivateKey_TEMP(buf, network);
136
+ }
122
137
  }
123
138
 
124
139
  export default new DeriverProxy();
@@ -16,6 +16,14 @@ export class SolDeriver implements IDeriver {
16
16
  return this.addressFromPublicKeyBuffer(Buffer.from(pubKey, 'hex'));
17
17
  }
18
18
 
19
+ getPublicKey(_network: string, privKey: Buffer): string {
20
+ if (!Buffer.isBuffer(privKey)) {
21
+ throw new Error('Expected privKey to be a Buffer');
22
+ }
23
+ const pubKey = ed25519.getPublicKey(privKey, false);
24
+ return Buffer.from(pubKey).toString('hex');
25
+ }
26
+
19
27
  addressFromPublicKeyBuffer(pubKey: Buffer): string {
20
28
  if (pubKey.length > 32) {
21
29
  pubKey = pubKey.subarray(pubKey.length - 32);
@@ -54,4 +62,20 @@ export class SolDeriver implements IDeriver {
54
62
  pubKey: Buffer.from(pubKey).toString('hex')
55
63
  } as Key;
56
64
  };
65
+
66
+ /**
67
+ * @param {Buffer | string} privKey - expects base 58 encoded string
68
+ * @returns {Buffer}
69
+ * @throws {Error} If privKey is not a Buffer (planned forwards compatibility) or string. Propagates all other errors
70
+ */
71
+ privateKeyToBuffer(privKey: Buffer | string): Buffer {
72
+ if (Buffer.isBuffer(privKey)) return privKey;
73
+ if (typeof privKey !== 'string') throw new Error(`Expected string, got ${typeof privKey}`);
74
+ // Expects to match return from derivePrivateKey's privKey.
75
+ return encoding.Base58.decode(privKey);
76
+ }
77
+
78
+ bufferToPrivateKey_TEMP(buf: Buffer, _network: string): string {
79
+ return encoding.Base58.encode(buf);
80
+ }
57
81
  }
@@ -35,4 +35,29 @@ export class XrpDeriver implements IDeriver {
35
35
  const address = deriveAddress(pubKey);
36
36
  return address;
37
37
  }
38
+
39
+ getPublicKey(network: string, privKey: Buffer): string {
40
+ if (!Buffer.isBuffer(privKey)) {
41
+ throw new Error('Expected privKey to be a Buffer');
42
+ }
43
+ const bn = BitcoreLib.crypto.BN.fromBuffer(privKey);
44
+ const key = new BitcoreLib.PrivateKey({ bn, network, compressed: true });
45
+ return key.publicKey.toString('hex').toUpperCase();
46
+ }
47
+
48
+ /**
49
+ * @param {Buffer | string} privKey - expects hex-encoded string, as returned from XrpDeriver.derivePrivateKey privKey
50
+ * @returns {Buffer}
51
+ * @throws {Error} If privKey is not a Buffer (planned forwards compatibility) or string. Propagates all other errors
52
+ */
53
+ privateKeyToBuffer(privKey: Buffer | string): Buffer {
54
+ if (Buffer.isBuffer(privKey)) return privKey;
55
+ if (typeof privKey !== 'string') throw new Error(`Expected string, got ${typeof privKey}`);
56
+ // Expects to match return from derivePrivateKey's privKey.
57
+ return Buffer.from(privKey, 'hex');
58
+ }
59
+
60
+ bufferToPrivateKey_TEMP(buf: Buffer, _network: string): string {
61
+ return buf.toString('hex').toUpperCase();
62
+ }
38
63
  }
@@ -14,4 +14,21 @@ export interface IDeriver {
14
14
  derivePrivateKeyWithPath(network: string, xprivKey: string, path: string, addressType: string): Key;
15
15
 
16
16
  getAddress(network: string, pubKey, addressType: string): string;
17
+
18
+ /**
19
+ * Derive the public key for a given chain-native private key representation.
20
+ * Used when importing plaintext private keys that may not include `pubKey`.
21
+ * Caller should clean up buffer after use
22
+ */
23
+ getPublicKey(network: string, privKey: Buffer): string;
24
+
25
+ /**
26
+ * Used to normalize output of Key.privKey
27
+ */
28
+ privateKeyToBuffer(privKey: any): Buffer;
29
+
30
+ /**
31
+ * Temporary - converts decrypted private key buffer to lib-specific private key format
32
+ */
33
+ bufferToPrivateKey_TEMP(buf: Buffer, network: string): string;
17
34
  }
@@ -0,0 +1,126 @@
1
+ import { expect } from 'chai';
2
+ import crypto from 'crypto';
3
+ import { Deriver } from '../src';
4
+
5
+ describe('IDeriver', function () {
6
+ describe('getPublicKey (Buffer-first)', () => {
7
+ it('BTC: should derive the compressed secp256k1 public key', () => {
8
+ // Well-known secp256k1 test vector: private key 1 maps to generator point G.
9
+ const privHex = '0000000000000000000000000000000000000000000000000000000000000001';
10
+ const expectedPubKey = '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798';
11
+ const privBuf = Deriver.privateKeyToBuffer('BTC', privHex);
12
+ try {
13
+ const pubKey = Deriver.getPublicKey('BTC', 'testnet', privBuf);
14
+
15
+ expect(pubKey).to.equal(expectedPubKey);
16
+ expect(pubKey).to.match(/^(02|03)/);
17
+ expect(pubKey).to.have.length(66);
18
+ } finally {
19
+ privBuf.fill(0);
20
+ }
21
+ });
22
+
23
+ it('ETH: should derive the compressed pubKey used by wallet imports', () => {
24
+ // Vetted fixture from `test/address.test.ts`.
25
+ const privHex = '62b8311c71f355c5c07f6bffe9b1ae60aa20d90e2e2ec93ec11b6014b2ae6340';
26
+ const expectedPubKey = '0386d153aad9395924631dbc78fa560107123a759eaa3e105958248c60cd4472ad';
27
+ const expectedAddress = '0xb497281830dE4F19a3482AbF3D5C35c514e6fB36';
28
+ const privBuf = Deriver.privateKeyToBuffer('ETH', privHex);
29
+ try {
30
+ const pubKey = Deriver.getPublicKey('ETH', 'mainnet', privBuf);
31
+
32
+ expect(pubKey).to.equal(expectedPubKey);
33
+ expect(pubKey).to.match(/^(02|03)/);
34
+ expect(pubKey).to.have.length(66);
35
+ expect(Deriver.getAddress('ETH', 'mainnet', pubKey)).to.equal(expectedAddress);
36
+ } finally {
37
+ privBuf.fill(0);
38
+ }
39
+ });
40
+
41
+ it('XRP: should derive the compressed pubKey used by wallet imports', () => {
42
+ // Vetted fixture from `test/address.test.ts`.
43
+ const privHex = 'D02C6801D8F328FF2EAD51D01F9580AF36C8D74E2BD463963AC4ADBE51AE5F2C';
44
+ const expectedPubKey = '03DBEEC5E9E76DA09C5B502A67136BC2D73423E8902A7C35A8CBC0C5A6AC0469E8';
45
+ const expectedAddress = 'r9dmAJBfBe7JL2RRLiFWGJ8kM4CHEeTpgN';
46
+ const privBuf = Deriver.privateKeyToBuffer('XRP', privHex);
47
+ try {
48
+ const pubKey = Deriver.getPublicKey('XRP', 'mainnet', privBuf);
49
+
50
+ expect(pubKey).to.equal(expectedPubKey);
51
+ expect(pubKey).to.match(/^(02|03)/);
52
+ expect(pubKey).to.have.length(66);
53
+ expect(Deriver.getAddress('XRP', 'mainnet', pubKey)).to.equal(expectedAddress);
54
+ } finally {
55
+ privBuf.fill(0);
56
+ }
57
+ });
58
+
59
+ it('SOL: should derive the public key used by wallet imports', () => {
60
+ // Vetted fixture from `test/address.test.ts`.
61
+ const privKey = 'E4Tp4nTgMCa5dtGwqvkWoMGrJC7FKRNjcpeFFXi4nNb9';
62
+ const expectedPubKey = '5c9c85b20525ee81d3cc56da1f8307ec169086ae41458c5458519aced7683b66';
63
+ const expectedAddress = '7EWwMxKQa5Gru7oTcS1Wi3AaEgTfA6MU3z7MaLUT6hnD';
64
+ const privBuf = Deriver.privateKeyToBuffer('SOL', privKey);
65
+ try {
66
+ const pubKey = Deriver.getPublicKey('SOL', 'mainnet', privBuf);
67
+
68
+ expect(pubKey).to.equal(expectedPubKey);
69
+ expect(pubKey).to.have.length(64);
70
+ expect(Deriver.getAddress('SOL', 'mainnet', pubKey)).to.equal(expectedAddress);
71
+ } finally {
72
+ privBuf.fill(0);
73
+ }
74
+ });
75
+ });
76
+
77
+ describe('privateKeyToBuffer', function () {
78
+ it('ETH: should accept with/without 0x', () => {
79
+ const privHex = crypto.randomBytes(32).toString('hex');
80
+ const privBufNo0x = Deriver.privateKeyToBuffer('ETH', privHex);
81
+ const privBuf0x = Deriver.privateKeyToBuffer('ETH', `0x${privHex}`);
82
+ try {
83
+ expect(privBuf0x.equals(privBufNo0x)).to.be.true;
84
+ } finally {
85
+ privBufNo0x.fill(0);
86
+ privBuf0x.fill(0);
87
+ }
88
+ });
89
+ it('ETH: hex-stringified buffer output equals input', () => {
90
+ const privHex = crypto.randomBytes(32).toString('hex');
91
+ const privBuf = Deriver.privateKeyToBuffer('ETH', privHex);
92
+ try {
93
+ expect(privBuf.toString('hex')).to.equal(privHex);
94
+ } finally {
95
+ privBuf.fill(0);
96
+ }
97
+ });
98
+
99
+ it('XRP: should accept uppercase/lowercase hex', () => {
100
+ // Vetted fixture from `test/address.test.ts`.
101
+ const privHexUpper = 'D02C6801D8F328FF2EAD51D01F9580AF36C8D74E2BD463963AC4ADBE51AE5F2C';
102
+ const privHexLower = privHexUpper.toLowerCase();
103
+ const privBufUpper = Deriver.privateKeyToBuffer('XRP', privHexUpper);
104
+ const privBufLower = Deriver.privateKeyToBuffer('XRP', privHexLower);
105
+ try {
106
+ expect(privBufUpper.equals(privBufLower)).to.be.true;
107
+ } finally {
108
+ privBufUpper.fill(0);
109
+ privBufLower.fill(0);
110
+ }
111
+ });
112
+
113
+ it('XRP: hex-stringified buffer output equals fixture bytes', () => {
114
+ // Vetted fixture from `test/address.test.ts`.
115
+ const privHexUpper = 'D02C6801D8F328FF2EAD51D01F9580AF36C8D74E2BD463963AC4ADBE51AE5F2C';
116
+ const privBuf = Deriver.privateKeyToBuffer('XRP', privHexUpper);
117
+ try {
118
+ expect(privBuf.toString('hex')).to.equal(privHexUpper.toLowerCase());
119
+ } finally {
120
+ privBuf.fill(0);
121
+ }
122
+ });
123
+ });
124
+ });
125
+
126
+