From b6551e3df3a48510b54be8839ed132e910787f18 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 10 Aug 2020 17:31:23 +0200 Subject: [PATCH] Save address via AccountIndex input (#3381) * Save address via AccuntIndex input * Apply suggestions from code review * Update packages/react-hooks/src/useAccountInfo.ts --- package.json | 6 +- .../src/Contacts/modals/Create.tsx | 32 +++-- packages/page-contracts/package.json | 2 +- packages/react-api/package.json | 2 +- yarn.lock | 112 +++++++++--------- 5 files changed, 83 insertions(+), 71 deletions(-) diff --git a/package.json b/package.json index 7603f802d7..5eb74258ac 100644 --- a/package.json +++ b/package.json @@ -33,10 +33,10 @@ "afterSign": "electron-builder-notarize" }, "resolutions": { - "@polkadot/api": "^1.28.1", - "@polkadot/api-contract": "^1.28.1", + "@polkadot/api": "^1.29.0-beta.0", + "@polkadot/api-contract": "^1.29.0-beta.0", "@polkadot/keyring": "^3.1.1", - "@polkadot/types": "^1.28.1", + "@polkadot/types": "^1.29.0-beta.0", "@polkadot/util": "^3.1.1", "@polkadot/util-crypto": "^3.1.1", "@polkadot/wasm-crypto": "^1.3.1", diff --git a/packages/page-accounts/src/Contacts/modals/Create.tsx b/packages/page-accounts/src/Contacts/modals/Create.tsx index 086b1f56ff..df19421f0d 100644 --- a/packages/page-accounts/src/Contacts/modals/Create.tsx +++ b/packages/page-accounts/src/Contacts/modals/Create.tsx @@ -2,12 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { DeriveAccountInfo } from '@polkadot/api-derive/types'; import { ActionStatus } from '@polkadot/react-components/Status/types'; import { ModalProps as Props } from '../../types'; import React, { useCallback, useState } from 'react'; - import { AddressRow, Button, Input, InputAddress, Modal } from '@polkadot/react-components'; +import { useApi, useCall } from '@polkadot/react-hooks'; import keyring from '@polkadot/ui-keyring'; import { useTranslation } from '../../translate'; @@ -17,6 +18,7 @@ interface AddrState { addressInput: string; isAddressExisting: boolean; isAddressValid: boolean; + isPublicKey: boolean; } interface NameState { @@ -26,21 +28,25 @@ interface NameState { function Create ({ onClose, onStatusChange }: Props): React.ReactElement { const { t } = useTranslation(); + const { api } = useApi(); const [{ isNameValid, name }, setName] = useState({ isNameValid: false, name: '' }); - const [{ address, addressInput, isAddressExisting, isAddressValid }, setAddress] = useState({ address: '', addressInput: '', isAddressExisting: false, isAddressValid: false }); - const isValid = isAddressValid && isNameValid; + const [{ address, addressInput, isAddressExisting, isAddressValid }, setAddress] = useState({ address: '', addressInput: '', isAddressExisting: false, isAddressValid: false, isPublicKey: false }); + const info = useCall(!!address && isAddressValid && api.derive.accounts.info, [address]); + const isValid = (isAddressValid && isNameValid) && !info?.accountId; const _onChangeAddress = useCallback( (addressInput: string): void => { let address = ''; let isAddressValid = true; let isAddressExisting = false; + let isPublicKey = false; try { - address = keyring.encodeAddress( - keyring.decodeAddress(addressInput) - ); + const publicKey = keyring.decodeAddress(addressInput); + + address = keyring.encodeAddress(publicKey); isAddressValid = keyring.isAvailable(address); + isPublicKey = publicKey.length === 32; if (!isAddressValid) { const old = keyring.getAddress(address); @@ -58,7 +64,7 @@ function Create ({ onClose, onStatusChange }: Props): React.ReactElement isAddressValid = false; } - setAddress({ address: isAddressValid ? address : '', addressInput, isAddressExisting, isAddressValid }); + setAddress({ address: isAddressValid ? address : '', addressInput, isAddressExisting, isAddressValid, isPublicKey }); }, [name] ); @@ -72,11 +78,13 @@ function Create ({ onClose, onStatusChange }: Props): React.ReactElement (): void => { const status = { action: 'create' } as ActionStatus; - if (!isValid) { + if (!isValid || !info?.accountId) { return; } try { + const address = info.accountId.toString(); + keyring.saveAddress(address, { genesisHash: keyring.genesisHash, name: name.trim(), tags: [] }); status.account = address; @@ -94,7 +102,7 @@ function Create ({ onClose, onStatusChange }: Props): React.ReactElement onStatusChange(status); onClose(); }, - [address, isAddressExisting, isValid, name, onClose, onStatusChange, t] + [info, isAddressExisting, isValid, name, onClose, onStatusChange, t] ); return ( @@ -103,7 +111,11 @@ function Create ({ onClose, onStatusChange }: Props): React.ReactElement