-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from DIMO-Network/moiz/auth-endpoints
JWT Flow Working with Hardcoded Creds
- Loading branch information
Showing
10 changed files
with
10,871 additions
and
5,305 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var script = document.createElement('script'); | ||
script.src = "https://cdn.jsdelivr.net/npm/ethers@5.7.2/dist/ethers.umd.min.js"; | ||
script.onload = function() { | ||
console.log("Ethers.js (UMD) loaded!"); | ||
const message = 'SIGNED MESSAGE'; | ||
|
||
// const hashedMessageWithoutPrefix = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(message)); | ||
const hashedMessageWithoutPrefix = ethers.utils.sha256(ethers.utils.toUtf8Bytes(message)); | ||
|
||
|
||
const signature = "SIGNATURE"; | ||
|
||
const recoveredAddress = ethers.utils.recoverAddress(hashedMessageWithoutPrefix, signature); | ||
const expectedAddress = "EXPECTED ADDRESS"; | ||
|
||
console.log(recoveredAddress); | ||
console.log(expectedAddress); | ||
|
||
console.log(recoveredAddress == expectedAddress); | ||
}; | ||
document.head.appendChild(script); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Step 1: Define the signed message | ||
const signedMessage = "Hello from DIMO"; | ||
|
||
// Step 2: Hash the message without Ethereum's prefix | ||
const hashedMessageWithoutPrefix = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(signedMessage)); | ||
|
||
// Step 4: Extract r, s, and v values from the signature | ||
let signatureR = "60bb900a556ead99e87f30bc9fb274d8426951a84d2258786f664a81266e8aec"; | ||
let signatureS = "4661155ee5182c87e8e584d244c4d51e4d089b9c516f8c34e8edfa42d306c508"; | ||
let signatureV = "01"; // v is in hexadecimal | ||
|
||
// Step 5: Convert v from hex to decimal and adjust for Ethereum signature recovery | ||
signatureV = parseInt(signatureV, 16); // Convert hex "01" to decimal 1 | ||
|
||
// Step 6: Ensure r and s are 0x-prefixed and in the correct format | ||
signatureR = "0x" + signatureR; | ||
signatureS = "0x" + signatureS; | ||
|
||
// Step 7: Try recovering the address with v = 27 | ||
let signatureV27 = signatureV + 27; // v = 27 | ||
const combinedSignatureV27 = ethers.utils.joinSignature({ r: signatureR, s: signatureS, v: signatureV27 }); | ||
|
||
console.log(combinedSignatureV27); | ||
const recoveredAddress = ethers.utils.recoverAddress(hashedMessageWithoutPrefix, combinedSignatureV27); | ||
const expectedAddress = "0xB1E674372d4A9cA625a4f8dfA0E41493C3f8b9ca"; | ||
|
||
console.log(recoveredAddress); | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,102 @@ | ||
/** | ||
* turnkeyService.ts | ||
* | ||
* | ||
* This service handles all actions dependent on turnkey | ||
* using the Turnkey Client Libraries, or custom Dimo SDK's such as the transactions SDK | ||
* | ||
* Specific Responsibilities include: Signing Messages, Triggering OTP's etc | ||
* | ||
* Specific Responsibilities include: Signing Messages, Triggering OTP's etc | ||
*/ | ||
|
||
export {} | ||
import { | ||
KernelSigner, | ||
newKernelConfig, | ||
sacdPermissionValue, | ||
} from "@dimo-network/transactions"; | ||
import { | ||
getWebAuthnAttestation, | ||
} from "@turnkey/http"; | ||
import { IframeStamper } from "@turnkey/iframe-stamper"; | ||
import { WebauthnStamper } from "@turnkey/webauthn-stamper"; | ||
import { | ||
base64UrlEncode, | ||
generateRandomBuffer, | ||
} from "../utils/authUtils"; | ||
import { verifyEmail } from "./accountsService"; | ||
|
||
const stamper = new WebauthnStamper({ | ||
rpId: "ab1a735dff55.ngrok.app", //TODO: Should not be hardcoded | ||
}); | ||
|
||
const kernelSignerConfig = newKernelConfig({ | ||
rpcUrl: | ||
"https://polygon-amoy.g.alchemy.com/v2/-0PsUljNtSdA31-XWj-kL_L1Mx2ArYfS", //TODO: Store in ENV | ||
bundlerUrl: | ||
"https://rpc.zerodev.app/api/v2/bundler/f4d1596a-edfd-4063-8f99-2d8835e07739", //TODO: Store in ENV | ||
paymasterUrl: | ||
"https://rpc.zerodev.app/api/v2/paymaster/f4d1596a-edfd-4063-8f99-2d8835e07739", //TODO: Store in ENV | ||
environment: "dev", // omit this to default to prod | ||
}); | ||
|
||
let kernelSigner: KernelSigner; | ||
|
||
export const createPasskey = async (email: string) => { | ||
const challenge = generateRandomBuffer(); | ||
const authenticatorUserId = generateRandomBuffer(); | ||
|
||
// An example of possible options can be found here: | ||
// https://www.w3.org/TR/webauthn-2/#sctn-sample-registration | ||
const attestation = await getWebAuthnAttestation({ | ||
publicKey: { | ||
rp: { | ||
id: "ab1a735dff55.ngrok.app", | ||
name: "Dimo Passkey Wallet", | ||
}, | ||
challenge, | ||
pubKeyCredParams: [ | ||
{ | ||
type: "public-key", | ||
alg: -7, | ||
}, | ||
], | ||
user: { | ||
id: authenticatorUserId, | ||
name: email, | ||
displayName: email, | ||
}, | ||
authenticatorSelection: { | ||
requireResidentKey: true, | ||
residentKey: "required", | ||
userVerification: "preferred", | ||
}, | ||
}, | ||
}); | ||
|
||
return [attestation, base64UrlEncode(challenge)]; | ||
}; | ||
|
||
export const initializePasskey = async ( | ||
subOrganizationId: string, | ||
walletAddress: string | ||
) => { | ||
kernelSigner = new KernelSigner(kernelSignerConfig); | ||
await kernelSigner.passkeyInit( | ||
subOrganizationId, | ||
walletAddress as `0x${string}`, | ||
stamper | ||
); | ||
}; | ||
|
||
export const signChallenge = async ( | ||
challenge: string, | ||
organizationId: string, | ||
walletAddress: string | ||
) => { | ||
|
||
//This is triggering a turnkey API request to sign a raw payload | ||
//Notes on signature, turnkey api returns an ecdsa signature, which the kernel client is handling | ||
const signature = await kernelSigner.kernelClient.signMessage({ | ||
message: challenge, | ||
}); | ||
|
||
return signature; | ||
}; |
Oops, something went wrong.