How did you create an account? You only need the password if you are getting the keys from the JSON backup file.
Your JSON backup file is named like this: your_account_address.json. Unlike your mnemonic phrase, your JSON backup file is password-protected. It has all the information to restore your account, but you cannot just read it because it is encrypted for safety. To restore your account with the JSON backup file, you need to have both the file and the password to decrypt
And this is why Polkadot UI is asking it because is not storing the keys or the mnemonic.
In PolkadotJS you can recover the keys from the JSON file you have created with Polkadot UI and sign a message/transaction like this:
import keyring from '@polkadot/ui-keyring';
import { cryptoWaitReady } from '@polkadot/util-crypto';
async function keys() {
// before sr25519 can be used, the WASM libraries needs to be loaded and initialized
await cryptoWaitReady();
// load all available addresses and accounts
keyring.loadAll({ ss58Format: 42, type: 'sr25519' });
const pairFromJSON = keyring.createFromJson(your_json);
//Check your address
console.log(pairFromJSON.address);
// decrypt it
const keys = keyring.addPair(pairFromJSON, 'myStr0ngP@ssworD');
//Do a normal signature (You can sign an extrinsic too)
const signature = keys.pair.sign("message");
console.log(signature);
}
If you want to create the JSON file with PolkadotJS too, you can get your keys from a Mnemonic phrase:
// With that you get the json file and the pair to sign messages/extrinsics
const { pair, json } = keyring.addUri(MNEMONIC_PHRASE, 'myStr0ngP@ssworD', { name: 'mnemonic acc' });
// sign with the pair generated above
const signature = pair.sign("message");
Or you can also generate the keys itself using PolkadotJS:
import { randomAsHex } from '@polkadot/util-crypto';
// generate a random hex raw seed, this needs to be 32 bytes in length
const seed = randomAsHex(32);
// add the account, encrypting it, override to ed25519
const { pair, json } = keyring.addUri(seed, 'myStr0ngP@ssworD', { name: 'hex seed acc' }, 'ed25519');
// sign with the pair generated above
const signature = pair.sign("message");