forked from wanderwallet/Wander
-
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.
- Loading branch information
1 parent
e67d70b
commit 2cf1ea5
Showing
7 changed files
with
456 additions
and
58 deletions.
There are no files selected for viewing
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
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,14 +1,58 @@ | ||
import type { OnMessageCallback } from "@arconnect/webext-bridge"; | ||
import type { GenerateCall, GenerationResult } from "shim"; | ||
import type { JWKInterface } from "arweave/web/lib/wallet"; | ||
import { getKeyPairFromMnemonic } from "human-crypto-keys"; | ||
|
||
export const generateWalletInBackground: OnMessageCallback< | ||
// @ts-expect-error | ||
GenerateCall, | ||
GenerationResult | ||
> = async ({ data, sender }) => { | ||
if (sender.context !== "web_accessible") return; | ||
/** | ||
* Credits to arweave.app for the mnemonic wallet generation | ||
* | ||
* https://github.com/jfbeats/ArweaveWebWallet/blob/master/src/functions/Wallets.ts | ||
* https://github.com/jfbeats/ArweaveWebWallet/blob/master/src/functions/Crypto.ts | ||
*/ | ||
|
||
/** | ||
* Generate a JWK from a mnemonic seedphrase | ||
* | ||
* @param mnemonic Mnemonic seedphrase to generate wallet from | ||
* @returns Wallet JWK | ||
*/ | ||
export async function jwkFromMnemonic(mnemonic: string) { | ||
const { privateKey } = await getKeyPairFromMnemonic( | ||
mnemonic, | ||
{ | ||
id: "rsa", | ||
modulusLength: 4096 | ||
}, | ||
{ privateKeyFormat: "pkcs8-der" } | ||
); | ||
const jwk = pkcs8ToJwk(privateKey as any); | ||
|
||
return jwk; | ||
} | ||
|
||
/** | ||
* Convert a PKCS8 private key to a JWK | ||
* | ||
* @param privateKey PKCS8 private key to convert | ||
* @returns JWK | ||
*/ | ||
async function pkcs8ToJwk(privateKey: Uint8Array): Promise<JWKInterface> { | ||
const key = await window.crypto.subtle.importKey( | ||
"pkcs8", | ||
privateKey, | ||
{ name: "RSA-PSS", hash: "SHA-256" }, | ||
true, | ||
["sign"] | ||
); | ||
const jwk = await window.crypto.subtle.exportKey("jwk", key); | ||
|
||
return { | ||
address: "test" | ||
kty: jwk.kty!, | ||
e: jwk.e!, | ||
n: jwk.n!, | ||
d: jwk.d, | ||
p: jwk.p, | ||
q: jwk.q, | ||
dp: jwk.dp, | ||
dq: jwk.dq, | ||
qi: jwk.qi | ||
}; | ||
}; | ||
} |
Oops, something went wrong.