Ethereum keypair signing & verification (#664)

* Ethereum keypair signing & verification

* Verify against hashes expanded

* Adjust via flags

* ss58 encoding for 33-byte publicKey

* ecdsa types stores address as raw publicKey

* Adjust for hash lengths

* Adjust expand/compress specs
This commit is contained in:
Jaco Greeff
2020-08-17 19:52:44 +02:00
committed by GitHub
parent 9808f58540
commit 8554d47030
18 changed files with 139 additions and 138 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ import { PairInfo } from './types';
import { assert, stringToU8a, u8aFixLength } from '@polkadot/util';
import { naclDecrypt, scryptEncode, scryptFromU8a } from '@polkadot/util-crypto';
import { NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH, SCRYPT_LENGTH } from './defaults';
import { ENCODING, NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER, PUB_LENGTH, SEC_LENGTH, SEED_LENGTH, SCRYPT_LENGTH } from './defaults';
const SEED_OFFSET = PKCS8_HEADER.length;
@@ -43,7 +43,7 @@ function decodePkcs8 (encoded: Uint8Array): DecodeResult {
};
}
export default function decode (passphrase?: string, encrypted?: Uint8Array | null, encType: KeyringPair$JsonEncodingTypes[] = ['scrypt', 'xsalsa20-poly1305']): DecodeResult {
export default function decode (passphrase?: string, encrypted?: Uint8Array | null, encType: KeyringPair$JsonEncodingTypes[] = ENCODING): DecodeResult {
assert(encrypted, 'No encrypted data available to decode');
assert(passphrase || !encType.includes('xsalsa20-poly1305'), 'Password required to decode encypted data');
+4
View File
@@ -2,6 +2,9 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$JsonEncodingTypes } from '../types';
const ENCODING: KeyringPair$JsonEncodingTypes[] = ['scrypt', 'xsalsa20-poly1305'];
const NONCE_LENGTH = 24;
const PKCS8_DIVIDER = new Uint8Array([161, 35, 3, 33, 0]);
const PKCS8_HEADER = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
@@ -12,6 +15,7 @@ const SEED_LENGTH = 32;
const SCRYPT_LENGTH = SALT_LENGTH + (3 * 4);
export {
ENCODING,
NONCE_LENGTH,
PKCS8_DIVIDER,
PKCS8_HEADER,
+36 -49
View File
@@ -6,8 +6,8 @@ import { Keypair, KeypairType } from '@polkadot/util-crypto/types';
import { KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta, SignOptions } from '../types';
import { PairInfo } from './types';
import { assert, u8aConcat } from '@polkadot/util';
import { blake2AsU8a, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify, secp256k1Compress, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, secp256k1Verify } from '@polkadot/util-crypto';
import { assert, u8aConcat, u8aToHex } from '@polkadot/util';
import { blake2AsU8a, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, secp256k1Verify, secp256k1Compress } from '@polkadot/util-crypto';
import decode from './decode';
import encode from './encode';
@@ -35,54 +35,28 @@ const TYPE_PREFIX = {
};
const TYPE_SIGNATURE = {
ecdsa: (message: Uint8Array, pair: Partial<Keypair>) =>
secp256k1Sign(message, pair, 'blake2'),
ecdsa: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'blake2'),
ed25519: naclSign,
ethereum: (message: Uint8Array, pair: Partial<Keypair>) =>
secp256k1Sign(message, pair, 'keccak'),
ethereum: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'keccak'),
sr25519: schnorrkelSign
};
const TYPE_ADDRESS = {
ecdsa: (p: Uint8Array) => p.length > 32 ? blake2AsU8a(p) : p,
ed25519: (p: Uint8Array) => p,
ethereum: (p: Uint8Array) => keccakAsU8a(secp256k1Expand(p)),
sr25519: (p: Uint8Array) => p
};
const TYPE_VERIFY = {
ecdsa: (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) =>
secp256k1Verify(message, signature, blake2AsU8a(publicKey), 'blake2'),
ecdsa: (m: Uint8Array, s: Uint8Array, p: Uint8Array) => secp256k1Verify(m, s, TYPE_ADDRESS.ecdsa(p), 'blake2'),
ed25519: naclVerify,
ethereum: (message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) =>
secp256k1Verify(message, signature, keccakAsU8a(publicKey), 'keccak'),
ethereum: (m: Uint8Array, s: Uint8Array, p: Uint8Array) => secp256k1Verify(m, s, TYPE_ADDRESS.ethereum(p), 'keccak', true),
sr25519: schnorrkelVerify
};
function isEmpty (u8a: Uint8Array): boolean {
return u8a.reduce((count, u8): number => count + u8, 0) === 0;
}
function fromSeed (type: KeypairType, seed: Uint8Array): Keypair {
return TYPE_FROM_SEED[type](seed);
}
function sign (type: KeypairType, message: Uint8Array, pair: Partial<Keypair>, { withType = false }: SignOptions = {}): Uint8Array {
return u8aConcat(
// for multi-signatures, i.e. with indicator, append the signature type as per
// the MultiSignature enum
withType
? TYPE_PREFIX[type]
: SIG_TYPE_NONE,
TYPE_SIGNATURE[type](message, pair)
);
}
function verify (type: KeypairType, message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean {
return TYPE_VERIFY[type](message, signature, publicKey);
}
function getAddress (type: KeypairType, publicKey: Uint8Array, isCompressed = false): Uint8Array {
if (type === 'ecdsa' && publicKey.length > 32) {
return blake2AsU8a(publicKey);
} else if (type === 'ethereum' && isCompressed) {
return secp256k1Compress(publicKey).slice(-32);
}
return publicKey;
return u8a.reduce((count, u8) => count + u8, 0) === 0;
}
// Not 100% correct, since it can be a Uint8Array, but an invalid one - just say "undefined" is anything non-valid
@@ -129,11 +103,14 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
return encoded;
};
const encodeAddress = (): string =>
type === 'ethereum'
? ethereumEncode(TYPE_ADDRESS[type](publicKey))
: toSS58(TYPE_ADDRESS[type](publicKey));
return {
get address (): string {
return type === 'ethereum'
? ethereumEncode(getAddress(type, publicKey))
: toSS58(getAddress(type, publicKey));
return encodeAddress();
},
get isLocked (): boolean {
return isLocked(secretKey);
@@ -155,7 +132,7 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
publicKey = decoded.publicKey;
secretKey = decoded.secretKey;
} else {
const pair = fromSeed(type, decoded.secretKey);
const pair = TYPE_FROM_SEED[type](decoded.secretKey);
publicKey = pair.publicKey;
secretKey = pair.secretKey;
@@ -177,14 +154,24 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
setMeta: (additional: KeyringPair$Meta): void => {
meta = { ...meta, ...additional };
},
sign: (message: Uint8Array, options?: SignOptions): Uint8Array => {
sign: (message: Uint8Array, options: SignOptions = {}): Uint8Array => {
assert(!isLocked(secretKey), 'Cannot sign with a locked key pair');
return sign(type, message, { publicKey, secretKey }, options);
return u8aConcat(
options.withType
? TYPE_PREFIX[type]
: SIG_TYPE_NONE,
TYPE_SIGNATURE[type](message, { publicKey, secretKey })
);
},
toJson: (passphrase?: string): KeyringPair$Json => {
const address = ['ecdsa', 'ethereum'].includes(type)
? u8aToHex(secp256k1Compress(publicKey))
: encodeAddress();
return toJson(type, { address, meta }, recode(passphrase), !!passphrase);
},
toJson: (passphrase?: string): KeyringPair$Json =>
toJson(type, { address: toSS58(getAddress(type, publicKey, true)), meta }, recode(passphrase), !!passphrase),
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
verify(type, message, signature, publicKey)
TYPE_VERIFY[type](message, signature, publicKey)
};
}
+9 -4
View File
@@ -3,10 +3,12 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '@polkadot/util-crypto/types';
import { KeyringPair$Json, KeyringPair$Meta } from '../types';
import { KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta } from '../types';
import { base64Encode } from '@polkadot/util-crypto';
import { ENCODING } from './defaults';
interface PairStateJson {
address: string;
meta: KeyringPair$Meta;
@@ -14,6 +16,9 @@ interface PairStateJson {
// version 2 - nonce, encoded (previous)
// version 3 - salt, nonce, encoded
const VERSION = '3';
const ENC_NONE: KeyringPair$JsonEncodingTypes[] = ['none'];
export default function toJson (type: KeypairType, { address, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
return {
@@ -22,9 +27,9 @@ export default function toJson (type: KeypairType, { address, meta }: PairStateJ
encoding: {
content: ['pkcs8', type],
type: isEncrypted
? ['scrypt', 'xsalsa20-poly1305']
: ['none'],
version: '3'
? ENCODING
: ENC_NONE,
version: VERSION
},
meta
};