@ocap/util 1.16.4 → 1.16.7

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.
Files changed (2) hide show
  1. package/lib/index.js +28 -13
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -22,7 +22,7 @@ const camelCase = require('lodash/camelCase');
22
22
  const isNull = require('lodash/isNull');
23
23
  const rightPad = require('lodash/padEnd');
24
24
  const leftPad = require('lodash/padStart');
25
- const multibase = require('multibase');
25
+ const { base58btc } = require('multiformats/bases/base58');
26
26
  const utf8 = require('utf8');
27
27
  const base64 = require('base64-url');
28
28
  const BN = require('bn.js');
@@ -31,6 +31,21 @@ const DID_PREFIX = 'did:abt:';
31
31
  const zero = new BN(0);
32
32
  const negative1 = new BN(-1);
33
33
 
34
+ const textDecoder = new TextDecoder();
35
+ const decodeText = (bytes) => textDecoder.decode(bytes);
36
+ const isBase58Encoded = (data) => {
37
+ if (data instanceof Uint8Array) {
38
+ data = decodeText(data);
39
+ }
40
+
41
+ // Ensure bufOrString is a string
42
+ if (Object.prototype.toString.call(data) !== '[object String]') {
43
+ return false;
44
+ }
45
+
46
+ return data[0] === 'z';
47
+ };
48
+
34
49
  /**
35
50
  * Returns a BN object, converts a number value to a BN
36
51
  * @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object
@@ -344,8 +359,8 @@ const toHex = (value, returnType) => {
344
359
  return returnType ? 'bytes' : bytesToHex(value);
345
360
  }
346
361
 
347
- if (multibase.isEncoded(value) === 'base58btc') {
348
- return returnType ? 'bytes' : bytesToHex(multibase.decode(value));
362
+ if (isBase58Encoded(value)) {
363
+ return returnType ? 'bytes' : bytesToHex(base58btc.decode(value));
349
364
  }
350
365
 
351
366
  if (isBoolean(value)) {
@@ -553,17 +568,17 @@ function isUUID(str) {
553
568
  function toUint8Array(v) {
554
569
  let vb = null;
555
570
  if ([null, undefined, ''].includes(v)) {
556
- vb = Uint8Array.from([]);
571
+ vb = new Uint8Array();
557
572
  } else if (Buffer.isBuffer(v)) {
558
- vb = Uint8Array.from(v);
573
+ vb = new Uint8Array(v);
559
574
  } else if (isHexStrict(v)) {
560
- vb = Uint8Array.from(hexToBytes(v));
575
+ vb = new Uint8Array(hexToBytes(v));
561
576
  } else if (isUint8Array(v)) {
562
- vb = Uint8Array.from(v);
563
- } else if (multibase.isEncoded(v) === 'base58btc') {
564
- vb = Uint8Array.from(multibase.decode(v));
577
+ vb = new Uint8Array(v);
578
+ } else if (isBase58Encoded(v)) {
579
+ vb = new Uint8Array(base58btc.decode(v));
565
580
  } else if (typeof v === 'string') {
566
- vb = Uint8Array.from(hexToBytes(toHex(v)));
581
+ vb = new Uint8Array(hexToBytes(toHex(v)));
567
582
  } else {
568
583
  throw new Error(
569
584
  `Unsupported input type ${typeof v} detected for toBuffer, only Uint8Array/Buffer/Hex/Base58 are supported`
@@ -596,7 +611,7 @@ function toBuffer(v) {
596
611
  * @throws {Error}
597
612
  */
598
613
  function toBase58(v) {
599
- const buf = multibase.encode('base58btc', toUint8Array(v));
614
+ const buf = base58btc.encode(toUint8Array(v));
600
615
  return Buffer.from(buf).toString('utf-8');
601
616
  }
602
617
 
@@ -609,11 +624,11 @@ function toBase58(v) {
609
624
  * @returns {buffer}
610
625
  */
611
626
  function fromBase58(v) {
612
- if (!multibase.isEncoded(v) === 'base58btc') {
627
+ if (isBase58Encoded(v) === false) {
613
628
  throw new Error('fromBase58 expect strict base58 encoded string as input');
614
629
  }
615
630
 
616
- return Buffer.from(multibase.decode(v));
631
+ return Buffer.from(base58btc.decode(v));
617
632
  }
618
633
 
619
634
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/util",
3
- "version": "1.16.4",
3
+ "version": "1.16.7",
4
4
  "description": "utils shared across multiple forge js libs, works in both node.js and browser",
5
5
  "keywords": [
6
6
  "forge",
@@ -16,7 +16,7 @@
16
16
  "base64-url": "^2.3.3",
17
17
  "bn.js": "5.2.0",
18
18
  "lodash": "^4.17.21",
19
- "multibase": "^3.1.1",
19
+ "multiformats": "^9.6.5",
20
20
  "utf8": "^3.0.0"
21
21
  },
22
22
  "resolutions": {
@@ -56,5 +56,5 @@
56
56
  "bugs": {
57
57
  "url": "https://github.com/ArcBlock/asset-chain/issues"
58
58
  },
59
- "gitHead": "3ffb49a6ff1d0cfb5669c1d0af0fbdad2b7cbd39"
59
+ "gitHead": "2d9b07c134cf57b6f73bf1ff2e76406fb682318e"
60
60
  }