-
-
Save sondreb/fc3ad3ec2392976c2f4fc103df9d1feb to your computer and use it in GitHub Desktop.
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
import { base64url } from '@scure/base'; | |
import { entropyToMnemonic } from '@scure/bip39'; | |
import { wordlist } from '@scure/bip39/wordlists/english'; | |
// Function to convert Base64URL string to Uint8Array | |
function base64UrlToUint8Array(b64url: string): Uint8Array { | |
return base64url.decode(b64url + '='.repeat((4 - (b64url.length % 4)) % 4)); | |
} | |
// Function to convert the private key to a BIP39 mnemonic | |
function privateKeysToMnemonic(combinedPrivKey: string): { privateKey: string, mnemonic: string } { | |
// Split the combined private key into two Base64URL strings | |
const b64urlpks = combinedPrivKey.split(':'); | |
// Decode the two Base64URL strings to Uint8Arrays | |
const b64urlpk1 = b64urlpks[0]; | |
const b64urlpk2 = b64urlpks[1]; | |
// Convert the two Base64URL strings to Uint8Arrays | |
const privateKey1 = base64UrlToUint8Array(b64urlpk1); | |
const privateKey2 = base64UrlToUint8Array(b64urlpk2); | |
// Check if the private keys are 32 bytes each | |
if (privateKey1.length === 32 && privateKey2.length === 32) { | |
// Convert the each 32-byte private key to a 24-word mnemonic | |
const m1 = entropyToMnemonic(privateKey1, wordlist); | |
const m2 = entropyToMnemonic(privateKey2, wordlist); | |
// Combine the two mnemonics and return 48-word mnemonic | |
return { privateKey: combinedPrivKey, mnemonic: `${m1} ${m2}` }; | |
} else { | |
throw new Error('Unexpected private key length. Expected 32 bytes.'); | |
} | |
} | |
try { | |
const privKey0 = {}; // Insert the #0 private key here | |
const privKeySig = {}; // Insert the #sig private key here | |
const { privateKey, mnemonic } = privateKeysToMnemonic(`${privKey0}:${privKeySig}`); | |
console.log('combined privateKeys', privateKey); | |
console.log('combined mnemonics', mnemonic); | |
} catch (error) { | |
console.error('Error:', error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment