* init * Attest component * Add top notification bar * better english * back to 3 steps * clean up * in the middle of the refactoring * Make TS pass * Clea up some comments * isPreclaimed * isOldClaimProcess * Attest vs Claim (#2737) * Choose between Claim and Attest * fix lint * fix flow between claim and attest (#2738) * Jaco's comments + Fetch StatementKind (#2741) * Fix small bug in flow * Add condition for the warning banner (#2742) * prevent crash for older chains * with useEffect * should work * back to something simple * remove hoc * fix CI * Add ETH address field for non preclaimed addresses (#2746) * Eth field * fix * remove stray logs * Update packages/page-claims/src/index.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * Update packages/page-claims/src/index.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * Update packages/apps/src/overlays/Attest.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * Show the amount of token that will be claimed (#2747) * show amount in attest * fix isApiReady * fix lint * Use counter instead of overlay (#2750) * Use counter instead of overlay * Add warning to claim (#2752) * add warning * css cleanup * fix unused const Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> * Update packages/page-claims/src/Warning.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * Update packages/page-claims/src/index.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * Update packages/page-claims/src/index.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * Update packages/page-claims/src/index.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * Update packages/page-claims/src/index.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * address comment shortcut * set Ethereum address outside transfor and fix typos * lint * extract claims.preclaims to a hook * address comments EthereumAddress type and avoid casting * Missing isUnsigned * use AddressMini in warning * use a proper step for eth address field * Add loading state * remove log * unwrapOrNull * Update packages/page-claims/src/Warning.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * fix for account plural translation * lint * Add getStatement function * nits * Fix lint * Update packages/page-claims/src/Warning.tsx Co-authored-by: Jaco Greeff <jacogr@gmail.com> * add statement * statement component * address comment unWrapOr * test styling with long md * add md to jest * Update packages/page-claims/src/Statement.tsx Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * copy text before * lint * Remove EthreumAddress type to avoid unexpected behaviors (#2796) * remove EthreumAddress type * createType when needed * revert * typo * Enhance UX for claim process (#2797) * update content * init * comment * Add HTML components for statements (#2798) * Add HTML components for statements * Update packages/page-claims/src/statements/saft.tsx * Update to final statements * More nits * Nits * OnSuccess * prevent display of empty statement when attested * Small fixmes * Update packages/page-claims/src/index.tsx * fix isError not being red as expected * remove word-break * statement text in black * attestation for -> accounc balance * Update packages/page-claims/src/Statement.tsx Co-authored-by: Logan Saether <x@logansaether.com> * margin left instead of space * I agree * Use iframe * update saft to QmXEkMahfhHJPzT3RjkXiZVFi77ZeVeuxtAjhojGRNYckz * remove hash and duplications (#2814) Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: Jaco Greeff <jacogr@gmail.com> Co-authored-by: Logan Saether <x@logansaether.com>
142 lines
4.1 KiB
TypeScript
142 lines
4.1 KiB
TypeScript
// Copyright 2017-2020 @polkadot/app-claims authors & contributors
|
|
// This software may be modified and distributed under the terms
|
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
|
|
|
import { EthereumAddress, EcdsaSignature, StatementKind } from '@polkadot/types/interfaces';
|
|
|
|
import secp256k1 from 'secp256k1/elliptic';
|
|
import { registry } from '@polkadot/react-api';
|
|
import { assert, hexToU8a, stringToU8a, u8aToBuffer, u8aConcat } from '@polkadot/util';
|
|
import { keccakAsHex, keccakAsU8a } from '@polkadot/util-crypto';
|
|
|
|
interface RecoveredSignature {
|
|
error: Error | null;
|
|
ethereumAddress: EthereumAddress | null;
|
|
signature: EcdsaSignature | null;
|
|
}
|
|
|
|
interface SignatureParts {
|
|
recovery: number;
|
|
signature: Buffer;
|
|
}
|
|
|
|
// converts an Ethereum address to a checksum representation
|
|
export function addrToChecksum (_address: string): string {
|
|
const address = _address.toLowerCase();
|
|
const hash = keccakAsHex(address.substr(2)).substr(2);
|
|
let result = '0x';
|
|
|
|
for (let n = 0; n < 40; n++) {
|
|
result = `${result}${
|
|
parseInt(hash[n], 16) > 7
|
|
? address[n + 2].toUpperCase()
|
|
: address[n + 2]
|
|
}`;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// convert a give public key to an Ethereum address (the last 20 bytes of an _exapnded_ key keccack)
|
|
export function publicToAddr (publicKey: Uint8Array): string {
|
|
return addrToChecksum(`0x${keccakAsHex(publicKey).slice(-40)}`);
|
|
}
|
|
|
|
// hash a message for use in signature recovery, adding the standard Ethereum header
|
|
export function hashMessage (message: string): Buffer {
|
|
const expanded = stringToU8a(`\x19Ethereum Signed Message:\n${message.length.toString()}${message}`);
|
|
const hashed = keccakAsU8a(expanded);
|
|
|
|
return u8aToBuffer(hashed);
|
|
}
|
|
|
|
// split is 65-byte signature into the r, s (combined) and recovery number (derived from v)
|
|
export function sigToParts (_signature: string): SignatureParts {
|
|
const signature = hexToU8a(_signature);
|
|
|
|
assert(signature.length === 65, `Invalid signature length, expected 65 found ${signature.length}`);
|
|
|
|
let v = signature[64];
|
|
|
|
if (v < 27) {
|
|
v += 27;
|
|
}
|
|
|
|
const recovery = v - 27;
|
|
|
|
assert(recovery === 0 || recovery === 1, 'Invalid signature v value');
|
|
|
|
return {
|
|
recovery,
|
|
signature: u8aToBuffer(signature.slice(0, 64))
|
|
};
|
|
}
|
|
|
|
// recover an address from a given message and a recover/signature combination
|
|
export function recoverAddress (message: string, { recovery, signature }: SignatureParts): string {
|
|
const msgHash = hashMessage(message);
|
|
const senderPubKey = secp256k1.recover(msgHash, signature, recovery);
|
|
|
|
return publicToAddr(
|
|
secp256k1.publicKeyConvert(senderPubKey, false).slice(1)
|
|
);
|
|
}
|
|
|
|
// recover an address from a signature JSON (as supplied by e.g. MyCrypto)
|
|
export function recoverFromJSON (signatureJson: string | null): RecoveredSignature {
|
|
try {
|
|
const { msg, sig } = JSON.parse(signatureJson || '{}') as Record<string, string>;
|
|
|
|
if (!msg || !sig) {
|
|
throw new Error('Invalid signature object');
|
|
}
|
|
|
|
const parts = sigToParts(sig);
|
|
|
|
return {
|
|
error: null,
|
|
ethereumAddress: registry.createType('EthereumAddress', recoverAddress(msg, parts)),
|
|
signature: registry.createType('EcdsaSignature', u8aConcat(parts.signature, new Uint8Array([parts.recovery])))
|
|
};
|
|
} catch (error) {
|
|
console.error(error);
|
|
|
|
return {
|
|
error: error as Error,
|
|
ethereumAddress: null,
|
|
signature: null
|
|
};
|
|
}
|
|
}
|
|
|
|
export interface Statement {
|
|
sentence: string;
|
|
url: string;
|
|
}
|
|
|
|
export function getStatement (network: string, kind?: StatementKind | null): Statement | undefined {
|
|
switch (network) {
|
|
case 'Polkadot CC1': {
|
|
if (!kind) {
|
|
return undefined;
|
|
}
|
|
|
|
const url = kind.isRegular
|
|
? 'https://statement.polkadot.network/regular.html'
|
|
: 'https://statement.polkadot.network/saft.html';
|
|
|
|
const hash = kind.isRegular
|
|
? 'Qmc1XYqT6S39WNp2UeiRUrZichUWUPpGEThDE6dAb3f6Ny'
|
|
: 'QmXEkMahfhHJPzT3RjkXiZVFi77ZeVeuxtAjhojGRNYckz';
|
|
|
|
return {
|
|
sentence: `I hereby agree to the terms of the statement whose SHA-256 multihash is ${hash}. (This may be found at the URL: ${url})`,
|
|
url
|
|
};
|
|
}
|
|
|
|
default:
|
|
return undefined;
|
|
}
|
|
}
|