@libp2p/floodsub 11.0.31-e48eabf67 → 11.0.32-2d4c93ed6

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,9 +1,9 @@
1
1
  import type { PubSubRPCMessage } from './floodsub.ts';
2
2
  import type { SignedMessage } from './index.ts';
3
- import type { PeerId, PrivateKey, PublicKey } from '@libp2p/interface';
3
+ import type { PeerId, PrivateKey } from '@libp2p/interface';
4
4
  export declare const SignPrefix: Uint8Array<ArrayBuffer>;
5
5
  /**
6
- * Signs the provided message with the given `peerId`
6
+ * Signs the provided message with the given private key
7
7
  */
8
8
  export declare function signMessage(privateKey: PrivateKey, message: {
9
9
  from: PeerId;
@@ -12,12 +12,8 @@ export declare function signMessage(privateKey: PrivateKey, message: {
12
12
  sequenceNumber: bigint;
13
13
  }, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise<SignedMessage>;
14
14
  /**
15
- * Verifies the signature of the given message
15
+ * Verifies the signature of the given message, returning false if its key does
16
+ * not belong to the author
16
17
  */
17
18
  export declare function verifySignature(message: SignedMessage, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise<boolean>;
18
- /**
19
- * Returns the PublicKey associated with the given message.
20
- * If no valid PublicKey can be retrieved an error will be returned.
21
- */
22
- export declare function messagePublicKey(message: SignedMessage): PublicKey;
23
19
  //# sourceMappingURL=sign.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../../src/sign.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAEtE,eAAO,MAAM,UAAU,yBAAyC,CAAA;AAEhE;;GAEG;AACH,wBAAsB,WAAW,CAAE,UAA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src/sign.js CHANGED
@@ -1,10 +1,11 @@
1
+ import { publicKeyForPeer } from '@libp2p/crypto/keys';
1
2
  import { peerIdFromPrivateKey } from '@libp2p/peer-id';
2
3
  import { concat as uint8ArrayConcat } from 'uint8arrays/concat';
3
4
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
4
5
  import { toRpcMessage } from "./utils.js";
5
6
  export const SignPrefix = uint8ArrayFromString('libp2p-pubsub:');
6
7
  /**
7
- * Signs the provided message with the given `peerId`
8
+ * Signs the provided message with the given private key
8
9
  */
9
10
  export async function signMessage(privateKey, message, encode) {
10
11
  // @ts-expect-error signature field is missing, added below
@@ -25,7 +26,8 @@ export async function signMessage(privateKey, message, encode) {
25
26
  return outputMessage;
26
27
  }
27
28
  /**
28
- * Verifies the signature of the given message
29
+ * Verifies the signature of the given message, returning false if its key does
30
+ * not belong to the author
29
31
  */
30
32
  export async function verifySignature(message, encode) {
31
33
  if (message.type !== 'signed') {
@@ -46,34 +48,18 @@ export async function verifySignature(message, encode) {
46
48
  key: undefined
47
49
  }).subarray()
48
50
  ]);
49
- // Get the public key
50
- const pubKey = messagePublicKey(message);
51
- // verify the base message
52
- return pubKey.verify(bytes, message.signature);
53
- }
54
- /**
55
- * Returns the PublicKey associated with the given message.
56
- * If no valid PublicKey can be retrieved an error will be returned.
57
- */
58
- export function messagePublicKey(message) {
59
- if (message.type !== 'signed') {
60
- throw new Error('Message type must be "signed" to have a public key');
51
+ let pubKey;
52
+ try {
53
+ // only trust a supplied key if it derives to the author
54
+ pubKey = await publicKeyForPeer(message.from, message.key);
61
55
  }
62
- // should be available in the from property of the message (peer id)
63
- if (message.from == null) {
64
- throw new Error('Could not get the public key from the originator id');
65
- }
66
- // an Ed25519/secp256k1 peer id embeds its own public key, so a key recovered
67
- // from `from` is provably the author's; trust it over the supplied message.key
68
- if (message.from.publicKey != null) {
69
- return message.from.publicKey;
70
- }
71
- // otherwise the supplied key must derive to the from peer id, else a message
72
- // could be signed by one key while claiming a different author
73
- if (message.key != null && message.from.equals(message.key.toMultihash().bytes)) {
74
- return message.key;
56
+ catch (err) {
57
+ if (err.name === 'InvalidPublicKeyError') {
58
+ return false;
59
+ }
60
+ throw err;
75
61
  }
76
- // We couldn't validate pubkey is from the originator, error
77
- throw new Error('Could not get the public key from the originator id');
62
+ // verify the base message
63
+ return pubKey.verify(bytes, message.signature);
78
64
  }
79
65
  //# sourceMappingURL=sign.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sign.js","sourceRoot":"","sources":["../../src/sign.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAKzC,MAAM,CAAC,MAAM,UAAU,GAAG,oBAAoB,CAAC,gBAAgB,CAAC,CAAA;AAEhE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAE,UAAsB,EAAE,OAAkF,EAAE,MAA6C;IAC1L,2DAA2D;IAC3D,MAAM,aAAa,GAAkB;QACnC,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,IAAI,EAAE,oBAAoB,CAAC,UAAU,CAAC;KACvC,CAAA;IAED,+DAA+D;IAC/D,MAAM,KAAK,GAAG,gBAAgB,CAAC;QAC7B,UAAU;QACV,MAAM,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,EAAE;KAC/C,CAAC,CAAA;IAEF,aAAa,CAAC,SAAS,GAAG,MAAM,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACtD,aAAa,CAAC,GAAG,GAAG,UAA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,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,SAAS,CAAC,CAAA;AAChD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/floodsub",
3
- "version": "11.0.31-e48eabf67",
3
+ "version": "11.0.32-2d4c93ed6",
4
4
  "description": "libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/floodsub#readme",
@@ -54,22 +54,22 @@
54
54
  "test:electron-main": "aegir test -t electron-main"
55
55
  },
56
56
  "dependencies": {
57
- "@libp2p/crypto": "5.1.24-e48eabf67",
58
- "@libp2p/interface": "3.3.1-e48eabf67",
59
- "@libp2p/interface-internal": "3.1.14-e48eabf67",
60
- "@libp2p/peer-collections": "7.0.29-e48eabf67",
61
- "@libp2p/peer-id": "6.0.16-e48eabf67",
62
- "@libp2p/utils": "7.4.2-e48eabf67",
57
+ "@libp2p/crypto": "5.1.25-2d4c93ed6",
58
+ "@libp2p/interface": "3.4.0-2d4c93ed6",
59
+ "@libp2p/interface-internal": "3.2.0-2d4c93ed6",
60
+ "@libp2p/peer-collections": "7.0.30-2d4c93ed6",
61
+ "@libp2p/peer-id": "6.0.17-2d4c93ed6",
62
+ "@libp2p/utils": "7.4.3-2d4c93ed6",
63
63
  "it-length-prefixed": "^11.0.1",
64
64
  "main-event": "^1.0.5",
65
65
  "multiformats": "^14.0.0",
66
- "p-queue": "^9.0.0",
66
+ "p-queue": "^10.0.0",
67
67
  "protons-runtime": "^7.0.0",
68
68
  "uint8arraylist": "^3.0.2",
69
69
  "uint8arrays": "^6.1.1"
70
70
  },
71
71
  "devDependencies": {
72
- "@libp2p/logger": "6.2.14-e48eabf67",
72
+ "@libp2p/logger": "6.2.15-2d4c93ed6",
73
73
  "@multiformats/multiaddr": "^13.0.3",
74
74
  "@types/sinon": "^22.0.0",
75
75
  "aegir": "^48.1.1",
package/src/sign.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { publicKeyForPeer } from '@libp2p/crypto/keys'
1
2
  import { peerIdFromPrivateKey } from '@libp2p/peer-id'
2
3
  import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
3
4
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
@@ -9,7 +10,7 @@ import type { PeerId, PrivateKey, PublicKey } from '@libp2p/interface'
9
10
  export const SignPrefix = uint8ArrayFromString('libp2p-pubsub:')
10
11
 
11
12
  /**
12
- * Signs the provided message with the given `peerId`
13
+ * Signs the provided message with the given private key
13
14
  */
14
15
  export async function signMessage (privateKey: PrivateKey, message: { from: PeerId, topic: string, data: Uint8Array, sequenceNumber: bigint }, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise<SignedMessage> {
15
16
  // @ts-expect-error signature field is missing, added below
@@ -34,7 +35,8 @@ export async function signMessage (privateKey: PrivateKey, message: { from: Peer
34
35
  }
35
36
 
36
37
  /**
37
- * Verifies the signature of the given message
38
+ * Verifies the signature of the given message, returning false if its key does
39
+ * not belong to the author
38
40
  */
39
41
  export async function verifySignature (message: SignedMessage, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise<boolean> {
40
42
  if (message.type !== 'signed') {
@@ -59,39 +61,19 @@ export async function verifySignature (message: SignedMessage, encode: (rpc: Pub
59
61
  }).subarray()
60
62
  ])
61
63
 
62
- // Get the public key
63
- const pubKey = messagePublicKey(message)
64
+ let pubKey: PublicKey
64
65
 
65
- // verify the base message
66
- return pubKey.verify(bytes, message.signature)
67
- }
66
+ try {
67
+ // only trust a supplied key if it derives to the author
68
+ pubKey = await publicKeyForPeer(message.from, message.key)
69
+ } catch (err: any) {
70
+ if (err.name === 'InvalidPublicKeyError') {
71
+ return false
72
+ }
68
73
 
69
- /**
70
- * Returns the PublicKey associated with the given message.
71
- * If no valid PublicKey can be retrieved an error will be returned.
72
- */
73
- export function messagePublicKey (message: SignedMessage): PublicKey {
74
- if (message.type !== 'signed') {
75
- throw new Error('Message type must be "signed" to have a public key')
74
+ throw err
76
75
  }
77
76
 
78
- // should be available in the from property of the message (peer id)
79
- if (message.from == null) {
80
- throw new Error('Could not get the public key from the originator id')
81
- }
82
-
83
- // an Ed25519/secp256k1 peer id embeds its own public key, so a key recovered
84
- // from `from` is provably the author's; trust it over the supplied message.key
85
- if (message.from.publicKey != null) {
86
- return message.from.publicKey
87
- }
88
-
89
- // otherwise the supplied key must derive to the from peer id, else a message
90
- // could be signed by one key while claiming a different author
91
- if (message.key != null && message.from.equals(message.key.toMultihash().bytes)) {
92
- return message.key
93
- }
94
-
95
- // We couldn't validate pubkey is from the originator, error
96
- throw new Error('Could not get the public key from the originator id')
77
+ // verify the base message
78
+ return pubKey.verify(bytes, message.signature)
97
79
  }