@leofcoin/codec-format-interface 1.7.6 → 1.7.8

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.
@@ -1,7 +1,9 @@
1
+ import type { base58String } from '@vandeurenglenn/base58';
2
+ import type { base32String } from '@vandeurenglenn/base32';
1
3
  export default class BasicInterface {
2
4
  #private;
3
- keys: string[];
4
5
  name: string;
6
+ get keys(): string[];
5
7
  get encoded(): Uint8Array;
6
8
  set encoded(value: Uint8Array);
7
9
  get decoded(): object;
@@ -27,7 +29,7 @@ export default class BasicInterface {
27
29
  /**
28
30
  * @return {String} encoded
29
31
  */
30
- toBs32(): string;
32
+ toBs32(): base32String;
31
33
  /**
32
34
  * @return {String} encoded
33
35
  */
@@ -1,4 +1,4 @@
1
- import bs32 from '@vandeurenglenn/base32';
1
+ import base32 from '@vandeurenglenn/base32';
2
2
  import base58 from '@vandeurenglenn/base58';
3
3
  import isHex from '@vandeurenglenn/is-hex';
4
4
  import proto from '@vandeurenglenn/proto-array';
@@ -7,9 +7,13 @@ import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/
7
7
  class BasicInterface {
8
8
  #encoded;
9
9
  #decoded;
10
- keys;
11
10
  name;
12
11
  #proto;
12
+ get keys() {
13
+ // handles proto keys
14
+ // protokey -> key
15
+ return Object.keys(this.#proto).map((key) => (key.endsWith('?') ? key.split('?')[0] : key));
16
+ }
13
17
  get encoded() {
14
18
  if (!this.#encoded)
15
19
  this.#encoded = this.encode();
@@ -28,7 +32,6 @@ class BasicInterface {
28
32
  }
29
33
  set proto(value) {
30
34
  this.#proto = value;
31
- this.keys = Object.keys(value);
32
35
  }
33
36
  get proto() {
34
37
  return this.#proto;
@@ -54,13 +57,13 @@ class BasicInterface {
54
57
  return isHex(string);
55
58
  }
56
59
  isBase32(string) {
57
- return bs32.isBase32(string);
60
+ return base32.isBase32(string);
58
61
  }
59
62
  isBase58(string) {
60
63
  return base58.isBase58(string);
61
64
  }
62
65
  fromBs32(encoded) {
63
- return this.decode(bs32.decode(encoded));
66
+ return this.decode(base32.decode(encoded));
64
67
  }
65
68
  fromBs58(encoded) {
66
69
  return this.decode(fromBase58(encoded));
@@ -74,7 +77,7 @@ class BasicInterface {
74
77
  }
75
78
  fromString(string) {
76
79
  const array = string.split(',');
77
- const arrayLike = array.map(string => Number(string));
80
+ const arrayLike = array.map((string) => Number(string));
78
81
  return this.decode(Uint8Array.from(arrayLike));
79
82
  }
80
83
  fromHex(string) {
@@ -94,7 +97,10 @@ class BasicInterface {
94
97
  toHex() {
95
98
  if (!this.encoded)
96
99
  this.encode();
97
- return toHex(this.encoded.toString().split(',').map(number => Number(number)));
100
+ return toHex(this.encoded
101
+ .toString()
102
+ .split(',')
103
+ .map((number) => Number(number)));
98
104
  }
99
105
  /**
100
106
  * @return {String} encoded
@@ -1,5 +1,5 @@
1
- import BasicInterface from "./basic-interface.js";
2
- import Hash from "./codec-hash.js";
1
+ import BasicInterface from './basic-interface.js';
2
+ import Hash from './codec-hash.js';
3
3
  export default class FormatInterface extends BasicInterface implements FormatInterface {
4
4
  #private;
5
5
  hashFormat: string;
@@ -17,6 +17,11 @@ export default class FormatInterface extends BasicInterface implements FormatInt
17
17
  name?: string;
18
18
  });
19
19
  get format(): string;
20
+ beforeHashing(decoded: {
21
+ [index: string]: any;
22
+ }): {
23
+ [index: string]: any;
24
+ };
20
25
  /**
21
26
  * @return {PeernetHash}
22
27
  */
@@ -19,7 +19,7 @@ class FormatInterface extends BasicInterface {
19
19
  this.fromUint8Array(buffer);
20
20
  else if (buffer instanceof ArrayBuffer)
21
21
  this.fromArrayBuffer(buffer);
22
- else if (typeof buffer === "string") {
22
+ else if (typeof buffer === 'string') {
23
23
  if (this.isHex(buffer))
24
24
  this.fromHex(buffer);
25
25
  else if (this.isBase58(buffer))
@@ -85,7 +85,7 @@ class FormatInterface extends BasicInterface {
85
85
  constructor(buffer, proto, options) {
86
86
  super();
87
87
  this.proto = proto;
88
- this.hashFormat = options?.hashFormat ? options.hashFormat : "bs32";
88
+ this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
89
89
  if (options?.name)
90
90
  this.name = options.name;
91
91
  this.init(buffer);
@@ -94,13 +94,16 @@ class FormatInterface extends BasicInterface {
94
94
  const upper = this.hashFormat.charAt(0).toUpperCase();
95
95
  return `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
96
96
  }
97
+ beforeHashing(decoded) {
98
+ delete decoded.hash;
99
+ return decoded;
100
+ }
97
101
  /**
98
102
  * @return {PeernetHash}
99
103
  */
100
104
  get peernetHash() {
101
- const decoded = this.decoded;
105
+ const decoded = this.beforeHashing({ ...this.decoded });
102
106
  // @ts-ignore
103
- delete decoded.hash;
104
107
  return new CodecHash(decoded, { name: this.name });
105
108
  }
106
109
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/codec-format-interface",
3
- "version": "1.7.6",
3
+ "version": "1.7.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {