added check if valid address to docs (#440)
* added check if valid address to docs * Update package.json
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
# Validate Adress
|
||||
|
||||
- This function will return true if the address is a legitamate Polkadot address and false if it is not
|
||||
|
||||
- Works both with Ed25519 and Sr25519 key types
|
||||
@@ -0,0 +1,16 @@
|
||||
const { encodeAddress, decodeAddress } = require('@polkadot/keyring');
|
||||
const { isHex, hexToU8a } = require('@polkadot/util');
|
||||
|
||||
const address = '5GrpknVvGGrGH3EFuURXeMrWHvbpj3VfER1oX5jFtuGbfzCE';
|
||||
|
||||
const isValidAddressPolkadotAddress = () => {
|
||||
try {
|
||||
encodeAddress(isHex(address) ? hexToU8a(address) : decodeAddress(address));
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const isValid = isValidAddressPolkadotAddress();
|
||||
console.log(isValid);
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "03_validate_address",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing how to validate a polkadot address",
|
||||
"main": "index.js",
|
||||
"author": "Jesse",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/keyring": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user