Skip to content

Commit

Permalink
feat: Basic support for H160 and H256 accounts. (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Sapede committed Jul 27, 2021
1 parent b7c2818 commit bddc2a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
9 changes: 8 additions & 1 deletion src/middleware/validate/validateAddressMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ describe('validateAddressMiddleware', () => {
},
} as unknown as Request);

doesNotErrorWith('a valid H160 address', {
params: {
number: '1',
address: '0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac',
},
} as unknown as Request);

errorsWith(
'an address containing an invalid base58 char',
{
Expand All @@ -97,7 +104,7 @@ describe('validateAddressMiddleware', () => {
address: 'y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b',
},
} as unknown as Request,
new BadRequest('Invalid decoded address length')
new BadRequest('Invalid address format')
);

errorsWith(
Expand Down
42 changes: 27 additions & 15 deletions src/middleware/validate/validateAddressMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { checkAddressChecksum } from '@polkadot/util-crypto';
import { base58Decode } from '@polkadot/util-crypto';
import { isHex } from '@polkadot/util';
import {
base58Decode,
checkAddressChecksum,
isEthereumAddress,
} from '@polkadot/util-crypto';
import { defaults } from '@polkadot/util-crypto/address/defaults';
import { RequestHandler } from 'express';
import { BadRequest } from 'http-errors';
Expand All @@ -22,27 +26,35 @@ export const validateAddressMiddleware: RequestHandler = (req, _res, next) => {
};

/**
* Verify that an address is a valid substrate ss58 address.
* Verify that an address is a valid substrate address.
*
* Note: this is very similar '@polkadot/util-crypto/address/checkAddress,
* except it does not check the prefix.
* except it does not check the ss58 prefix and supports H256/H160 raw address.
*
* @param address potential ss58 address
* @param address potential ss58 or raw address
*/
function checkAddress(address: string): [boolean, string | undefined] {
let decoded;

try {
decoded = base58Decode(address);
} catch (error) {
return [false, (error as Error).message];
let u8Address;

if (isHex(address)) {
u8Address = Uint8Array.from(Buffer.from(address.slice(2), 'hex'));
} else {
try {
u8Address = base58Decode(address);
} catch (error) {
return [false, (error as Error).message];
}
}

if (!defaults.allowedEncodedLengths.includes(decoded.length)) {
return [false, 'Invalid decoded address length'];
if (defaults.allowedEncodedLengths.includes(u8Address.length)) {
const [isValid] = checkAddressChecksum(u8Address);

return [isValid, isValid ? undefined : 'Invalid decoded address checksum'];
}

const [isValid] = checkAddressChecksum(decoded);
if (isEthereumAddress(address)) {
return [true, undefined];
}

return [isValid, isValid ? undefined : 'Invalid decoded address checksum'];
return [false, 'Invalid address format'];
}

0 comments on commit bddc2a2

Please sign in to comment.