|
8 | 8 | generateJWT, |
9 | 9 | jwtExp, |
10 | 10 | } from "@walletconnect/did-jwt"; |
| 11 | +import { hashMessage } from "@ethersproject/hash"; |
| 12 | +import { recoverAddress } from "@ethersproject/transactions"; |
11 | 13 | import { ICore, IStore } from "@walletconnect/types"; |
12 | 14 | import { formatMessage, generateRandomBytes32 } from "@walletconnect/utils"; |
13 | 15 | import axios from "axios"; |
@@ -42,94 +44,101 @@ export class IdentityKeys implements IIdentityKeys { |
42 | 44 | await this.identityKeys.init(); |
43 | 45 | }; |
44 | 46 |
|
45 | | - private generateIdentityKey = async (accountId: string) => { |
46 | | - const privateKey = ed25519.utils.randomPrivateKey(); |
47 | | - const publicKey = await ed25519.getPublicKey(privateKey); |
48 | | - |
49 | | - const pubKeyHex = ed25519.utils.bytesToHex(publicKey).toLowerCase(); |
50 | | - const privKeyHex = ed25519.utils.bytesToHex(privateKey).toLowerCase(); |
51 | | - |
52 | | - return { |
53 | | - pubKeyHex, |
54 | | - persist: async () => { |
55 | | - // Deferring persistence to caller to only persist after success |
56 | | - // of signing and registering full cacao on keyserver |
57 | | - await this.core.crypto.keychain.set(pubKeyHex, privKeyHex); |
58 | | - await this.identityKeys.set(accountId, { |
59 | | - identityKeyPriv: privKeyHex, |
60 | | - identityKeyPub: pubKeyHex, |
61 | | - accountId, |
62 | | - }); |
63 | | - }, |
64 | | - }; |
65 | | - }; |
66 | | - |
67 | 47 | public generateIdAuth = async (accountId: string, payload: JwtPayload) => { |
68 | 48 | const { identityKeyPub, identityKeyPriv } = this.identityKeys.get(accountId); |
69 | 49 |
|
70 | 50 | return generateJWT([identityKeyPub, identityKeyPriv], payload); |
71 | 51 | }; |
72 | 52 |
|
73 | | - public async registerIdentity({ |
74 | | - accountId, |
75 | | - onSign, |
| 53 | + public isRegistered(account: string) { |
| 54 | + return this.identityKeys.keys.includes(account); |
| 55 | + } |
| 56 | + |
| 57 | + public async prepareRegistration({ |
76 | 58 | domain, |
| 59 | + accountId, |
77 | 60 | statement, |
| 61 | + }: { |
| 62 | + domain: string; |
| 63 | + statement?: string; |
| 64 | + accountId: string; |
| 65 | + }) { |
| 66 | + const { privateKey, pubKeyHex } = await this.generateIdentityKey(); |
| 67 | + |
| 68 | + const cacaoPayload = { |
| 69 | + aud: encodeEd25519Key(pubKeyHex), |
| 70 | + statement, |
| 71 | + domain, |
| 72 | + iss: composeDidPkh(accountId), |
| 73 | + nonce: generateRandomBytes32(), |
| 74 | + iat: new Date().toISOString(), |
| 75 | + version: "1", |
| 76 | + resources: [this.keyserverUrl], |
| 77 | + }; |
| 78 | + |
| 79 | + return { |
| 80 | + message: formatMessage(cacaoPayload, composeDidPkh(accountId)), |
| 81 | + registerParams: { |
| 82 | + cacaoPayload, |
| 83 | + privateIdentityKey: privateKey, |
| 84 | + }, |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + public async registerIdentity({ |
| 89 | + registerParams, |
| 90 | + signature, |
78 | 91 | }: RegisterIdentityParams): Promise<string> { |
79 | | - if (this.identityKeys.keys.includes(accountId)) { |
| 92 | + const accountId = registerParams.cacaoPayload.iss.split(":").slice(-3).join(":"); |
| 93 | + |
| 94 | + if (this.isRegistered(accountId)) { |
80 | 95 | const storedKeyPair = this.identityKeys.get(accountId); |
81 | 96 | return storedKeyPair.identityKeyPub; |
82 | 97 | } else { |
83 | 98 | try { |
84 | | - const { pubKeyHex, persist } = await this.generateIdentityKey(accountId); |
| 99 | + const message = formatMessage(registerParams.cacaoPayload, registerParams.cacaoPayload.iss); |
| 100 | + |
| 101 | + if (!signature) { |
| 102 | + throw new Error(`Provided an invalid signature. Expected a string but got: ${signature}`); |
| 103 | + } |
85 | 104 |
|
86 | | - const didKey = encodeEd25519Key(pubKeyHex); |
| 105 | + const recoveredAddress = recoverAddress(hashMessage(message), signature); |
| 106 | + const signatureValid = |
| 107 | + recoveredAddress.toLowerCase() === accountId.split(":").pop()!.toLowerCase(); |
| 108 | + |
| 109 | + if (!signatureValid) { |
| 110 | + throw new Error(`Provided an invalid signature. Signature ${signature} by account |
| 111 | + ${accountId} is not a valid signature for message ${message}`); |
| 112 | + } |
| 113 | + |
| 114 | + const url = `${this.keyserverUrl}/identity`; |
87 | 115 |
|
88 | 116 | const cacao: Cacao = { |
89 | 117 | h: { |
90 | 118 | t: "eip4361", |
91 | 119 | }, |
92 | | - p: { |
93 | | - aud: didKey, |
94 | | - statement, |
95 | | - domain, |
96 | | - iss: composeDidPkh(accountId), |
97 | | - nonce: generateRandomBytes32(), |
98 | | - iat: new Date().toISOString(), |
99 | | - version: "1", |
100 | | - resources: [this.keyserverUrl], |
101 | | - }, |
| 120 | + p: registerParams.cacaoPayload, |
102 | 121 | s: { |
103 | 122 | t: "eip191", |
104 | | - s: "", |
| 123 | + s: signature, |
105 | 124 | }, |
106 | 125 | }; |
107 | 126 |
|
108 | | - const cacaoMessage = formatMessage(cacao.p, composeDidPkh(accountId)); |
109 | | - |
110 | | - const signature = await onSign(cacaoMessage); |
111 | | - |
112 | | - if (!signature) { |
113 | | - throw new Error(`Provided an invalid signature. Expected a string but got: ${signature}`); |
114 | | - } |
115 | | - |
116 | | - const url = `${this.keyserverUrl}/identity`; |
117 | | - |
118 | 127 | try { |
119 | | - await axios.post(url, { |
120 | | - cacao: { |
121 | | - ...cacao, |
122 | | - s: { |
123 | | - ...cacao.s, |
124 | | - s: signature, |
125 | | - }, |
126 | | - }, |
127 | | - }); |
| 128 | + await axios.post(url, { cacao }); |
128 | 129 | } catch (e) { |
129 | 130 | throw new Error(`Failed to register on keyserver: ${e}`); |
130 | 131 | } |
131 | 132 |
|
132 | | - await persist(); |
| 133 | + // Persist keys only after successful registration |
| 134 | + const { pubKeyHex, privKeyHex } = await this.getKeyData(registerParams.privateIdentityKey); |
| 135 | + |
| 136 | + await this.core.crypto.keychain.set(pubKeyHex, privKeyHex); |
| 137 | + await this.identityKeys.set(accountId, { |
| 138 | + identityKeyPriv: privKeyHex, |
| 139 | + identityKeyPub: pubKeyHex, |
| 140 | + accountId, |
| 141 | + }); |
133 | 142 |
|
134 | 143 | return pubKeyHex; |
135 | 144 | } catch (error) { |
@@ -197,4 +206,27 @@ export class IdentityKeys implements IIdentityKeys { |
197 | 206 | public async hasIdentity({ account }: GetIdentityParams): Promise<boolean> { |
198 | 207 | return this.identityKeys.keys.includes(account); |
199 | 208 | } |
| 209 | + |
| 210 | + // --------------------------- Private Helpers -----------------------------// |
| 211 | + |
| 212 | + private generateIdentityKey = () => { |
| 213 | + const privateKey = ed25519.utils.randomPrivateKey(); |
| 214 | + |
| 215 | + return this.getKeyData(privateKey); |
| 216 | + }; |
| 217 | + |
| 218 | + private getKeyHex = (key: Uint8Array) => { |
| 219 | + return ed25519.utils.bytesToHex(key).toLowerCase(); |
| 220 | + }; |
| 221 | + |
| 222 | + private getKeyData = async (privateKey: Uint8Array) => { |
| 223 | + const publicKey = await ed25519.getPublicKey(privateKey); |
| 224 | + |
| 225 | + return { |
| 226 | + publicKey, |
| 227 | + privateKey, |
| 228 | + pubKeyHex: this.getKeyHex(publicKey), |
| 229 | + privKeyHex: this.getKeyHex(privateKey), |
| 230 | + }; |
| 231 | + }; |
200 | 232 | } |
0 commit comments