Created
April 2, 2024 09:41
-
-
Save haltakov/5a9a9a3e4bc3ef3b7a07329254c7350b to your computer and use it in GitHub Desktop.
Recover public key from DID token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {recoverPublicKey, hashMessage} from "ethers/lib/utils" | |
| // DID token generated from Wallet API /account endpoint | |
| const didToken = "WyIweDAyMTdhMjRlMTFkYTg4NTU2YWUwYzMxNzc3OWE4MGQ2OGYxNDY1YWMzNmY0ZWY4Yjk2YTY1ZTZkNzUxZWExMjk0NDZmZDc2N2NjZjczYTliZmFhZTEzZDczNDAxODNlZTliOWM3ODBkOGNkYzJlNDEzOWI5NzdlZWViYjkzYzExMWIiLCJ7XCJpYXRcIjoxNzEyMDQ5MTYxLFwiZXh0XCI6MTcxMjA1Mjc2MSxcImlzc1wiOlwiZGlkOmV0aHI6MHhmQjI1YTQxYTY0ZTc0ZjVDNmIyM2IyRkI0NDhDZjkzNDUxODlhM2U1XCIsXCJlbWFpbFwiOlwidmxhZGltaXIuaGFsdGFrb3ZAZ21haWwuY29tXCJ9Il0" | |
| // Public address generated from the Wallet API /account endpoint | |
| const actPublicKey = "0x04b5640e6b4e0590adf368105897c98acffde2dad3e1c148d6bcd05f410d5fb1a019bc7dea9e04badad53f81713317d931a926b7f4418bafc6cdb1a43dce1cb663" | |
| // Decompose the DID token into signature and payload | |
| const [signature, payload] = JSON.parse(Buffer.from(didToken, "base64").toString("utf-8")); | |
| // Compute the digest of the payload | |
| const digest = hashMessage(payload); | |
| // Recover the public key from the digest and signature | |
| const recPublicKey = recoverPublicKey(digest, signature); | |
| // Compare the recovered public key with the one returned from the Wallet API | |
| console.log({actPublicKey, recPublicKey}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment