edge-currency-plugins 1.2.0 → 1.2.1

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.
@@ -32,6 +32,11 @@ var _ProcessorTransaction = require('../db/Models/ProcessorTransaction');
32
32
 
33
33
 
34
34
  var _cleaners3 = require('../keymanager/cleaners');
35
+
36
+
37
+
38
+
39
+
35
40
  var _keymanager = require('../keymanager/keymanager');
36
41
  var _makeUtxoEngineState = require('./makeUtxoEngineState');
37
42
  var _makeUtxoWalletTools = require('./makeUtxoWalletTools');
@@ -514,10 +519,12 @@ var _utils = require('./utils');
514
519
 
515
520
  // Use the privateKeys (WIFs) from spendInfo, otherwise get them from the
516
521
  // PBST inputs.
517
- const privateKeys = await (async () => {
522
+ const privateKeyEncodings = await (async (
523
+
524
+ ) => {
518
525
  if (edgeSpendInfo.privateKeys != null) {
519
526
  return edgeSpendInfo.privateKeys.map(wif =>
520
- walletTools.getPrivateKeyFromWif(wif)
527
+ walletTools.getPrivateKeyEncodingFromWif(wif)
521
528
  )
522
529
  } else {
523
530
  return await Promise.all(
@@ -533,10 +540,14 @@ var _utils = require('./utils');
533
540
  if (_optionalChain([address, 'optionalAccess', _15 => _15.path]) == null)
534
541
  throw new Error('Invalid script pubkey')
535
542
 
536
- return walletTools.getPrivateKey({
543
+ const privateKey = walletTools.getPrivateKey({
537
544
  path: address.path,
538
545
  xprivKeys
539
546
  })
547
+ return {
548
+ hex: privateKey,
549
+ compressed: true // We shouldn't ever be storing/deriving uncompressed keys
550
+ }
540
551
  })
541
552
  )
542
553
  }
@@ -544,7 +555,7 @@ var _utils = require('./utils');
544
555
  const signedTx = await _keymanager.signTx.call(void 0, {
545
556
  psbtBase64: psbt.base64,
546
557
  coin: coinInfo.name,
547
- privateKeys
558
+ privateKeyEncodings: privateKeyEncodings
548
559
  })
549
560
  transaction.txid = signedTx.id
550
561
  transaction.signedTx = signedTx.hex
@@ -11,6 +11,8 @@
11
11
 
12
12
 
13
13
 
14
+
15
+
14
16
  var _keymanager = require('../keymanager/keymanager');
15
17
 
16
18
 
@@ -125,12 +127,24 @@ var _utils = require('./utils');
125
127
  ) {
126
128
  const purposeType = _utils.currencyFormatToPurposeType.call(void 0, format)
127
129
  const scriptType = _utils.getScriptTypeFromPurposeType.call(void 0, purposeType)
128
- const pubkey = _keymanager.privateKeyToPubkey.call(void 0,
129
- _keymanager.wifToPrivateKey.call(void 0, {
130
- wifKey,
131
- coin
132
- })
133
- )
130
+ const privateKeyEncoding = _keymanager.wifToPrivateKeyEncoding.call(void 0, {
131
+ wifKey,
132
+ coin
133
+ })
134
+
135
+ // Restrict only compressed keys for segwit pubkeys as per BIP143
136
+ // (see https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#restrictions-on-public-key-type)
137
+ if (
138
+ [
139
+ _keymanager.BIP43PurposeTypeEnum.Segwit,
140
+ _keymanager.BIP43PurposeTypeEnum.WrappedSegwit
141
+ ].includes(purposeType) &&
142
+ !privateKeyEncoding.compressed
143
+ ) {
144
+ privateKeyEncoding.compressed = true
145
+ }
146
+
147
+ const pubkey = _keymanager.privateKeyEncodingToPubkey.call(void 0, privateKeyEncoding)
134
148
  return _keymanager.pubkeyToScriptPubkey.call(void 0, {
135
149
  pubkey: pubkey,
136
150
  scriptType
@@ -179,8 +193,8 @@ var _utils = require('./utils');
179
193
  })
180
194
  },
181
195
 
182
- getPrivateKeyFromWif(wifKey) {
183
- return _keymanager.wifToPrivateKey.call(void 0, {
196
+ getPrivateKeyEncodingFromWif(wifKey) {
197
+ return _keymanager.wifToPrivateKeyEncoding.call(void 0, {
184
198
  wifKey,
185
199
  coin
186
200
  })
@@ -221,7 +221,7 @@ var _keymanager = require('../keymanager/keymanager');
221
221
 
222
222
  // Check if the pathname type is a wif
223
223
  try {
224
- _keymanager.wifToPrivateKey.call(void 0, {
224
+ _keymanager.wifToPrivateKeyEncoding.call(void 0, {
225
225
  wifKey: args.pathname,
226
226
  coin: args.coin
227
227
  })
@@ -230,6 +230,11 @@ var VerifyAddressEnum; (function (VerifyAddressEnum) {
230
230
 
231
231
 
232
232
 
233
+
234
+
235
+
236
+
237
+
233
238
 
234
239
 
235
240
 
@@ -785,7 +790,7 @@ const xprivToPrivateKeyInternal = (
785
790
  }).toWIF(coinClass.wifEncodeFunc)
786
791
  } exports.privateKeyToWIF = privateKeyToWIF;
787
792
 
788
- const wifToPrivateKeyInternal = (
793
+ const wifToPrivateKeyEncodingInternal = (
789
794
  prefixIndex,
790
795
  args
791
796
  ) => {
@@ -795,29 +800,39 @@ const wifToPrivateKeyInternal = (
795
800
  forWIF: true,
796
801
  prefixIndex
797
802
  })
798
- return _optionalChain([bitcoin, 'access', _10 => _10.ECPair, 'access', _11 => _11.fromWIF, 'call', _12 => _12(
803
+ const ecPair = bitcoin.ECPair.fromWIF(
799
804
  args.wifKey,
800
805
  network,
801
806
  coin.bs58DecodeFunc
802
- ), 'access', _13 => _13.privateKey, 'optionalAccess', _14 => _14.toString, 'call', _15 => _15('hex')])
807
+ )
808
+ if (ecPair.privateKey == null) return
809
+ return {
810
+ hex: ecPair.privateKey.toString('hex'),
811
+ compressed: ecPair.compressed
812
+ }
803
813
  }
804
- const wifToPrivateKey = (args) => {
805
- const privateKey = filterCoinPrefixes(
814
+ const wifToPrivateKeyEncoding = (
815
+ args
816
+ ) => {
817
+ const privateKeyEncoding = filterCoinPrefixes(
806
818
  args.coin,
807
- prefixIndex => wifToPrivateKeyInternal(prefixIndex, args),
819
+ prefixIndex => wifToPrivateKeyEncodingInternal(prefixIndex, args),
808
820
  ['Invalid network version']
809
821
  )
810
- if (privateKey == null) {
822
+ if (privateKeyEncoding == null) {
811
823
  throw new Error('Failed to convert WIF key to private key')
812
824
  }
813
- return privateKey
814
- }; exports.wifToPrivateKey = wifToPrivateKey
825
+ return privateKeyEncoding
826
+ }; exports.wifToPrivateKeyEncoding = wifToPrivateKeyEncoding
815
827
 
816
- function privateKeyToPubkey(privateKey) {
817
- return bitcoin.ECPair.fromPrivateKey(
818
- Buffer.from(privateKey, 'hex')
819
- ).publicKey.toString('hex')
820
- } exports.privateKeyToPubkey = privateKeyToPubkey;
828
+ function privateKeyEncodingToPubkey(
829
+ privateKeyEncoding
830
+ ) {
831
+ const { hex, compressed } = privateKeyEncoding
832
+ return bitcoin.ECPair.fromPrivateKey(Buffer.from(hex, 'hex'), {
833
+ compressed
834
+ }).publicKey.toString('hex')
835
+ } exports.privateKeyEncodingToPubkey = privateKeyEncodingToPubkey;
821
836
 
822
837
  function signMessageBase64(message, privateKey) {
823
838
  const keyPair = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'))
@@ -977,7 +992,7 @@ const wifToPrivateKeyInternal = (
977
992
  psbt.addInputs(sortedInputs)
978
993
  psbt.addOutputs(sortedOutputs)
979
994
  } catch (error) {
980
- _optionalChain([log, 'optionalAccess', _16 => _16.error, 'call', _17 => _17(`Failed makeTx: ${String(error)}`)])
995
+ _optionalChain([log, 'optionalAccess', _10 => _10.error, 'call', _11 => _11(`Failed makeTx: ${String(error)}`)])
981
996
  throw error
982
997
  }
983
998
 
@@ -987,7 +1002,7 @@ const wifToPrivateKeyInternal = (
987
1002
  changeUsed: result.changeUsed,
988
1003
  fee: result.fee,
989
1004
  psbtBase64: psbt.toBase64(),
990
- hex: _optionalChain([psbt, 'access', _18 => _18.data, 'optionalAccess', _19 => _19.globalMap, 'optionalAccess', _20 => _20.unsignedTx, 'optionalAccess', _21 => _21.toBuffer, 'call', _22 => _22(), 'optionalAccess', _23 => _23.toString, 'call', _24 => _24('hex')])
1005
+ hex: _optionalChain([psbt, 'access', _12 => _12.data, 'optionalAccess', _13 => _13.globalMap, 'optionalAccess', _14 => _14.unsignedTx, 'optionalAccess', _15 => _15.toBuffer, 'call', _16 => _16(), 'optionalAccess', _17 => _17.toString, 'call', _18 => _18('hex')])
991
1006
  }
992
1007
  } exports.makeTx = makeTx;
993
1008
 
@@ -996,13 +1011,13 @@ const wifToPrivateKeyInternal = (
996
1011
  const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
997
1012
 
998
1013
  for (let i = 0; i < psbt.inputCount; i++) {
999
- const privateKey = Buffer.from(
1000
- _nullishCoalesce(args.privateKeys[i], () => ( args.privateKeys[args.privateKeys.length - 1])),
1001
- 'hex'
1002
- )
1014
+ const privateKeyEncoding =
1015
+ _nullishCoalesce(args.privateKeyEncodings[i], () => (
1016
+ args.privateKeyEncodings[args.privateKeyEncodings.length - 1]))
1017
+ const { hex, compressed } = privateKeyEncoding
1003
1018
  psbt.signInput(
1004
1019
  i,
1005
- bitcoin.ECPair.fromPrivateKey(privateKey),
1020
+ bitcoin.ECPair.fromPrivateKey(Buffer.from(hex, 'hex'), { compressed }),
1006
1021
  bitcoin.Psbt.DEFAULT_SIGHASHES,
1007
1022
  coin.sighashFunction
1008
1023
  )
@@ -1,6 +1,7 @@
1
1
  import { AddressPath, CurrencyFormat } from '../../plugin/types';
2
2
  import { ScriptTemplate } from '../info/scriptTemplates/types';
3
3
  import { PublicKey } from '../keymanager/cleaners';
4
+ import { PrivateKeyEncoding } from '../keymanager/keymanager';
4
5
  import { CurrencyFormatKeys } from './utils';
5
6
  export interface WalletToolsConfig {
6
7
  publicKey: PublicKey;
@@ -14,7 +15,7 @@ export interface UTXOPluginWalletTools {
14
15
  addressToScriptPubkey: (address: string) => string;
15
16
  scriptPubkeyToAddress: (args: ScriptPubkeyToAddressArgs) => AddressReturn;
16
17
  getPrivateKey: (args: GetPrivateKeyArgs) => string;
17
- getPrivateKeyFromWif: (wif: string) => string;
18
+ getPrivateKeyEncodingFromWif: (wif: string) => PrivateKeyEncoding;
18
19
  getScriptAddress: (args: GetScriptAddressArgs) => GetScriptAddressReturn;
19
20
  signMessageBase64: (args: SignMessageArgs) => string;
20
21
  }
@@ -101,10 +101,14 @@ export interface ScriptPubkeyToP2SHReturn {
101
101
  redeemScript: string;
102
102
  address?: string;
103
103
  }
104
- export interface WIFToPrivateKeyArgs {
104
+ export interface WIFToPrivateKeyEncodingArgs {
105
105
  wifKey: string;
106
106
  coin: string;
107
107
  }
108
+ export interface PrivateKeyEncoding {
109
+ hex: string;
110
+ compressed: boolean;
111
+ }
108
112
  export interface PrivateKeyToWIFArgs {
109
113
  privateKey: string;
110
114
  coin: string;
@@ -145,7 +149,7 @@ interface MakeTxReturn extends Required<utxopicker.UtxoPickerResult> {
145
149
  psbtBase64: string;
146
150
  }
147
151
  export interface SignTxArgs {
148
- privateKeys: string[];
152
+ privateKeyEncodings: PrivateKeyEncoding[];
149
153
  psbtBase64: string;
150
154
  coin: string;
151
155
  }
@@ -195,8 +199,8 @@ export declare function pubkeyToScriptPubkey(args: PubkeyToScriptPubkeyArgs): Pu
195
199
  export declare const toNewFormat: (address: string, coinName: string) => string;
196
200
  export declare const xprivToPrivateKey: (args: XPrivToPrivateKeyArgs) => string;
197
201
  export declare function privateKeyToWIF(args: PrivateKeyToWIFArgs): string;
198
- export declare const wifToPrivateKey: (args: WIFToPrivateKeyArgs) => string;
199
- export declare function privateKeyToPubkey(privateKey: string): string;
202
+ export declare const wifToPrivateKeyEncoding: (args: WIFToPrivateKeyEncodingArgs) => PrivateKeyEncoding;
203
+ export declare function privateKeyEncodingToPubkey(privateKeyEncoding: PrivateKeyEncoding): string;
200
204
  export declare function signMessageBase64(message: string, privateKey: string): string;
201
205
  export declare function makeTx(args: MakeTxArgs): MakeTxReturn;
202
206
  export declare function signTx(args: SignTxArgs): Promise<SignTxReturn>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "edge-currency-plugins",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Edge currency plugins to fetch and process wallet accounts, balances and transactions.",
5
5
  "repository": "https://github.com/EdgeApp/edge-currency-plugins",
6
6
  "license": "MIT",