edge-currency-plugins 1.1.6 → 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');
@@ -275,6 +280,8 @@ var _utils = require('./utils');
275
280
  options
276
281
  ) {
277
282
  const { spendTargets } = edgeSpendInfo
283
+ const { outputSort = 'bip69' } = _nullishCoalesce(edgeSpendInfo.otherParams, () => ( {}))
284
+
278
285
  if (_optionalChain([options, 'optionalAccess', _5 => _5.CPFP]) == null && spendTargets.length < 1) {
279
286
  throw new Error('Need to provide Spend Targets')
280
287
  }
@@ -397,7 +404,8 @@ var _utils = require('./utils');
397
404
  setRBF,
398
405
  freshChangeAddress,
399
406
  subtractFee,
400
- log
407
+ log,
408
+ outputSort
401
409
  })
402
410
  if (tx.changeUsed) {
403
411
  ourReceiveAddresses.push(freshChangeAddress)
@@ -511,10 +519,12 @@ var _utils = require('./utils');
511
519
 
512
520
  // Use the privateKeys (WIFs) from spendInfo, otherwise get them from the
513
521
  // PBST inputs.
514
- const privateKeys = await (async () => {
522
+ const privateKeyEncodings = await (async (
523
+
524
+ ) => {
515
525
  if (edgeSpendInfo.privateKeys != null) {
516
526
  return edgeSpendInfo.privateKeys.map(wif =>
517
- walletTools.getPrivateKeyFromWif(wif)
527
+ walletTools.getPrivateKeyEncodingFromWif(wif)
518
528
  )
519
529
  } else {
520
530
  return await Promise.all(
@@ -530,10 +540,14 @@ var _utils = require('./utils');
530
540
  if (_optionalChain([address, 'optionalAccess', _15 => _15.path]) == null)
531
541
  throw new Error('Invalid script pubkey')
532
542
 
533
- return walletTools.getPrivateKey({
543
+ const privateKey = walletTools.getPrivateKey({
534
544
  path: address.path,
535
545
  xprivKeys
536
546
  })
547
+ return {
548
+ hex: privateKey,
549
+ compressed: true // We shouldn't ever be storing/deriving uncompressed keys
550
+ }
537
551
  })
538
552
  )
539
553
  }
@@ -541,7 +555,7 @@ var _utils = require('./utils');
541
555
  const signedTx = await _keymanager.signTx.call(void 0, {
542
556
  psbtBase64: psbt.base64,
543
557
  coin: coinInfo.name,
544
- privateKeys
558
+ privateKeyEncodings: privateKeyEncodings
545
559
  })
546
560
  transaction.txid = signedTx.id
547
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
  })
@@ -228,6 +228,12 @@ var VerifyAddressEnum; (function (VerifyAddressEnum) {
228
228
 
229
229
 
230
230
 
231
+
232
+
233
+
234
+
235
+
236
+
231
237
 
232
238
 
233
239
 
@@ -784,7 +790,7 @@ const xprivToPrivateKeyInternal = (
784
790
  }).toWIF(coinClass.wifEncodeFunc)
785
791
  } exports.privateKeyToWIF = privateKeyToWIF;
786
792
 
787
- const wifToPrivateKeyInternal = (
793
+ const wifToPrivateKeyEncodingInternal = (
788
794
  prefixIndex,
789
795
  args
790
796
  ) => {
@@ -794,29 +800,39 @@ const wifToPrivateKeyInternal = (
794
800
  forWIF: true,
795
801
  prefixIndex
796
802
  })
797
- return _optionalChain([bitcoin, 'access', _10 => _10.ECPair, 'access', _11 => _11.fromWIF, 'call', _12 => _12(
803
+ const ecPair = bitcoin.ECPair.fromWIF(
798
804
  args.wifKey,
799
805
  network,
800
806
  coin.bs58DecodeFunc
801
- ), '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
+ }
802
813
  }
803
- const wifToPrivateKey = (args) => {
804
- const privateKey = filterCoinPrefixes(
814
+ const wifToPrivateKeyEncoding = (
815
+ args
816
+ ) => {
817
+ const privateKeyEncoding = filterCoinPrefixes(
805
818
  args.coin,
806
- prefixIndex => wifToPrivateKeyInternal(prefixIndex, args),
819
+ prefixIndex => wifToPrivateKeyEncodingInternal(prefixIndex, args),
807
820
  ['Invalid network version']
808
821
  )
809
- if (privateKey == null) {
822
+ if (privateKeyEncoding == null) {
810
823
  throw new Error('Failed to convert WIF key to private key')
811
824
  }
812
- return privateKey
813
- }; exports.wifToPrivateKey = wifToPrivateKey
825
+ return privateKeyEncoding
826
+ }; exports.wifToPrivateKeyEncoding = wifToPrivateKeyEncoding
814
827
 
815
- function privateKeyToPubkey(privateKey) {
816
- return bitcoin.ECPair.fromPrivateKey(
817
- Buffer.from(privateKey, 'hex')
818
- ).publicKey.toString('hex')
819
- } 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;
820
836
 
821
837
  function signMessageBase64(message, privateKey) {
822
838
  const keyPair = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'))
@@ -829,7 +845,7 @@ const wifToPrivateKeyInternal = (
829
845
  } exports.signMessageBase64 = signMessageBase64;
830
846
 
831
847
  function makeTx(args) {
832
- const { log } = args
848
+ const { log, outputSort } = args
833
849
  let sequence = 0xffffffff
834
850
  if (args.setRBF) {
835
851
  sequence -= 2
@@ -962,14 +978,21 @@ const wifToPrivateKeyInternal = (
962
978
  }
963
979
 
964
980
  const sortedInputs = _bip69.sortInputs.call(void 0, result.inputs)
965
- const sortedOutputs = _bip69.sortOutputs.call(void 0, result.outputs)
981
+ const sortedOutputs = (() => {
982
+ switch (outputSort) {
983
+ case 'bip69':
984
+ return _bip69.sortOutputs.call(void 0, result.outputs)
985
+ case 'targets':
986
+ return result.outputs
987
+ }
988
+ })()
966
989
 
967
990
  const psbt = new bitcoin.Psbt()
968
991
  try {
969
992
  psbt.addInputs(sortedInputs)
970
993
  psbt.addOutputs(sortedOutputs)
971
994
  } catch (error) {
972
- _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)}`)])
973
996
  throw error
974
997
  }
975
998
 
@@ -979,7 +1002,7 @@ const wifToPrivateKeyInternal = (
979
1002
  changeUsed: result.changeUsed,
980
1003
  fee: result.fee,
981
1004
  psbtBase64: psbt.toBase64(),
982
- 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')])
983
1006
  }
984
1007
  } exports.makeTx = makeTx;
985
1008
 
@@ -988,13 +1011,13 @@ const wifToPrivateKeyInternal = (
988
1011
  const coin = _coinmapper.getCoinFromString.call(void 0, args.coin)
989
1012
 
990
1013
  for (let i = 0; i < psbt.inputCount; i++) {
991
- const privateKey = Buffer.from(
992
- _nullishCoalesce(args.privateKeys[i], () => ( args.privateKeys[args.privateKeys.length - 1])),
993
- 'hex'
994
- )
1014
+ const privateKeyEncoding =
1015
+ _nullishCoalesce(args.privateKeyEncodings[i], () => (
1016
+ args.privateKeyEncodings[args.privateKeyEncodings.length - 1]))
1017
+ const { hex, compressed } = privateKeyEncoding
995
1018
  psbt.signInput(
996
1019
  i,
997
- bitcoin.ECPair.fromPrivateKey(privateKey),
1020
+ bitcoin.ECPair.fromPrivateKey(Buffer.from(hex, 'hex'), { compressed }),
998
1021
  bitcoin.Psbt.DEFAULT_SIGHASHES,
999
1022
  coin.sighashFunction
1000
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;
@@ -133,6 +137,7 @@ export interface MakeTxArgs {
133
137
  freshChangeAddress: string;
134
138
  subtractFee?: boolean;
135
139
  log?: EdgeLog;
140
+ outputSort: 'bip69' | 'targets';
136
141
  }
137
142
  export interface MakeTxTarget {
138
143
  address?: string;
@@ -144,7 +149,7 @@ interface MakeTxReturn extends Required<utxopicker.UtxoPickerResult> {
144
149
  psbtBase64: string;
145
150
  }
146
151
  export interface SignTxArgs {
147
- privateKeys: string[];
152
+ privateKeyEncodings: PrivateKeyEncoding[];
148
153
  psbtBase64: string;
149
154
  coin: string;
150
155
  }
@@ -194,8 +199,8 @@ export declare function pubkeyToScriptPubkey(args: PubkeyToScriptPubkeyArgs): Pu
194
199
  export declare const toNewFormat: (address: string, coinName: string) => string;
195
200
  export declare const xprivToPrivateKey: (args: XPrivToPrivateKeyArgs) => string;
196
201
  export declare function privateKeyToWIF(args: PrivateKeyToWIFArgs): string;
197
- export declare const wifToPrivateKey: (args: WIFToPrivateKeyArgs) => string;
198
- export declare function privateKeyToPubkey(privateKey: string): string;
202
+ export declare const wifToPrivateKeyEncoding: (args: WIFToPrivateKeyEncodingArgs) => PrivateKeyEncoding;
203
+ export declare function privateKeyEncodingToPubkey(privateKeyEncoding: PrivateKeyEncoding): string;
199
204
  export declare function signMessageBase64(message: string, privateKey: string): string;
200
205
  export declare function makeTx(args: MakeTxArgs): MakeTxReturn;
201
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.1.6",
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",