From 6f6db63580517b1d766540d671995fa8cd487a95 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 20 Jan 2021 14:11:50 +0100 Subject: [PATCH] Add vrf{Sign, Verify} to kering interface (#846) --- packages/keyring/src/pair/index.spec.ts | 11 ++++++++++ packages/keyring/src/pair/index.ts | 27 ++++++++++++++++++++++--- packages/keyring/src/pair/nobody.ts | 6 ++++++ packages/keyring/src/types.ts | 2 ++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/packages/keyring/src/pair/index.spec.ts b/packages/keyring/src/pair/index.spec.ts index b51c84353..58f2c3b2f 100644 --- a/packages/keyring/src/pair/index.spec.ts +++ b/packages/keyring/src/pair/index.spec.ts @@ -56,6 +56,17 @@ describe('pair', (): void => { ).toEqual(false); }); + it('allows vrf sign and verify', (): void => { + const message = new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]); + + expect( + keyring.alice.vrfVerify( + message, + keyring.alice.vrfSign(message) + ) + ).toBe(true); + }); + it('allows setting/getting of meta', (): void => { keyring.bob.setMeta({ foo: 'bar', something: 'else' }); diff --git a/packages/keyring/src/pair/index.ts b/packages/keyring/src/pair/index.ts index 3a86f33d1..c9bea41e5 100644 --- a/packages/keyring/src/pair/index.ts +++ b/packages/keyring/src/pair/index.ts @@ -5,8 +5,8 @@ import type { Keypair, KeypairType } from '@polkadot/util-crypto/types'; import type { KeyringPair, KeyringPair$Json, KeyringPair$JsonEncodingTypes, KeyringPair$Meta, SignOptions } from '../types'; import type { PairInfo } from './types'; -import { assert, u8aConcat, u8aToHex } from '@polkadot/util'; -import { blake2AsU8a, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, secp256k1Compress, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify } from '@polkadot/util-crypto'; +import { assert, u8aConcat, u8aEq, u8aToHex } from '@polkadot/util'; +import { blake2AsU8a, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclSign, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVrfSign, schnorrkelVrfVerify, secp256k1Compress, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify } from '@polkadot/util-crypto'; import { decodePair } from './decode'; import { encodePair } from './encode'; @@ -178,6 +178,27 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P return pairToJson(type, { address, meta }, recode(passphrase), !!passphrase); }, verify: (message: Uint8Array, signature: Uint8Array): boolean => - signatureVerify(message, signature, TYPE_ADDRESS[type](publicKey)).isValid + signatureVerify(message, signature, TYPE_ADDRESS[type](publicKey)).isValid, + vrfSign: (message: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array => { + assert(!isLocked(secretKey), 'Cannot sign with a locked key pair'); + + if (type === 'sr25519') { + return schnorrkelVrfSign(message, { secretKey }, context, extra); + } + + const proof = TYPE_SIGNATURE[type](message, { publicKey, secretKey }); + + return u8aConcat( + blake2AsU8a(u8aConcat(context || '', extra || '', proof)), + proof + ); + }, + vrfVerify: (message: Uint8Array, vrfResult: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): boolean => + type === 'sr25519' + ? schnorrkelVrfVerify(message, vrfResult, publicKey, context, extra) + : ( + signatureVerify(message, vrfResult.subarray(32), TYPE_ADDRESS[type](publicKey)).isValid && + u8aEq(vrfResult.subarray(0, 32), blake2AsU8a(u8aConcat(context || '', extra || '', vrfResult.subarray(32)))) + ) }; } diff --git a/packages/keyring/src/pair/nobody.ts b/packages/keyring/src/pair/nobody.ts index bea52d010..34673cbae 100644 --- a/packages/keyring/src/pair/nobody.ts +++ b/packages/keyring/src/pair/nobody.ts @@ -53,6 +53,12 @@ export function nobody (): KeyringPair { type: 'ed25519', // eslint-disable-next-line @typescript-eslint/no-unused-vars verify: (message: Uint8Array, signature: Uint8Array): boolean => + false, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + vrfSign: (message: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array => + new Uint8Array(96), + // eslint-disable-next-line @typescript-eslint/no-unused-vars + vrfVerify: (message: Uint8Array, vrfResult: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): boolean => false }; diff --git a/packages/keyring/src/types.ts b/packages/keyring/src/types.ts index 971cf0d71..8aa774e68 100644 --- a/packages/keyring/src/types.ts +++ b/packages/keyring/src/types.ts @@ -48,6 +48,8 @@ export interface KeyringPair { sign (message: Uint8Array, options?: SignOptions): Uint8Array; toJson (passphrase?: string): KeyringPair$Json; verify (message: Uint8Array, signature: Uint8Array): boolean; + vrfSign (message: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): Uint8Array; + vrfVerify (message: Uint8Array, vrfResult: Uint8Array, context?: string | Uint8Array, extra?: string | Uint8Array): boolean; } export interface KeyringPairs {