Page MenuHomePhabricator

D6131.id20543.diff
No OneTemporary

D6131.id20543.diff

diff --git a/lib/utils/siwe-utils.js b/lib/utils/siwe-utils.js
--- a/lib/utils/siwe-utils.js
+++ b/lib/utils/siwe-utils.js
@@ -52,6 +52,22 @@
return siweStatementWithPublicKeyRegex.test(candidate);
}
+const publicKeyFromSIWEStatementRegex: RegExp = /[a-zA-Z0-9+/]{43}/;
+function getPublicKeyFromSIWEStatement(statement: string): string {
+ invariant(
+ isValidSIWEStatementWithPublicKey(statement),
+ 'candidate must be well formed SIWE statement with public key',
+ );
+ const publicKeyMatchArray = statement.match(publicKeyFromSIWEStatementRegex);
+ invariant(
+ publicKeyMatchArray !== null &&
+ publicKeyMatchArray !== undefined &&
+ publicKeyMatchArray.length === 1,
+ 'publicKeyMatchArray should have one and only one element',
+ );
+ return publicKeyMatchArray[0];
+}
+
export {
siweStatement,
isValidSIWENonce,
@@ -60,4 +76,5 @@
isValidSIWEMessage,
getSIWEStatementForPublicKey,
isValidSIWEStatementWithPublicKey,
+ getPublicKeyFromSIWEStatement,
};
diff --git a/lib/utils/siwe-utils.test.js b/lib/utils/siwe-utils.test.js
--- a/lib/utils/siwe-utils.test.js
+++ b/lib/utils/siwe-utils.test.js
@@ -1,6 +1,7 @@
// @flow
import {
+ getPublicKeyFromSIWEStatement,
getSIWEStatementForPublicKey,
isValidEthereumAddress,
isValidPrimaryIdentityPublicKey,
@@ -201,4 +202,14 @@
),
).toBe(true);
});
+
+ it(`getPublicKeyFromSIWEStatement should pull public key out of valid SIWE statement`, () => {
+ const validSIWEMessageStatement =
+ `Device IdPubKey: rPFzRtV7E6v1b60zjTvghqb2xgnggmn6j4UaYccJYdo ` +
+ `By continuing, I accept the Comm Terms of Service: https://comm.app/terms`;
+
+ expect(getPublicKeyFromSIWEStatement(validSIWEMessageStatement)).toBe(
+ 'rPFzRtV7E6v1b60zjTvghqb2xgnggmn6j4UaYccJYdo',
+ );
+ });
});

File Metadata

Mime Type
text/plain
Expires
Sat, Jan 11, 6:08 AM (20 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2858222
Default Alt Text
D6131.id20543.diff (1 KB)

Event Timeline