Skip to content

Commit

Permalink
Change default to use mnemonic with backward support
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Aug 16, 2023
1 parent 34fa2a9 commit fccd890
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/components/auth/ProfileModal/contents/PrivateKeyContent.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { CopyText } from '@/components/CopyText'
import { useSendEvent } from '@/stores/analytics'
import { useMyAccount } from '@/stores/my-account'
import { decodeSecretKey } from '@/utils/account'
import { decodeSecretKey, isSecretKeyUsingMiniSecret } from '@/utils/account'
import { useMemo } from 'react'

function PrivateKeyContent() {
const encodedSecretKey = useMyAccount((state) => state.encodedSecretKey)
const secretKey = decodeSecretKey(encodedSecretKey ?? '')
const secretKey = useMemo(() => {
const decodedSecretKey = decodeSecretKey(encodedSecretKey ?? '')
if (isSecretKeyUsingMiniSecret(decodedSecretKey)) {
return `0x${decodedSecretKey}`
}

return Buffer.from(decodedSecretKey, 'hex').toString()
}, [encodedSecretKey])

const sendEvent = useSendEvent()
const onCopyClick = () => {
Expand Down
23 changes: 15 additions & 8 deletions src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@ async function getKeyring() {
}

export async function generateAccount() {
const { mnemonicGenerate, mnemonicToMiniSecret } = await import(
'@polkadot/util-crypto'
)
const { mnemonicGenerate } = await import('@polkadot/util-crypto')
const keyring = await getKeyring()

const mnemonic = mnemonicGenerate()
const seed = mnemonicToMiniSecret(mnemonic)
const pair = keyring.addFromSeed(seed, {}, 'sr25519')
const pair = keyring.addFromMnemonic(mnemonic, {}, 'sr25519')

const secretKey = Buffer.from(seed).toString('hex')
const secretKey = Buffer.from(mnemonic).toString('hex')
return { publicKey: pair.address, secretKey }
}

export function isSecretKeyUsingMiniSecret(secretKey: string) {
return secretKey.length === 64
}

export async function loginWithSecretKey(secretKey: string): Promise<Signer> {
const keyring = await getKeyring()

const secret = Buffer.from(secretKey, 'hex')
const signer = keyring.addFromSeed(secret, {}, 'sr25519')
if (isSecretKeyUsingMiniSecret(secretKey)) {
const secret = Buffer.from(secretKey, 'hex')
const signer = keyring.addFromSeed(secret, {}, 'sr25519')
return signer
}

const secret = Buffer.from(secretKey, 'hex').toString()
const signer = keyring.addFromMnemonic(secret, {}, 'sr25519')
return signer
}

Expand Down

0 comments on commit fccd890

Please sign in to comment.