Verify with MultiSignature (#703)

* Verify with MultiSignature

* Bump dev
This commit is contained in:
Jaco Greeff
2020-10-07 13:24:24 +02:00
committed by GitHub
parent fab087bbe5
commit ae1a37cdf7
6 changed files with 583 additions and 4406 deletions
+7
View File
@@ -2,9 +2,16 @@
### 3.6.0-beta.x
Contributed:
- Remove invalid link to docs (thanks to https://github.com/roccomuso)
Changes:
- Rename `mnemonicToBip39` to `mnemonicToLegacySeed` (used in non-Substrate compatible fashion)
- Update pair signature verification to handle MultiSignature results
- Move documentation to docs repo
## 3.5.1 Sep 27, 2020
+3 -3
View File
@@ -21,11 +21,11 @@
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@polkadot/dev": "^0.57.1",
"@polkadot/ts": "^0.3.48",
"@polkadot/dev": "^0.58.1",
"@polkadot/ts": "^0.3.49",
"@types/jest": "^26.0.14",
"typedoc": "^0.19.2",
"typedoc-plugin-markdown": "^3.0.6",
"typedoc-plugin-markdown": "^3.0.7",
"typedoc-plugin-no-inherit": "^1.2.0"
},
"version": "3.6.0-beta.4"
+44
View File
@@ -84,6 +84,16 @@ describe('keypair', (): void => {
const signature = pair.sign(MESSAGE);
expect(pair.verify(MESSAGE, signature)).toBe(true);
expect(pair.verify(new Uint8Array(), signature)).toBe(false);
});
it('signs and verifies (withType)', (): void => {
const MESSAGE = stringToU8a('this is a message');
const pair = keyring.getPair(publicKeyOne);
const signature = pair.sign(MESSAGE, { withType: true });
expect(pair.verify(MESSAGE, signature)).toBe(true);
expect(pair.verify(new Uint8Array(), signature)).toBe(false);
});
});
@@ -150,6 +160,16 @@ describe('keypair', (): void => {
const signature = pair.sign(MESSAGE);
expect(pair.verify(MESSAGE, signature)).toBe(true);
expect(pair.verify(new Uint8Array(), signature)).toBe(false);
});
it('signs and verifies (withType)', (): void => {
const MESSAGE = stringToU8a('this is a message');
const pair = keyring.getPair(publicKeyOne);
const signature = pair.sign(MESSAGE, { withType: true });
expect(pair.verify(MESSAGE, signature)).toBe(true);
expect(pair.verify(new Uint8Array(), signature)).toBe(false);
});
});
@@ -245,6 +265,16 @@ describe('keypair', (): void => {
const signature = pair.sign(MESSAGE);
expect(pair.verify(MESSAGE, signature)).toBe(true);
expect(pair.verify(new Uint8Array(), signature)).toBe(false);
});
it('signs and verifies (withType)', (): void => {
const MESSAGE = stringToU8a('this is a message');
const pair = keyring.getPair(addressKeyOne);
const signature = pair.sign(MESSAGE, { withType: true });
expect(pair.verify(MESSAGE, signature)).toBe(true);
expect(pair.verify(new Uint8Array(), signature)).toBe(false);
});
});
@@ -300,6 +330,20 @@ describe('keypair', (): void => {
const signature = signer.sign(MESSAGE);
expect(verifier.verify(MESSAGE, signature)).toBe(true);
expect(verifier.verify(new Uint8Array(), signature)).toBe(false);
});
it('allows for signing/verification (withType)', (): void => {
const MESSAGE = stringToU8a('just some test message');
const signer = keyring.createFromUri(PHRASE);
const verifier = keyring.addFromJson(
JSON.parse('{"address":"KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou","encoded":"U8qFEaghhmNV2PgFhjqzmhyUy37Ok7abfFU2MNsBd0sAgAAAAQAAAAgAAAA3+NniKogzNphiMNueB1X0sGA07B6CaXWfpXPx45iSXoTTprwzU5mOoSqUWO0GKHROI72LN+uJ8Yfv6Ll6JOOV3VPKfoVoFmYm+zDrrMPa0gk5E5kUuSijxADcE6zUrliPVr0Ix/qaghu5SJ7RtWDQLBf4Hp86SJ8Gg6gTSSk=","encoding":{"content":["pkcs8","ethereum"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{}}')
);
const signature = signer.sign(MESSAGE, { withType: true });
expect(verifier.verify(MESSAGE, signature)).toBe(true);
expect(verifier.verify(new Uint8Array(), signature)).toBe(false);
});
});
+2 -9
View File
@@ -6,7 +6,7 @@ import { KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPa
import { PairInfo } from './types';
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 { blake2AsU8a, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, secp256k1Compress, signatureVerify } from '@polkadot/util-crypto';
import decode from './decode';
import encode from './encode';
@@ -47,13 +47,6 @@ const TYPE_ADDRESS = {
sr25519: (p: Uint8Array) => p
};
const TYPE_VERIFY = {
ecdsa: (m: Uint8Array, s: Uint8Array, p: Uint8Array) => secp256k1Verify(m, s, TYPE_ADDRESS.ecdsa(p), 'blake2'),
ed25519: naclVerify,
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) => count + u8, 0) === 0;
}
@@ -185,6 +178,6 @@ export default function createPair ({ toSS58, type }: Setup, { publicKey, secret
return toJson(type, { address, meta }, recode(passphrase), !!passphrase);
},
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
TYPE_VERIFY[type](message, signature, publicKey)
signatureVerify(message, signature, TYPE_ADDRESS[type](publicKey), type === 'ethereum').isValid
};
}
+29 -30
View File
@@ -10,32 +10,33 @@ import naclVerify from '../nacl/verify';
import schnorrkelVerify from '../schnorrkel/verify';
import secp256k1Verify from '../secp256k1/verify';
const VERIFIERS: [KeypairType, (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string) => boolean][] = [
['ed25519', naclVerify],
['sr25519', schnorrkelVerify],
type Verifier = [KeypairType, (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, isExpanded?: boolean) => boolean];
const VERIFIERS_ECDSA: Verifier[] = [
[
'ecdsa',
(message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string) =>
secp256k1Verify(message, signature, publicKey, 'blake2')
(message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, isExpanded?: boolean) =>
secp256k1Verify(message, signature, publicKey, 'blake2', isExpanded)
],
[
'ethereum',
(message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string) =>
secp256k1Verify(message, signature, publicKey, 'keccak')
(message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, isExpanded?: boolean) =>
secp256k1Verify(message, signature, publicKey, 'keccak', isExpanded)
]
];
const CRYPTO_TYPES: KeypairType[] = [
'ed25519',
'sr25519',
'ecdsa',
'ethereum'
const VERIFIERS: Verifier[] = [
['ed25519', naclVerify],
['sr25519', schnorrkelVerify],
...VERIFIERS_ECDSA
];
function verifyDetect (result: VerifyResult, message: Uint8Array | string, signature: Uint8Array, publicKey: Uint8Array): VerifyResult {
result.isValid = VERIFIERS.some(([crypto, verify]): boolean => {
const CRYPTO_TYPES: ('ed25519' | 'sr25519' | 'ecdsa')[] = ['ed25519', 'sr25519', 'ecdsa'];
function verifyDetect (result: VerifyResult, message: Uint8Array | string, signature: Uint8Array, publicKey: Uint8Array, isExpanded?: boolean, verifiers = VERIFIERS): VerifyResult {
result.isValid = verifiers.some(([crypto, verify]): boolean => {
try {
if (verify(message, signature, publicKey)) {
if (verify(message, signature, publicKey, isExpanded)) {
result.crypto = crypto;
return true;
@@ -50,19 +51,20 @@ function verifyDetect (result: VerifyResult, message: Uint8Array | string, signa
return result;
}
function verifyMultisig (result: VerifyResult, message: Uint8Array | string, signature: Uint8Array, publicKey: Uint8Array): VerifyResult {
function verifyMultisig (result: VerifyResult, message: Uint8Array | string, signature: Uint8Array, publicKey: Uint8Array, isExpanded?: boolean): VerifyResult {
assert([0, 1, 2].includes(signature[0]), `Unknown crypto type, expected signature prefix [0..2], found ${signature[0]}`);
result.crypto = CRYPTO_TYPES[signature[0]] || 'none';
const type = CRYPTO_TYPES[signature[0]] || 'none';
result.crypto = type;
try {
result.isValid = {
ecdsa: (): boolean => secp256k1Verify(message, signature.subarray(1), publicKey, 'blake2'),
ed25519: (): boolean => naclVerify(message, signature.subarray(1), publicKey),
ethereum: (): boolean => secp256k1Verify(message, signature.subarray(1), publicKey, 'keccak'),
none: (): boolean => { throw Error('no verify for `none` crypto type'); },
sr25519: (): boolean => schnorrkelVerify(message, signature.subarray(1), publicKey)
}[result.crypto]();
ecdsa: () => verifyDetect(result, message, signature.subarray(1), publicKey, isExpanded, VERIFIERS_ECDSA).isValid,
ed25519: () => naclVerify(message, signature.subarray(1), publicKey),
none: () => { throw Error('no verify for `none` crypto type'); },
sr25519: () => schnorrkelVerify(message, signature.subarray(1), publicKey)
}[type]();
} catch (error) {
// ignore, result.isValid still set to false
}
@@ -70,18 +72,15 @@ function verifyMultisig (result: VerifyResult, message: Uint8Array | string, sig
return result;
}
export default function signatureVerify (message: Uint8Array | string, signature: Uint8Array | string, addressOrPublicKey: Uint8Array | string): VerifyResult {
export default function signatureVerify (message: Uint8Array | string, signature: Uint8Array | string, addressOrPublicKey: Uint8Array | string, isExpanded?: boolean): VerifyResult {
const signatureU8a = u8aToU8a(signature);
assert([64, 65, 66].includes(signatureU8a.length), `Invalid signature length, expected [64..66] bytes, found ${signatureU8a.length}`);
const result: VerifyResult = { crypto: 'none', isValid: false };
const publicKey = addressDecode(addressOrPublicKey);
const isMultisig = [0, 1, 2].includes(signatureU8a[0]) && [65, 66].includes(signatureU8a.length);
if (isMultisig) {
return verifyMultisig(result, message, signatureU8a, publicKey);
} else {
return verifyDetect(result, message, signatureU8a, publicKey);
}
return [0, 1, 2].includes(signatureU8a[0]) && [65, 66].includes(signatureU8a.length)
? verifyMultisig(result, message, signatureU8a, publicKey, isExpanded)
: verifyDetect(result, message, signatureU8a, publicKey, isExpanded);
}
+498 -4364
View File
File diff suppressed because it is too large Load Diff