Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update account generation #344

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/CopyText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ function getTextToCopy({
}

export type CopyTextProps = CommonCopyTextProps &
VariantProps<typeof copyTextStyles>
VariantProps<typeof copyTextStyles> & {
wordBreakType?: 'words' | 'all'
}
export function CopyText({
text,
textToCopy,
onCopyClick,
isCodeText,
withHideButton,
wordBreakType = 'words',
size,
...props
}: CopyTextProps) {
Expand Down Expand Up @@ -75,7 +78,8 @@ export function CopyText({
>
<div
className={cx(
'cursor-pointer select-all break-all px-4 py-2',
'cursor-pointer select-all break-words px-4 py-2',
wordBreakType === 'words' ? 'break-words' : 'break-all',
copyTextStyles({ size }),
isHidden && 'blur-sm'
)}
Expand Down
1 change: 1 addition & 0 deletions src/components/auth/LoginModal/LoginModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default function LoginModal({

useEffect(() => {
if (props.isOpen) setCurrentStep(initialOpenState)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.isOpen])

const ModalContent = loginModalContents[currentStep]
Expand Down
1 change: 1 addition & 0 deletions src/components/auth/LoginModal/LoginModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const EnterSecretKeyContent = ({
const onSubmit = async (e: SyntheticEvent) => {
e.preventDefault()
beforeLogin?.()

if (await login(privateKey)) {
afterLogin?.()
setPrivateKey('')
Expand Down
22 changes: 19 additions & 3 deletions src/components/auth/ProfileModal/contents/PrivateKeyContent.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
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, isUsingMiniSecret } = useMemo(() => {
const decodedSecretKey = decodeSecretKey(encodedSecretKey ?? '')
if (isSecretKeyUsingMiniSecret(decodedSecretKey)) {
return { secretKey: `0x${decodedSecretKey}`, isUsingMiniSecret: true }
}

return {
secretKey: decodedSecretKey,
isUsingMiniSecret: false,
}
}, [encodedSecretKey])

const sendEvent = useSendEvent()
const onCopyClick = () => {
Expand All @@ -18,7 +29,12 @@ function PrivateKeyContent() {
Grill secret key is like a long password. We recommend keeping it in a
safe place, so you can recover your account.
</p>
<CopyText onCopyClick={onCopyClick} isCodeText text={secretKey || ''} />
<CopyText
onCopyClick={onCopyClick}
isCodeText
wordBreakType={isUsingMiniSecret ? 'all' : 'words'}
text={secretKey || ''}
/>
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ function ShareSessionContent() {
return (
<div className='mt-2 flex flex-col gap-4'>
<QrCode url={shareSessionLink} />
<CopyText text={shareSessionLink} onCopyClick={onCopyClick} />
<CopyText
wordBreakType='all'
text={shareSessionLink}
onCopyClick={onCopyClick}
/>
</div>
)
}
Expand Down
8 changes: 8 additions & 0 deletions src/stores/my-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
decodeSecretKey,
encodeSecretKey,
generateAccount,
isSecretKeyUsingMiniSecret,
loginWithSecretKey,
Signer,
} from '@/utils/account'
Expand Down Expand Up @@ -58,6 +59,13 @@ export const useMyAccount = create<State & Actions>()((set, get) => ({
try {
if (!secretKey) {
secretKey = (await generateAccount()).secretKey
} else {
if (secretKey.startsWith('0x')) {
const augmented = secretKey.substring(2)
if (isSecretKeyUsingMiniSecret(augmented)) {
secretKey = augmented
}
}
}

const signer = await loginWithSecretKey(secretKey)
Expand Down
37 changes: 24 additions & 13 deletions src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@ 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')
return { publicKey: pair.address, secretKey }
return { publicKey: pair.address, secretKey: mnemonic }
}

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 signer = keyring.addFromMnemonic(secretKey, {}, 'sr25519')
return signer
}

Expand All @@ -39,13 +44,19 @@ export function truncateAddress(address: string) {
}

export function encodeSecretKey(secretKey: string) {
return encodeURIComponent(Buffer.from(secretKey, 'hex').toString('base64'))
let buffer
if (isSecretKeyUsingMiniSecret(secretKey))
buffer = Buffer.from(secretKey, 'hex')
else buffer = Buffer.from(secretKey)

return encodeURIComponent(buffer.toString('base64'))
}

export function decodeSecretKey(encodedSecretKey: string) {
return Buffer.from(decodeURIComponent(encodedSecretKey), 'base64').toString(
'hex'
)
const buffer = Buffer.from(decodeURIComponent(encodedSecretKey), 'base64')
const hexString = buffer.toString('hex')
if (isSecretKeyUsingMiniSecret(hexString)) return hexString
return buffer.toString()
}

export async function validateAddress(address: string) {
Expand Down
Loading