Skip to content

Commit bda842f

Browse files
committed
fix: use peerId in handoff-receive, not publicKey
1 parent c17a8d1 commit bda842f

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

packages/ocapn/src/client/ocapn.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ const makeBootstrapObject = (
704704
const {
705705
signedGive,
706706
receivingSession,
707-
receivingSide: receiverKeyForExporter,
707+
receivingSide: peerIdFromHandoffReceive,
708708
handoffCount,
709709
} = handoffReceive;
710710
const { object: handoffGive, signature: handoffGiveSig } = signedGive;
@@ -721,11 +721,12 @@ const makeBootstrapObject = (
721721
`${label}: Bootstrap withdraw-gift: No peer public key for session id: ${toHex(sessionId)}. This should never happen.`,
722722
);
723723
}
724+
const peerIdFromSession = makePublicKeyId(peerPublicKey);
724725
if (
725-
compareByteArrays(peerPublicKey.bytes, receiverKeyForExporter) !== 0
726+
compareByteArrays(peerIdFromSession, peerIdFromHandoffReceive) !== 0
726727
) {
727728
throw Error(
728-
`${label}: Bootstrap withdraw-gift: Receiver key mismatch.`,
729+
`${label}: Bootstrap withdraw-gift: Receiver key mismatch for session ${toHex(sessionId)}.\n peerIdFromSession: ${toHex(peerIdFromSession)}\n peerIdFromHandoffReceive: ${toHex(peerIdFromHandoffReceive)}`,
729730
);
730731
}
731732
if (compareByteArrays(sessionId, receivingSession) !== 0) {
@@ -1078,21 +1079,28 @@ export const makeOcapn = (
10781079

10791080
/** @type {MakeHandoff} */
10801081
const makeHandoff = signedGive => {
1082+
// We are the Receiver.
1083+
// This peer is the Gifter.
1084+
// The Exporter is specified by location in the HandoffGive.
1085+
const gifterLocation = peerLocation;
10811086
const {
10821087
object: { exporterLocation },
10831088
} = signedGive;
10841089
return HandledPromise.resolve(
10851090
(async () => {
10861091
const [receiverGifterSession, receiverExporterSession] =
10871092
await Promise.all([
1088-
provideSession(peerLocation),
1093+
provideSession(gifterLocation),
10891094
provideSession(exporterLocation),
10901095
]);
10911096
const {
10921097
ocapn,
10931098
id: receiverExporterSessionId,
1094-
self: { keyPair: receiverExporterKey },
1099+
self: { keyPair: receiverKeyForExporter },
10951100
} = receiverExporterSession;
1101+
const receiverPeerIdForExporter = makePublicKeyId(
1102+
receiverKeyForExporter.publicKey,
1103+
);
10961104
const {
10971105
self: { keyPair: receiverGifterKey },
10981106
} = receiverGifterSession;
@@ -1102,7 +1110,7 @@ export const makeOcapn = (
11021110
signedGive,
11031111
handoffCount,
11041112
receiverExporterSessionId,
1105-
receiverExporterKey.publicKey,
1113+
receiverPeerIdForExporter,
11061114
receiverGifterKey,
11071115
);
11081116
return E(bootstrap)['withdraw-gift'](signedHandoffReceive);

packages/ocapn/src/codecs/descriptors.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,22 @@ export const serializeHandoffReceive = handoffReceive => {
153153
* @param {HandoffGiveSigEnvelope} signedGive
154154
* @param {bigint} handoffCount
155155
* @param {Uint8Array} sessionId
156-
* @param {OcapnPublicKey} pubKeyForExporter
156+
* @param {Uint8Array} receiverPeerId
157157
* @param {OcapnKeyPair} privKeyForGifter
158158
* @returns {HandoffReceiveSigEnvelope}
159159
*/
160160
export const makeWithdrawGiftDescriptor = (
161161
signedGive,
162162
handoffCount,
163163
sessionId,
164-
pubKeyForExporter,
164+
receiverPeerId,
165165
privKeyForGifter,
166166
) => {
167167
/** @type {HandoffReceive} */
168168
const handoffReceive = {
169169
type: 'desc:handoff-receive',
170170
receivingSession: sessionId,
171-
// This should be removed from the spec
172-
receivingSide: pubKeyForExporter.bytes,
171+
receivingSide: receiverPeerId,
173172
handoffCount,
174173
signedGive,
175174
};

packages/ocapn/test/cryptography.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,16 @@ test('makeWithdrawGiftDescriptor', t => {
9191
key2: receiverKeyForExporter,
9292
sessionId: exporterReceiverSessionId,
9393
} = exporterReceiverSession;
94+
const receiverPeerIdForExporter = makePublicKeyId(
95+
receiverKeyForExporter.publicKey,
96+
);
9497
const { key2: receiverKeyForGifter } = gifterReceiverSession;
9598
const handoffCount = 0n;
9699
/** @type {HandoffReceive} */
97100
const handoffReceive = {
98101
type: 'desc:handoff-receive',
99102
receivingSession: exporterReceiverSessionId,
100-
receivingSide: receiverKeyForExporter.publicKey.bytes,
103+
receivingSide: receiverPeerIdForExporter,
101104
handoffCount,
102105
signedGive,
103106
};
@@ -106,7 +109,7 @@ test('makeWithdrawGiftDescriptor', t => {
106109
signedGive,
107110
handoffCount,
108111
exporterReceiverSessionId,
109-
receiverKeyForExporter.publicKey,
112+
receiverPeerIdForExporter,
110113
receiverKeyForGifter,
111114
);
112115
const signedReceiveIsValid = receiverKeyForGifter.publicKey.verify(

0 commit comments

Comments
 (0)