Rename {ed, sr}25519 functions (#1237)
* Rename {ed, sr}25519 functions
* Finalize renames
* Fix wasm imports
* CHANGELOG
This commit is contained in:
@@ -4,8 +4,11 @@
|
||||
|
||||
**Important** In the 8.0 version the hashing utilities have been consolidated around an optimized and audited version. The happy-path with WASM-first is in-place and these fallbacks won't be used when `BigInt` is not available in your environment (as required by the new hashing implementations).
|
||||
|
||||
**Breaking change** The `schnorrkel*` functions have been renamed to `sr25519*`. Likewise the `ed25519` operations have been renamed from `nacl*` to `ed25519*`. Additionally the `{ed, sr}25519KeyPairFrom*` functions have been renamed to `{ed, sr}25519PairFrom*`.
|
||||
|
||||
Changes:
|
||||
|
||||
- Introduce `ed25519*` & `sr25519*` functions
|
||||
- Replace camelcase dep with simplified/optimized version
|
||||
- Replace (most) hashing operations with `@noble/hashes`
|
||||
- Replace JS fallback for `xxhash`
|
||||
|
||||
@@ -5,17 +5,17 @@ import type { EncryptedJsonEncoding, Keypair, KeypairType } from '@polkadot/util
|
||||
import type { KeyringInstance, KeyringOptions, KeyringPair, KeyringPair$Json, KeyringPair$Meta } from './types';
|
||||
|
||||
import { assert, hexToU8a, isHex, isUndefined, stringToU8a } from '@polkadot/util';
|
||||
import { base64Decode, decodeAddress, encodeAddress, ethereumEncode, hdEthereum, keyExtractSuri, keyFromPath, mnemonicToLegacySeed, mnemonicToMiniSecret, naclKeypairFromSeed as naclFromSeed, schnorrkelKeypairFromSeed as schnorrkelFromSeed, secp256k1KeypairFromSeed as secp256k1FromSeed } from '@polkadot/util-crypto';
|
||||
import { base64Decode, decodeAddress, ed25519PairFromSeed as ed25519FromSeed, encodeAddress, ethereumEncode, hdEthereum, keyExtractSuri, keyFromPath, mnemonicToLegacySeed, mnemonicToMiniSecret, secp256k1PairFromSeed as secp256k1FromSeed, sr25519PairFromSeed as sr25519FromSeed } from '@polkadot/util-crypto';
|
||||
|
||||
import { DEV_PHRASE } from './defaults';
|
||||
import { createPair } from './pair';
|
||||
import { Pairs } from './pairs';
|
||||
|
||||
const keypairFromSeed = {
|
||||
const PairFromSeed = {
|
||||
ecdsa: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
|
||||
ed25519: (seed: Uint8Array): Keypair => naclFromSeed(seed),
|
||||
ed25519: (seed: Uint8Array): Keypair => ed25519FromSeed(seed),
|
||||
ethereum: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
|
||||
sr25519: (seed: Uint8Array): Keypair => schnorrkelFromSeed(seed)
|
||||
sr25519: (seed: Uint8Array): Keypair => sr25519FromSeed(seed)
|
||||
};
|
||||
|
||||
function pairToPublic ({ publicKey }: KeyringPair): Uint8Array {
|
||||
@@ -142,7 +142,7 @@ export class Keyring implements KeyringInstance {
|
||||
*/
|
||||
public addFromSeed (seed: Uint8Array, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair {
|
||||
return this.addPair(
|
||||
createPair({ toSS58: this.encodeAddress, type }, keypairFromSeed[type](seed), meta, null)
|
||||
createPair({ toSS58: this.encodeAddress, type }, PairFromSeed[type](seed), meta, null)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -224,9 +224,9 @@ export class Keyring implements KeyringInstance {
|
||||
|
||||
const derived = type === 'ethereum'
|
||||
? isPhraseHex
|
||||
? keypairFromSeed[type](seed) // for eth, if the private key is provided as suri, it must be derived only once
|
||||
? PairFromSeed[type](seed) // for eth, if the private key is provided as suri, it must be derived only once
|
||||
: hdEthereum(seed, derivePath.substring(1))
|
||||
: keyFromPath(keypairFromSeed[type](seed), path, type);
|
||||
: keyFromPath(PairFromSeed[type](seed), path, type);
|
||||
|
||||
return createPair({ toSS58: this.encodeAddress, type }, derived, meta, null);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta, SignOptions } fro
|
||||
import type { PairInfo } from './types';
|
||||
|
||||
import { assert, objectSpread, u8aConcat, u8aEmpty, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { blake2AsU8a, convertPublicKeyToCurve25519, convertSecretKeyToCurve25519, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclKeypairFromSeed as naclFromSeed, naclOpen, naclSeal, naclSign, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVrfSign, schnorrkelVrfVerify, secp256k1Compress, secp256k1Expand, secp256k1KeypairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify } from '@polkadot/util-crypto';
|
||||
import { blake2AsU8a, convertPublicKeyToCurve25519, convertSecretKeyToCurve25519, ed25519PairFromSeed as ed25519FromSeed, ed25519Sign, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, naclOpen, naclSeal, secp256k1Compress, secp256k1Expand, secp256k1PairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify, sr25519PairFromSeed as sr25519FromSeed, sr25519Sign, sr25519VrfSign, sr25519VrfVerify } from '@polkadot/util-crypto';
|
||||
|
||||
import { decodePair } from './decode';
|
||||
import { encodePair } from './encode';
|
||||
@@ -22,9 +22,9 @@ const SIG_TYPE_NONE = new Uint8Array();
|
||||
|
||||
const TYPE_FROM_SEED = {
|
||||
ecdsa: secp256k1FromSeed,
|
||||
ed25519: naclFromSeed,
|
||||
ed25519: ed25519FromSeed,
|
||||
ethereum: secp256k1FromSeed,
|
||||
sr25519: schnorrkelFromSeed
|
||||
sr25519: sr25519FromSeed
|
||||
};
|
||||
|
||||
const TYPE_PREFIX = {
|
||||
@@ -36,9 +36,9 @@ const TYPE_PREFIX = {
|
||||
|
||||
const TYPE_SIGNATURE = {
|
||||
ecdsa: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'blake2'),
|
||||
ed25519: naclSign,
|
||||
ed25519: ed25519Sign,
|
||||
ethereum: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'keccak'),
|
||||
sr25519: schnorrkelSign
|
||||
sr25519: sr25519Sign
|
||||
};
|
||||
|
||||
const TYPE_ADDRESS = {
|
||||
@@ -215,7 +215,7 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P
|
||||
assert(!isLocked(secretKey), 'Cannot sign with a locked key pair');
|
||||
|
||||
if (type === 'sr25519') {
|
||||
return schnorrkelVrfSign(message, { secretKey }, context, extra);
|
||||
return sr25519VrfSign(message, { secretKey }, context, extra);
|
||||
}
|
||||
|
||||
const proof = TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey });
|
||||
@@ -224,7 +224,7 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P
|
||||
},
|
||||
vrfVerify: (message: HexString | string | Uint8Array, vrfResult: Uint8Array, signerPublic: HexString | Uint8Array | string, context?: HexString | string | Uint8Array, extra?: HexString | string | Uint8Array): boolean => {
|
||||
if (type === 'sr25519') {
|
||||
return schnorrkelVrfVerify(message, vrfResult, publicKey, context, extra);
|
||||
return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
|
||||
}
|
||||
|
||||
const result = signatureVerify(message, u8aConcat(TYPE_PREFIX[type], vrfResult.subarray(32)), TYPE_ADDRESS[type](u8aToU8a(signerPublic)));
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { KeyringPair } from '../types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { cryptoWaitReady, encodeAddress as toSS58, naclKeypairFromSeed, randomAsU8a, schnorrkelKeypairFromSeed, secp256k1KeypairFromSeed } from '@polkadot/util-crypto';
|
||||
import { cryptoWaitReady, ed25519PairFromSeed, encodeAddress as toSS58, randomAsU8a, secp256k1PairFromSeed, sr25519PairFromSeed } from '@polkadot/util-crypto';
|
||||
|
||||
import { createPair } from '.';
|
||||
|
||||
@@ -19,9 +19,9 @@ describe('vrf', (): void => {
|
||||
beforeAll(async (): Promise<void> => {
|
||||
await cryptoWaitReady();
|
||||
|
||||
ecdsa = createPair({ toSS58, type: 'ecdsa' }, secp256k1KeypairFromSeed(randomAsU8a()));
|
||||
ed25519 = createPair({ toSS58, type: 'ed25519' }, naclKeypairFromSeed(randomAsU8a()));
|
||||
sr25519 = createPair({ toSS58, type: 'sr25519' }, schnorrkelKeypairFromSeed(randomAsU8a()));
|
||||
ecdsa = createPair({ toSS58, type: 'ecdsa' }, secp256k1PairFromSeed(randomAsU8a()));
|
||||
ed25519 = createPair({ toSS58, type: 'ed25519' }, ed25519PairFromSeed(randomAsU8a()));
|
||||
sr25519 = createPair({ toSS58, type: 'sr25519' }, sr25519PairFromSeed(randomAsU8a()));
|
||||
});
|
||||
|
||||
it('has deterministic signature values for ecdsa', (): void => {
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { Prefix } from './types';
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { keyExtractPath } from '../key';
|
||||
import { schnorrkelDerivePublic } from '../schnorrkel';
|
||||
import { sr25519DerivePublic } from '../sr25519';
|
||||
import { decodeAddress } from './decode';
|
||||
import { encodeAddress } from './encode';
|
||||
|
||||
@@ -30,7 +30,7 @@ export function deriveAddress (who: HexString | Uint8Array | string, suri: strin
|
||||
let publicKey = decodeAddress(who);
|
||||
|
||||
for (const { chainCode } of path) {
|
||||
publicKey = schnorrkelDerivePublic(publicKey, chainCode);
|
||||
publicKey = sr25519DerivePublic(publicKey, chainCode);
|
||||
}
|
||||
|
||||
return encodeAddress(publicKey, ss58Format);
|
||||
|
||||
@@ -12,6 +12,7 @@ export * from './base58';
|
||||
export * from './base64';
|
||||
export * from './blake2';
|
||||
export * from './crypto';
|
||||
export * from './ed25519';
|
||||
export * from './ethereum';
|
||||
export * from './hd';
|
||||
export * from './hmac';
|
||||
@@ -23,9 +24,9 @@ export * from './networks';
|
||||
export * from './nacl';
|
||||
export * from './pbkdf2';
|
||||
export * from './random';
|
||||
export * from './schnorrkel';
|
||||
export * from './scrypt';
|
||||
export * from './secp256k1';
|
||||
export * from './sha';
|
||||
export * from './signature';
|
||||
export * from './sr25519';
|
||||
export * from './xxhash';
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import { blake2AsU8a } from '../blake2/asU8a';
|
||||
|
||||
const HDKD = compactAddLength(stringToU8a('Ed25519HDKD'));
|
||||
|
||||
export function naclDeriveHard (seed: Uint8Array, chainCode: Uint8Array): Uint8Array {
|
||||
export function ed25519DeriveHard (seed: Uint8Array, chainCode: Uint8Array): Uint8Array {
|
||||
assert(isU8a(chainCode) && chainCode.length === 32, 'Invalid chainCode passed to derive');
|
||||
|
||||
return blake2AsU8a(
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Implements ed25519 operations
|
||||
*/
|
||||
export { convertPublicKeyToCurve25519, convertSecretKeyToCurve25519 } from './convertKey';
|
||||
export { ed25519DeriveHard } from './deriveHard';
|
||||
export { ed25519PairFromRandom } from './keypair/fromRandom';
|
||||
export { ed25519PairFromSecret } from './keypair/fromSecret';
|
||||
export { ed25519PairFromSeed } from './keypair/fromSeed';
|
||||
export { ed25519PairFromString } from './keypair/fromString';
|
||||
export { ed25519Sign } from './sign';
|
||||
export { ed25519Verify } from './verify';
|
||||
+3
-3
@@ -3,13 +3,13 @@
|
||||
|
||||
import type { Keypair } from '../../types';
|
||||
|
||||
import { naclKeypairFromRandom } from '..';
|
||||
import { ed25519PairFromRandom } from '..';
|
||||
|
||||
describe('naclKeypairFromRandom', (): void => {
|
||||
describe('ed25519PairFromRandom', (): void => {
|
||||
let keypair: Keypair;
|
||||
|
||||
beforeEach((): void => {
|
||||
keypair = naclKeypairFromRandom();
|
||||
keypair = ed25519PairFromRandom();
|
||||
});
|
||||
|
||||
it('generates a valid publicKey', (): void => {
|
||||
+4
-4
@@ -6,7 +6,7 @@ import type { Keypair } from '../../types';
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclKeypairFromRandom
|
||||
* @name ed25519PairFromRandom
|
||||
* @summary Creates a new public/secret keypair.
|
||||
* @description
|
||||
* Returns a new generate object containing a `publicKey` & `secretKey`.
|
||||
@@ -14,11 +14,11 @@ import nacl from 'tweetnacl';
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclKeypairFromRandom } from '@polkadot/util-crypto';
|
||||
* import { ed25519PairFromRandom } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclKeypairFromRandom(); // => { secretKey: [...], publicKey: [...] }
|
||||
* ed25519PairFromRandom(); // => { secretKey: [...], publicKey: [...] }
|
||||
* ```
|
||||
*/
|
||||
export function naclKeypairFromRandom (): Keypair {
|
||||
export function ed25519PairFromRandom (): Keypair {
|
||||
return nacl.sign.keyPair();
|
||||
}
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { naclKeypairFromSecret } from '..';
|
||||
import { ed25519PairFromSecret } from '..';
|
||||
|
||||
describe('naclKeypairFromSecret', (): void => {
|
||||
describe('ed25519PairFromSecret', (): void => {
|
||||
const secretKey = new Uint8Array([
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18,
|
||||
@@ -17,7 +17,7 @@ describe('naclKeypairFromSecret', (): void => {
|
||||
|
||||
it('generates a valid publicKey/secretKey pair', (): void => {
|
||||
expect(
|
||||
naclKeypairFromSecret(secretKey)
|
||||
ed25519PairFromSecret(secretKey)
|
||||
).toEqual({
|
||||
publicKey: new Uint8Array([
|
||||
180, 114, 93, 155, 165, 255, 217, 82,
|
||||
+4
-4
@@ -6,7 +6,7 @@ import type { Keypair } from '../../types';
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclKeypairFromSecret
|
||||
* @name ed25519PairFromSecret
|
||||
* @summary Creates a new public/secret keypair from a secret.
|
||||
* @description
|
||||
* Returns a object containing a `publicKey` & `secretKey` generated from the supplied secret.
|
||||
@@ -14,11 +14,11 @@ import nacl from 'tweetnacl';
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclKeypairFromSecret } from '@polkadot/util-crypto';
|
||||
* import { ed25519PairFromSecret } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclKeypairFromSecret(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* ed25519PairFromSecret(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* ```
|
||||
*/
|
||||
export function naclKeypairFromSecret (secret: Uint8Array): Keypair {
|
||||
export function ed25519PairFromSecret (secret: Uint8Array): Keypair {
|
||||
return nacl.sign.keyPair.fromSecretKey(secret);
|
||||
}
|
||||
+3
-3
@@ -4,9 +4,9 @@
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { naclKeypairFromSeed } from '..';
|
||||
import { ed25519PairFromSeed } from '..';
|
||||
|
||||
describe('naclKeypairFromSeed', (): void => {
|
||||
describe('ed25519PairFromSeed', (): void => {
|
||||
// NOTE: Aligned with Rust test, b"12345678901234567890123456789012"
|
||||
const TEST = stringToU8a('12345678901234567890123456789012');
|
||||
const RESULT = {
|
||||
@@ -33,7 +33,7 @@ describe('naclKeypairFromSeed', (): void => {
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
|
||||
[true, false].forEach((onlyJs): void => {
|
||||
expect(
|
||||
naclKeypairFromSeed(TEST, onlyJs)
|
||||
ed25519PairFromSeed(TEST, onlyJs)
|
||||
).toEqual(RESULT);
|
||||
});
|
||||
});
|
||||
+4
-4
@@ -8,7 +8,7 @@ import nacl from 'tweetnacl';
|
||||
import { ed25519KeypairFromSeed, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name naclKeypairFromSeed
|
||||
* @name ed25519PairFromSeed
|
||||
* @summary Creates a new public/secret keypair from a seed.
|
||||
* @description
|
||||
* Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
@@ -16,12 +16,12 @@ import { ed25519KeypairFromSeed, isReady } from '@polkadot/wasm-crypto';
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclKeypairFromSeed } from '@polkadot/util-crypto';
|
||||
* import { ed25519PairFromSeed } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclKeypairFromSeed(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* ed25519PairFromSeed(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* ```
|
||||
*/
|
||||
export function naclKeypairFromSeed (seed: Uint8Array, onlyJs?: boolean): Keypair {
|
||||
export function ed25519PairFromSeed (seed: Uint8Array, onlyJs?: boolean): Keypair {
|
||||
if (!onlyJs && isReady()) {
|
||||
const full = ed25519KeypairFromSeed(seed);
|
||||
|
||||
+3
-3
@@ -1,12 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { naclKeypairFromString } from '..';
|
||||
import { ed25519PairFromString } from '..';
|
||||
|
||||
describe('naclKeypairFromSeed', (): void => {
|
||||
describe('ed25519PairFromSeed', (): void => {
|
||||
it('generates a valid publicKey/secretKey pair', (): void => {
|
||||
expect(
|
||||
naclKeypairFromString('test')
|
||||
ed25519PairFromString('test')
|
||||
).toEqual({
|
||||
publicKey: new Uint8Array([188, 108, 179, 142, 36, 142, 76, 87, 77, 193, 147, 139, 254, 110, 196, 217, 117, 233, 167, 165, 250, 150, 247, 237, 198, 68, 129, 4, 211, 209, 136, 48]),
|
||||
secretKey: new Uint8Array([146, 139, 32, 54, 105, 67, 226, 175, 209, 30, 188, 14, 174, 46, 83, 169, 59, 241, 119, 164, 252, 243, 91, 204, 100, 213, 3, 112, 78, 101, 226, 2, 188, 108, 179, 142, 36, 142, 76, 87, 77, 193, 147, 139, 254, 110, 196, 217, 117, 233, 167, 165, 250, 150, 247, 237, 198, 68, 129, 4, 211, 209, 136, 48])
|
||||
+6
-6
@@ -6,10 +6,10 @@ import type { Keypair } from '../../types';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
import { blake2AsU8a } from '../../blake2/asU8a';
|
||||
import { naclKeypairFromSeed } from './fromSeed';
|
||||
import { ed25519PairFromSeed } from './fromSeed';
|
||||
|
||||
/**
|
||||
* @name naclKeypairFromString
|
||||
* @name ed25519PairFromString
|
||||
* @summary Creates a new public/secret keypair from a string.
|
||||
* @description
|
||||
* Returns a object containing a `publicKey` & `secretKey` generated from the supplied string. The string is hashed and the value used as the input seed.
|
||||
@@ -17,13 +17,13 @@ import { naclKeypairFromSeed } from './fromSeed';
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclKeypairFromString } from '@polkadot/util-crypto';
|
||||
* import { ed25519PairFromString } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclKeypairFromString('test'); // => { secretKey: [...], publicKey: [...] }
|
||||
* ed25519PairFromString('test'); // => { secretKey: [...], publicKey: [...] }
|
||||
* ```
|
||||
*/
|
||||
export function naclKeypairFromString (value: string): Keypair {
|
||||
return naclKeypairFromSeed(
|
||||
export function ed25519PairFromString (value: string): Keypair {
|
||||
return ed25519PairFromSeed(
|
||||
blake2AsU8a(
|
||||
stringToU8a(value),
|
||||
256
|
||||
+6
-6
@@ -5,13 +5,13 @@ import { stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceWasm } from '../../test/performance';
|
||||
import { naclKeypairFromSeed, naclSign } from '.';
|
||||
import { ed25519PairFromSeed, ed25519Sign } from '.';
|
||||
|
||||
const PAIR = naclKeypairFromSeed(
|
||||
const PAIR = ed25519PairFromSeed(
|
||||
stringToU8a('12345678901234567890123456789012')
|
||||
);
|
||||
|
||||
describe('naclSign', (): void => {
|
||||
describe('ed25519Sign', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
@@ -19,7 +19,7 @@ describe('naclSign', (): void => {
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('returns a valid signature for the message', (): void => {
|
||||
expect(
|
||||
naclSign(
|
||||
ed25519Sign(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
PAIR,
|
||||
onlyJs
|
||||
@@ -30,7 +30,7 @@ describe('naclSign', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
performanceWasm('naclSign', 250, (input, onlyJs) =>
|
||||
naclSign(input, PAIR, onlyJs)
|
||||
performanceWasm('ed25519Sign', 250, (input, onlyJs) =>
|
||||
ed25519Sign(input, PAIR, onlyJs)
|
||||
);
|
||||
});
|
||||
@@ -7,10 +7,10 @@ import type { Keypair } from '../types';
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { ed25519Sign, isReady } from '@polkadot/wasm-crypto';
|
||||
import { ed25519Sign as wasmSign, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name naclSign
|
||||
* @name ed25519Sign
|
||||
* @summary Signs a message using the supplied secretKey
|
||||
* @description
|
||||
* Returns message signature of `message`, using the `secretKey`.
|
||||
@@ -18,17 +18,17 @@ import { ed25519Sign, isReady } from '@polkadot/wasm-crypto';
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclSign } from '@polkadot/util-crypto';
|
||||
* import { ed25519Sign } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclSign([...], [...]); // => [...]
|
||||
* ed25519Sign([...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function naclSign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>, onlyJs?: boolean): Uint8Array {
|
||||
export function ed25519Sign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>, onlyJs?: boolean): Uint8Array {
|
||||
assert(secretKey, 'Expected a valid secretKey');
|
||||
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
return !onlyJs && isReady()
|
||||
? ed25519Sign(publicKey as Uint8Array, secretKey.subarray(0, 32), messageU8a)
|
||||
? wasmSign(publicKey as Uint8Array, secretKey.subarray(0, 32), messageU8a)
|
||||
: nacl.sign.detached(messageU8a, secretKey);
|
||||
}
|
||||
+8
-8
@@ -4,16 +4,16 @@
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { naclKeypairFromSeed, naclVerify } from '.';
|
||||
import { ed25519PairFromSeed, ed25519Verify } from '.';
|
||||
|
||||
describe('naclVerify', (): void => {
|
||||
describe('ed25519Verify', (): void => {
|
||||
let publicKey: Uint8Array;
|
||||
let signature: Uint8Array;
|
||||
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
|
||||
publicKey = naclKeypairFromSeed(
|
||||
publicKey = ed25519PairFromSeed(
|
||||
stringToU8a('12345678901234567890123456789012')
|
||||
).publicKey;
|
||||
signature = new Uint8Array([28, 58, 206, 239, 249, 70, 59, 191, 166, 40, 219, 218, 235, 170, 25, 79, 10, 94, 9, 197, 34, 126, 1, 150, 246, 68, 28, 238, 36, 26, 172, 163, 168, 90, 202, 211, 126, 246, 57, 212, 43, 24, 88, 197, 240, 113, 118, 76, 37, 81, 91, 110, 236, 50, 144, 134, 100, 223, 220, 238, 34, 185, 211, 7]);
|
||||
@@ -22,7 +22,7 @@ describe('naclVerify', (): void => {
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('validates a correctly signed message', (): void => {
|
||||
expect(
|
||||
naclVerify(
|
||||
ed25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
signature,
|
||||
publicKey,
|
||||
@@ -33,7 +33,7 @@ describe('naclVerify', (): void => {
|
||||
|
||||
it('fails a correctly signed message (message changed)', (): void => {
|
||||
expect(
|
||||
naclVerify(
|
||||
ed25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]),
|
||||
signature,
|
||||
publicKey,
|
||||
@@ -46,7 +46,7 @@ describe('naclVerify', (): void => {
|
||||
signature[0] = 0xff;
|
||||
|
||||
expect(
|
||||
naclVerify(
|
||||
ed25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
signature,
|
||||
publicKey,
|
||||
@@ -57,7 +57,7 @@ describe('naclVerify', (): void => {
|
||||
|
||||
it('throws error when publicKey lengths do not match', (): void => {
|
||||
expect(
|
||||
() => naclVerify(
|
||||
() => ed25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
signature,
|
||||
new Uint8Array([1, 2]),
|
||||
@@ -68,7 +68,7 @@ describe('naclVerify', (): void => {
|
||||
|
||||
it('throws error when signature lengths do not match', (): void => {
|
||||
expect(
|
||||
() => naclVerify(
|
||||
() => ed25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array([1, 2]),
|
||||
publicKey,
|
||||
+6
-6
@@ -6,10 +6,10 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { ed25519Verify, isReady } from '@polkadot/wasm-crypto';
|
||||
import { ed25519Verify as wasmVerify, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name naclSign
|
||||
* @name ed25519Sign
|
||||
* @summary Verifies the signature on the supplied message.
|
||||
* @description
|
||||
* Verifies the `signature` on `message` with the supplied `publicKey`. Returns `true` on sucess, `false` otherwise.
|
||||
@@ -17,12 +17,12 @@ import { ed25519Verify, isReady } from '@polkadot/wasm-crypto';
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclVerify } from '@polkadot/util-crypto';
|
||||
* import { ed25519Verify } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclVerify([...], [...], [...]); // => true/false
|
||||
* ed25519Verify([...], [...], [...]); // => true/false
|
||||
* ```
|
||||
*/
|
||||
export function naclVerify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string, onlyJs?: boolean): boolean {
|
||||
export function ed25519Verify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string, onlyJs?: boolean): boolean {
|
||||
const messageU8a = u8aToU8a(message);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
@@ -31,6 +31,6 @@ export function naclVerify (message: HexString | Uint8Array | string, signature:
|
||||
assert(signatureU8a.length === 64, () => `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
|
||||
return !onlyJs && isReady()
|
||||
? ed25519Verify(signatureU8a, messageU8a, publicKeyU8a)
|
||||
? wasmVerify(signatureU8a, messageU8a, publicKeyU8a)
|
||||
: nacl.sign.detached.verify(messageU8a, signatureU8a, publicKeyU8a);
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import { assert, bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { BN_BE_32_OPTS } from '../../bn';
|
||||
import { hmacShaAsU8a } from '../../hmac';
|
||||
import { secp256k1KeypairFromSeed, secp256k1PrivateKeyTweakAdd } from '../../secp256k1';
|
||||
import { secp256k1PairFromSeed, secp256k1PrivateKeyTweakAdd } from '../../secp256k1';
|
||||
import { HARDENED, hdValidatePath } from '../validatePath';
|
||||
|
||||
interface CodedKeypair extends Keypair {
|
||||
@@ -19,7 +19,7 @@ const MASTER_SECRET = stringToU8a('Bitcoin seed');
|
||||
function createCoded (secretKey: Uint8Array, chainCode: Uint8Array): CodedKeypair {
|
||||
return {
|
||||
chainCode,
|
||||
publicKey: secp256k1KeypairFromSeed(secretKey).publicKey,
|
||||
publicKey: secp256k1PairFromSeed(secretKey).publicKey,
|
||||
secretKey
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import type { Keypair } from '../../types';
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { ed25519PairFromSeed } from '../../ed25519';
|
||||
import { mnemonicValidate } from '../../mnemonic';
|
||||
import { naclKeypairFromSeed } from '../../nacl';
|
||||
import { HARDENED, hdValidatePath } from '../validatePath';
|
||||
import { ledgerDerivePrivate } from './derivePrivate';
|
||||
import { ledgerMaster } from './master';
|
||||
@@ -35,5 +35,5 @@ export function hdLedger (_mnemonic: string, path: string): Keypair {
|
||||
seed = ledgerDerivePrivate(seed, (n < HARDENED) ? (n + HARDENED) : n);
|
||||
}
|
||||
|
||||
return naclKeypairFromSeed(seed.slice(0, 32));
|
||||
return ed25519PairFromSeed(seed.slice(0, 32));
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { secp256k1DeriveHard } from '../secp256k1/deriveHard';
|
||||
import { secp256k1KeypairFromSeed } from '../secp256k1/keypair/fromSeed';
|
||||
import { secp256k1PairFromSeed } from '../secp256k1/keypair/fromSeed';
|
||||
import { createSeedDeriveFn } from './hdkdDerive';
|
||||
|
||||
export const keyHdkdEcdsa = createSeedDeriveFn(secp256k1KeypairFromSeed, secp256k1DeriveHard);
|
||||
export const keyHdkdEcdsa = createSeedDeriveFn(secp256k1PairFromSeed, secp256k1DeriveHard);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { naclDeriveHard } from '../nacl/deriveHard';
|
||||
import { naclKeypairFromSeed } from '../nacl/keypair/fromSeed';
|
||||
import { ed25519DeriveHard, ed25519PairFromSeed } from '../ed25519';
|
||||
import { createSeedDeriveFn } from './hdkdDerive';
|
||||
|
||||
export const keyHdkdEd25519 = createSeedDeriveFn(naclKeypairFromSeed, naclDeriveHard);
|
||||
export const keyHdkdEd25519 = createSeedDeriveFn(ed25519PairFromSeed, ed25519DeriveHard);
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
|
||||
import type { Keypair } from '../types';
|
||||
|
||||
import { schnorrkelDeriveHard } from '../schnorrkel/deriveHard';
|
||||
import { schnorrkelDeriveSoft } from '../schnorrkel/deriveSoft';
|
||||
import { sr25519DeriveHard } from '../sr25519/deriveHard';
|
||||
import { sr25519DeriveSoft } from '../sr25519/deriveSoft';
|
||||
import { DeriveJunction } from './DeriveJunction';
|
||||
|
||||
export function keyHdkdSr25519 (keypair: Keypair, { chainCode, isSoft }: DeriveJunction): Keypair {
|
||||
return isSoft
|
||||
? schnorrkelDeriveSoft(keypair, chainCode)
|
||||
: schnorrkelDeriveHard(keypair, chainCode);
|
||||
? sr25519DeriveSoft(keypair, chainCode)
|
||||
: sr25519DeriveHard(keypair, chainCode);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import tests from '../schnorrkel/keypair/testing';
|
||||
import tests from '../sr25519/keypair/testing';
|
||||
import { cryptoWaitReady } from '..';
|
||||
import { mnemonicToEntropy } from './toEntropy';
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { u8aEq, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import tests from '../schnorrkel/keypair/testing';
|
||||
import tests from '../sr25519/keypair/testing';
|
||||
import { cryptoWaitReady } from '..';
|
||||
import { mnemonicToMiniSecret } from './toMiniSecret';
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { arrayRange, u8aEq } from '@polkadot/util';
|
||||
|
||||
import { cryptoWaitReady, mnemonicGenerate, mnemonicToMiniSecret, naclKeypairFromSeed, schnorrkelKeypairFromSeed } from '..';
|
||||
import { cryptoWaitReady, ed25519PairFromSeed, mnemonicGenerate, mnemonicToMiniSecret, sr25519PairFromSeed } from '..';
|
||||
|
||||
// NOTE: This basically controls how long stuff runs for, YMMV
|
||||
//
|
||||
@@ -29,8 +29,8 @@ describe.each([true, false])('mnemonicToMiniSecret (compare), onlyJsMnemonic=%p'
|
||||
// do iterations to check and re-check that all matches
|
||||
it.concurrent.each(arrayRange(NUM_CHECKS))('check=%p', (count): void => {
|
||||
const minisecret = mnemonicToMiniSecret(mnemonic, count ? `${count}` : '', onlyJsMnemonic);
|
||||
const edpub = naclKeypairFromSeed(minisecret).publicKey;
|
||||
const srpub = schnorrkelKeypairFromSeed(minisecret).publicKey;
|
||||
const edpub = ed25519PairFromSeed(minisecret).publicKey;
|
||||
const srpub = sr25519PairFromSeed(minisecret).publicKey;
|
||||
|
||||
const testmini = mnemonicToMiniSecret(mnemonic, count ? `${count}` : '', onlyJsMini);
|
||||
|
||||
@@ -41,13 +41,13 @@ describe.each([true, false])('mnemonicToMiniSecret (compare), onlyJsMnemonic=%p'
|
||||
|
||||
// compare the sr25519 keypair generated
|
||||
expect(
|
||||
u8aEq(srpub, schnorrkelKeypairFromSeed(testmini).publicKey)
|
||||
u8aEq(srpub, sr25519PairFromSeed(testmini).publicKey)
|
||||
).toEqual(true);
|
||||
|
||||
// compare ed both in WASM and JS
|
||||
[true, false].forEach((onlyJsEd): void => {
|
||||
expect(
|
||||
u8aEq(edpub, naclKeypairFromSeed(testmini, onlyJsEd).publicKey)
|
||||
u8aEq(edpub, ed25519PairFromSeed(testmini, onlyJsEd).publicKey)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { naclBoxKeypairFromSecret } from '..';
|
||||
import { naclBoxPairFromSecret } from '..';
|
||||
|
||||
describe('naclBoxKeypairFromSecret', (): void => {
|
||||
describe('naclBoxPairFromSecret', (): void => {
|
||||
const secretKey = new Uint8Array([
|
||||
18, 52, 86, 120, 144, 18, 52, 86,
|
||||
120, 144, 18, 52, 86, 120, 144, 18,
|
||||
@@ -13,7 +13,7 @@ describe('naclBoxKeypairFromSecret', (): void => {
|
||||
|
||||
it('generates a valid publicKey/secretKey pair', (): void => {
|
||||
expect(
|
||||
naclBoxKeypairFromSecret(secretKey)
|
||||
naclBoxPairFromSecret(secretKey)
|
||||
).toEqual({
|
||||
publicKey: new Uint8Array([
|
||||
206, 110, 228, 222, 155, 43, 229, 254,
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { Keypair } from '../../types';
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
/**
|
||||
* @name naclBoxKeypairFromSecret
|
||||
* @name naclBoxPairFromSecret
|
||||
* @summary Creates a new public/secret box keypair from a secret.
|
||||
* @description
|
||||
* Returns a object containing a box `publicKey` & `secretKey` generated from the supplied secret.
|
||||
@@ -14,11 +14,11 @@ import nacl from 'tweetnacl';
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclBoxKeypairFromSecret } from '@polkadot/util-crypto';
|
||||
* import { naclBoxPairFromSecret } from '@polkadot/util-crypto';
|
||||
*
|
||||
* naclBoxKeypairFromSecret(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* naclBoxPairFromSecret(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* ```
|
||||
*/
|
||||
export function naclBoxKeypairFromSecret (secret: Uint8Array): Keypair {
|
||||
export function naclBoxPairFromSecret (secret: Uint8Array): Keypair {
|
||||
return nacl.box.keyPair.fromSecretKey(secret.slice(0, 32));
|
||||
}
|
||||
|
||||
@@ -2,17 +2,10 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Implements [NaCl](http://nacl.cr.yp.to/) secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures
|
||||
* @summary Implements [NaCl](http://nacl.cr.yp.to/) secret-key authenticated encryption, public-key authenticated encryption
|
||||
*/
|
||||
export { naclDecrypt } from './decrypt';
|
||||
export { naclEncrypt } from './encrypt';
|
||||
export { naclKeypairFromRandom } from './keypair/fromRandom';
|
||||
export { naclKeypairFromSecret } from './keypair/fromSecret';
|
||||
export { naclKeypairFromSeed } from './keypair/fromSeed';
|
||||
export { naclKeypairFromString } from './keypair/fromString';
|
||||
export { naclSign } from './sign';
|
||||
export { naclVerify } from './verify';
|
||||
export { naclBoxKeypairFromSecret } from './box/fromSecret';
|
||||
export { naclBoxPairFromSecret } from './box/fromSecret';
|
||||
export { naclOpen } from './open';
|
||||
export { naclSeal } from './seal';
|
||||
export { convertSecretKeyToCurve25519, convertPublicKeyToCurve25519 } from './convertKey';
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ed25519PairFromString } from '../ed25519';
|
||||
import { keyExtractPath } from '../key';
|
||||
import { keyFromPath } from '../key/fromPath';
|
||||
import { naclBoxKeypairFromSecret, naclKeypairFromString, naclOpen, naclSeal } from '.';
|
||||
import { naclBoxPairFromSecret, naclOpen, naclSeal } from '.';
|
||||
|
||||
describe('naclOpen', (): void => {
|
||||
it('opens a sealed message by the sender', (): void => {
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
const sender = naclKeypairFromString('sender');
|
||||
const receiver = naclKeypairFromString('receiver');
|
||||
const senderBox = naclBoxKeypairFromSecret(sender.secretKey);
|
||||
const receiverBox = naclBoxKeypairFromSecret(receiver.secretKey);
|
||||
const sender = ed25519PairFromString('sender');
|
||||
const receiver = ed25519PairFromString('receiver');
|
||||
const senderBox = naclBoxPairFromSecret(sender.secretKey);
|
||||
const receiverBox = naclBoxPairFromSecret(receiver.secretKey);
|
||||
const { nonce, sealed } = naclSeal(message, senderBox.secretKey, receiverBox.publicKey);
|
||||
|
||||
expect(
|
||||
@@ -22,20 +23,20 @@ describe('naclOpen', (): void => {
|
||||
});
|
||||
|
||||
it('polkadot does double ratchet', () => {
|
||||
const sender = naclKeypairFromString('sender');
|
||||
const receiver = naclKeypairFromString('receiver');
|
||||
const sender = ed25519PairFromString('sender');
|
||||
const receiver = ed25519PairFromString('receiver');
|
||||
|
||||
// Make id key for identifying each other
|
||||
const senderIdKey = keyFromPath(sender, keyExtractPath('//1//1//1//1').path, 'ed25519');
|
||||
const receiverIdKey = keyFromPath(receiver, keyExtractPath('//2//2//2//2').path, 'ed25519');
|
||||
|
||||
// Receiver sends encrypting public key to receive message to decrypt with his private key
|
||||
const receiverIdBoxKey = naclBoxKeypairFromSecret(receiverIdKey.secretKey);
|
||||
const receiverIdBoxKey = naclBoxPairFromSecret(receiverIdKey.secretKey);
|
||||
|
||||
// console.log(`Receiver sends receiver's public key to sender ${receiverIdBoxKey.publicKey.toString()}`);
|
||||
|
||||
// Sender encrypts message to send with the public key the receiver sent and send it to receiver
|
||||
const senderIdBoxKey = naclBoxKeypairFromSecret(senderIdKey.secretKey);
|
||||
const senderIdBoxKey = naclBoxPairFromSecret(senderIdKey.secretKey);
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
const { nonce, sealed } = naclSeal(message, senderIdBoxKey.secretKey, receiverIdBoxKey.publicKey);
|
||||
|
||||
@@ -51,12 +52,12 @@ describe('naclOpen', (): void => {
|
||||
const receiverIdKey2 = keyFromPath(receiver, keyExtractPath('//2//2//2//3').path, 'ed25519');
|
||||
|
||||
// Receiver sends encrypting public key to receive message to decrypt with his private key
|
||||
const receiverIdBoxKey2 = naclBoxKeypairFromSecret(receiverIdKey2.secretKey);
|
||||
const receiverIdBoxKey2 = naclBoxPairFromSecret(receiverIdKey2.secretKey);
|
||||
|
||||
// console.log(`Receiver sends receiver's public key to sender ${receiverIdBoxKey2.publicKey.toString()}`);
|
||||
|
||||
// Sender encrypts message to send with the public key the receiver sent and send it to receiver
|
||||
const senderIdBoxKey2 = naclBoxKeypairFromSecret(senderIdKey2.secretKey);
|
||||
const senderIdBoxKey2 = naclBoxPairFromSecret(senderIdKey2.secretKey);
|
||||
const message2 = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
let ctx = naclSeal(message2, senderIdBoxKey2.secretKey, receiverIdBoxKey2.publicKey);
|
||||
const sealed2 = ctx.sealed;
|
||||
@@ -70,12 +71,12 @@ describe('naclOpen', (): void => {
|
||||
|
||||
// This time derive key for each message to send and receive
|
||||
// Receiver sends encrypting public key to receive message to decrypt with his private key
|
||||
const receiverIdBoxKey2One = naclBoxKeypairFromSecret(receiverIdBoxKey2.secretKey);
|
||||
const receiverIdBoxKey2One = naclBoxPairFromSecret(receiverIdBoxKey2.secretKey);
|
||||
|
||||
// console.log(`Receiver sends receiver's public key to sender ${receiverIdBoxKey2One.publicKey.toString()}`);
|
||||
|
||||
// Sender encrypts message to send with the public key the receiver sent and send it to receiver
|
||||
const senderIdBoxKey2One = naclBoxKeypairFromSecret(senderIdBoxKey2.secretKey);
|
||||
const senderIdBoxKey2One = naclBoxPairFromSecret(senderIdBoxKey2.secretKey);
|
||||
const message3 = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
|
||||
ctx = naclSeal(message3, senderIdBoxKey2One.secretKey, receiverIdBoxKey2One.publicKey);
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { naclBoxKeypairFromSecret, naclKeypairFromString, naclSeal } from '.';
|
||||
import { ed25519PairFromString } from '../ed25519';
|
||||
import { naclBoxPairFromSecret, naclSeal } from '.';
|
||||
|
||||
describe('naclSeal', (): void => {
|
||||
it('seals a message', (): void => {
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
const sender = naclKeypairFromString('sender');
|
||||
const receiver = naclKeypairFromString('receiver');
|
||||
const senderBox = naclBoxKeypairFromSecret(sender.secretKey);
|
||||
const receiverBox = naclBoxKeypairFromSecret(receiver.secretKey);
|
||||
const sender = ed25519PairFromString('sender');
|
||||
const receiver = ed25519PairFromString('receiver');
|
||||
const senderBox = naclBoxPairFromSecret(sender.secretKey);
|
||||
const receiverBox = naclBoxPairFromSecret(receiver.secretKey);
|
||||
const senderBoxSecret = senderBox.secretKey;
|
||||
const receiverBoxPublic = receiverBox.publicKey;
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { schnorrkelAgreement } from './agreement';
|
||||
export { schnorrkelDeriveHard } from './deriveHard';
|
||||
export { schnorrkelDerivePublic } from './derivePublic';
|
||||
export { schnorrkelDeriveSoft } from './deriveSoft';
|
||||
export { schnorrkelKeypairFromSeed } from './keypair/fromSeed';
|
||||
export { schnorrkelSign } from './sign';
|
||||
export { schnorrkelVerify } from './verify';
|
||||
export { schnorrkelVrfSign } from './vrfSign';
|
||||
export { schnorrkelVrfVerify } from './vrfVerify';
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
export { secp256k1Compress } from './compress';
|
||||
export { secp256k1Expand } from './expand';
|
||||
export { secp256k1KeypairFromSeed } from './keypair/fromSeed';
|
||||
export { secp256k1PairFromSeed } from './keypair/fromSeed';
|
||||
export { secp256k1Recover } from './recover';
|
||||
export { secp256k1Sign } from './sign';
|
||||
export { secp256k1PrivateKeyTweakAdd } from './tweakAdd';
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { hexToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { mnemonicToMiniSecret } from '../../mnemonic';
|
||||
import { secp256k1KeypairFromSeed } from '..';
|
||||
import { secp256k1PairFromSeed } from '..';
|
||||
|
||||
// mnemonic, secret, public, account_id
|
||||
type Test = [string, string, string, string];
|
||||
@@ -36,7 +36,7 @@ const tests: Test[] = [
|
||||
]
|
||||
];
|
||||
|
||||
describe('secp256k1KeypairFromSeed', (): void => {
|
||||
describe('secp256k1PairFromSeed', (): void => {
|
||||
const TEST = hexToU8a('0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae');
|
||||
const RESULT = {
|
||||
publicKey: hexToU8a('0x03fd8c74f795ced92064b86191cb2772b1e3a0947740aa0a5a6e379592471fd85b'),
|
||||
@@ -44,13 +44,13 @@ describe('secp256k1KeypairFromSeed', (): void => {
|
||||
};
|
||||
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
|
||||
expect(secp256k1KeypairFromSeed(TEST)).toEqual(RESULT);
|
||||
expect(secp256k1PairFromSeed(TEST)).toEqual(RESULT);
|
||||
});
|
||||
|
||||
tests.forEach(([mnemonic, secretKey, publicKey], index): void => {
|
||||
it(`creates valid against known (${index})`, (): void => {
|
||||
const seed = mnemonicToMiniSecret(mnemonic);
|
||||
const pair = secp256k1KeypairFromSeed(seed);
|
||||
const pair = secp256k1PairFromSeed(seed);
|
||||
|
||||
expect(u8aToHex(pair.secretKey)).toEqual(secretKey);
|
||||
expect(u8aToHex(pair.publicKey)).toEqual(publicKey);
|
||||
|
||||
@@ -9,10 +9,10 @@ import { BN_BE_256_OPTS } from '../../bn';
|
||||
import { secp256k1 } from '../secp256k1';
|
||||
|
||||
/**
|
||||
* @name secp256k1KeypairFromSeed
|
||||
* @name secp256k1PairFromSeed
|
||||
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
*/
|
||||
export function secp256k1KeypairFromSeed (seed: Uint8Array): Keypair {
|
||||
export function secp256k1PairFromSeed (seed: Uint8Array): Keypair {
|
||||
assert(seed.length === 32, 'Expected valid 32-byte private key as a seed');
|
||||
|
||||
const key = secp256k1.keyFromPrivate(seed);
|
||||
|
||||
@@ -5,7 +5,7 @@ import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { secp256k1Hasher } from './hasher';
|
||||
import { secp256k1Expand, secp256k1KeypairFromSeed, secp256k1Sign, secp256k1Verify } from '.';
|
||||
import { secp256k1Expand, secp256k1PairFromSeed, secp256k1Sign, secp256k1Verify } from '.';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
@@ -19,13 +19,13 @@ describe('sign and verify', (): void => {
|
||||
});
|
||||
|
||||
it('has 65-byte signatures', (): void => {
|
||||
const pair = secp256k1KeypairFromSeed(randomAsU8a());
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
|
||||
expect(secp256k1Sign(MESSAGE, pair)).toHaveLength(65);
|
||||
});
|
||||
|
||||
it('signs/verifies a message by random key (blake2)', (): void => {
|
||||
const pair = secp256k1KeypairFromSeed(randomAsU8a());
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
const signature = secp256k1Sign(MESSAGE, pair);
|
||||
const address = secp256k1Hasher('blake2', pair.publicKey);
|
||||
|
||||
@@ -33,7 +33,7 @@ describe('sign and verify', (): void => {
|
||||
});
|
||||
|
||||
it('signs/verifies a message by random key (keccak)', (): void => {
|
||||
const pair = secp256k1KeypairFromSeed(randomAsU8a());
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
const signature = secp256k1Sign(MESSAGE, pair, 'keccak');
|
||||
const address = secp256k1Hasher('keccak', secp256k1Expand(pair.publicKey));
|
||||
|
||||
@@ -41,7 +41,7 @@ describe('sign and verify', (): void => {
|
||||
});
|
||||
|
||||
it('fails verification on hasher mismatches', (): void => {
|
||||
const pair = secp256k1KeypairFromSeed(randomAsU8a());
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
const signature = secp256k1Sign(MESSAGE, pair, 'keccak');
|
||||
const address = secp256k1Hasher('keccak', secp256k1Expand(pair.publicKey));
|
||||
|
||||
@@ -50,7 +50,7 @@ describe('sign and verify', (): void => {
|
||||
|
||||
it('works over a range of random keys (blake2)', (): void => {
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const pair = secp256k1KeypairFromSeed(randomAsU8a());
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
|
||||
try {
|
||||
expect(
|
||||
@@ -70,7 +70,7 @@ describe('sign and verify', (): void => {
|
||||
|
||||
it('works over a range of random keys (keccak)', (): void => {
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const pair = secp256k1KeypairFromSeed(randomAsU8a());
|
||||
const pair = secp256k1PairFromSeed(randomAsU8a());
|
||||
|
||||
try {
|
||||
expect(
|
||||
|
||||
@@ -7,9 +7,9 @@ import type { KeypairType, VerifyResult } from '../types';
|
||||
import { assert, u8aIsWrapped, u8aToU8a, u8aUnwrapBytes, u8aWrapBytes } from '@polkadot/util';
|
||||
|
||||
import { decodeAddress } from '../address/decode';
|
||||
import { naclVerify } from '../nacl/verify';
|
||||
import { schnorrkelVerify } from '../schnorrkel/verify';
|
||||
import { ed25519Verify } from '../ed25519/verify';
|
||||
import { secp256k1Verify } from '../secp256k1/verify';
|
||||
import { sr25519Verify } from '../sr25519/verify';
|
||||
|
||||
interface VerifyInput {
|
||||
message: Uint8Array;
|
||||
@@ -31,8 +31,8 @@ const VERIFIERS_ECDSA: Verifier[] = [
|
||||
];
|
||||
|
||||
const VERIFIERS: Verifier[] = [
|
||||
['ed25519', naclVerify],
|
||||
['sr25519', schnorrkelVerify],
|
||||
['ed25519', ed25519Verify],
|
||||
['sr25519', sr25519Verify],
|
||||
...VERIFIERS_ECDSA
|
||||
];
|
||||
|
||||
@@ -66,9 +66,9 @@ function verifyMultisig (result: VerifyResult, { message, publicKey, signature }
|
||||
try {
|
||||
result.isValid = {
|
||||
ecdsa: () => verifyDetect(result, { message, publicKey, signature: signature.subarray(1) }, VERIFIERS_ECDSA).isValid,
|
||||
ed25519: () => naclVerify(message, signature.subarray(1), publicKey),
|
||||
ed25519: () => ed25519Verify(message, signature.subarray(1), publicKey),
|
||||
none: () => { throw Error('no verify for `none` crypto type'); },
|
||||
sr25519: () => schnorrkelVerify(message, signature.subarray(1), publicKey)
|
||||
sr25519: () => sr25519Verify(message, signature.subarray(1), publicKey)
|
||||
}[type]();
|
||||
} catch (error) {
|
||||
// ignore, result.isValid still set to false
|
||||
|
||||
+5
-5
@@ -6,7 +6,7 @@ import type { Keypair } from '../types';
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { schnorrkelAgreement, schnorrkelKeypairFromSeed } from '.';
|
||||
import { sr25519Agreement, sr25519PairFromSeed } from '.';
|
||||
|
||||
describe('agreement', (): void => {
|
||||
let pairA: Keypair;
|
||||
@@ -15,18 +15,18 @@ describe('agreement', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
|
||||
pairA = schnorrkelKeypairFromSeed('0x98b3d305d5a5eace562387e47e59badd4d77e3f72cabfb10a60f8a197059f0a8');
|
||||
pairB = schnorrkelKeypairFromSeed('0x9732eea001851ff862d949a1699c9971f3a26edbede2ad7922cbbe9a0701f366');
|
||||
pairA = sr25519PairFromSeed('0x98b3d305d5a5eace562387e47e59badd4d77e3f72cabfb10a60f8a197059f0a8');
|
||||
pairB = sr25519PairFromSeed('0x9732eea001851ff862d949a1699c9971f3a26edbede2ad7922cbbe9a0701f366');
|
||||
});
|
||||
|
||||
it('matches a known agreement (both ways)', (): void => {
|
||||
const TEST = '0xb03a0b198c34c16f35cae933d88b16341b4cef3e84e851f20e664c6a30527f4e';
|
||||
|
||||
expect(
|
||||
u8aToHex(schnorrkelAgreement(pairA.secretKey, pairB.publicKey))
|
||||
u8aToHex(sr25519Agreement(pairA.secretKey, pairB.publicKey))
|
||||
).toEqual(TEST);
|
||||
expect(
|
||||
u8aToHex(schnorrkelAgreement(pairB.secretKey, pairA.publicKey))
|
||||
u8aToHex(sr25519Agreement(pairB.secretKey, pairA.publicKey))
|
||||
).toEqual(TEST);
|
||||
});
|
||||
});
|
||||
+2
-2
@@ -7,10 +7,10 @@ import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519Agree } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name schnorrkelAgreement
|
||||
* @name sr25519Agreement
|
||||
* @description Key agreement between other's public key and self secret key
|
||||
*/
|
||||
export function schnorrkelAgreement (secretKey: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string): Uint8Array {
|
||||
export function sr25519Agreement (secretKey: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string): Uint8Array {
|
||||
const secretKeyU8a = u8aToU8a(secretKey);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
|
||||
+4
-4
@@ -5,15 +5,15 @@ import type { Keypair } from '../types';
|
||||
|
||||
import { assert, isU8a } from '@polkadot/util';
|
||||
|
||||
import { schnorrkelKeypairFromU8a } from './keypair/fromU8a';
|
||||
import { schnorrkelKeypairToU8a } from './keypair/toU8a';
|
||||
import { sr25519PairFromU8a } from './keypair/fromU8a';
|
||||
import { sr25519KeypairToU8a } from './keypair/toU8a';
|
||||
|
||||
export function createDeriveFn (derive: (pair: Uint8Array, cc: Uint8Array) => Uint8Array): (keypair: Keypair, chainCode: Uint8Array) => Keypair {
|
||||
return (keypair: Keypair, chainCode: Uint8Array): Keypair => {
|
||||
assert(isU8a(chainCode) && chainCode.length === 32, 'Invalid chainCode passed to derive');
|
||||
|
||||
return schnorrkelKeypairFromU8a(
|
||||
derive(schnorrkelKeypairToU8a(keypair), chainCode)
|
||||
return sr25519PairFromU8a(
|
||||
derive(sr25519KeypairToU8a(keypair), chainCode)
|
||||
);
|
||||
};
|
||||
}
|
||||
+1
-1
@@ -5,4 +5,4 @@ import { sr25519DeriveKeypairHard } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { createDeriveFn } from './derive';
|
||||
|
||||
export const schnorrkelDeriveHard = createDeriveFn(sr25519DeriveKeypairHard);
|
||||
export const sr25519DeriveHard = createDeriveFn(sr25519DeriveKeypairHard);
|
||||
+1
-1
@@ -6,7 +6,7 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import { assert, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519DerivePublicSoft } from '@polkadot/wasm-crypto';
|
||||
|
||||
export function schnorrkelDerivePublic (publicKey: HexString | Uint8Array | string, chainCode: Uint8Array): Uint8Array {
|
||||
export function sr25519DerivePublic (publicKey: HexString | Uint8Array | string, chainCode: Uint8Array): Uint8Array {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
|
||||
assert(isU8a(chainCode) && chainCode.length === 32, 'Invalid chainCode passed to derive');
|
||||
+1
-1
@@ -5,4 +5,4 @@ import { sr25519DeriveKeypairSoft } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { createDeriveFn } from './derive';
|
||||
|
||||
export const schnorrkelDeriveSoft = createDeriveFn(sr25519DeriveKeypairSoft);
|
||||
export const sr25519DeriveSoft = createDeriveFn(sr25519DeriveKeypairSoft);
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { sr25519Agreement } from './agreement';
|
||||
export { sr25519DeriveHard } from './deriveHard';
|
||||
export { sr25519DerivePublic } from './derivePublic';
|
||||
export { sr25519DeriveSoft } from './deriveSoft';
|
||||
export { sr25519PairFromSeed } from './keypair/fromSeed';
|
||||
export { sr25519Sign } from './sign';
|
||||
export { sr25519Verify } from './verify';
|
||||
export { sr25519VrfSign } from './vrfSign';
|
||||
export { sr25519VrfVerify } from './vrfVerify';
|
||||
+4
-4
@@ -5,10 +5,10 @@ import { stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { mnemonicToMiniSecret } from '../../mnemonic';
|
||||
import { schnorrkelKeypairFromSeed } from '..';
|
||||
import { sr25519PairFromSeed } from '..';
|
||||
import tests from './testing';
|
||||
|
||||
describe('schnorrkelKeypairFromSeed', (): void => {
|
||||
describe('sr25519PairFromSeed', (): void => {
|
||||
const TEST = stringToU8a('12345678901234567890123456789012');
|
||||
const RESULT = {
|
||||
publicKey: new Uint8Array([116, 28, 8, 160, 111, 65, 197, 150, 96, 143, 103, 116, 37, 155, 217, 4, 51, 4, 173, 250, 93, 62, 234, 98, 118, 11, 217, 190, 151, 99, 77, 99]),
|
||||
@@ -21,14 +21,14 @@ describe('schnorrkelKeypairFromSeed', (): void => {
|
||||
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
|
||||
expect(
|
||||
schnorrkelKeypairFromSeed(TEST)
|
||||
sr25519PairFromSeed(TEST)
|
||||
).toEqual(RESULT);
|
||||
});
|
||||
|
||||
tests.forEach(([mnemonic, , , secret], index): void => {
|
||||
it(`creates valid against known (${index})`, (): void => {
|
||||
const seed = mnemonicToMiniSecret(mnemonic, 'Substrate');
|
||||
const pair = schnorrkelKeypairFromSeed(seed);
|
||||
const pair = sr25519PairFromSeed(seed);
|
||||
|
||||
expect(
|
||||
u8aToHex(pair.secretKey)
|
||||
+4
-4
@@ -7,18 +7,18 @@ import type { Keypair } from '../../types';
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519KeypairFromSeed } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { schnorrkelKeypairFromU8a } from './fromU8a';
|
||||
import { sr25519PairFromU8a } from './fromU8a';
|
||||
|
||||
/**
|
||||
* @name schnorrkelKeypairFromSeed
|
||||
* @name sr25519PairFromSeed
|
||||
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
*/
|
||||
export function schnorrkelKeypairFromSeed (seed: HexString | Uint8Array | string): Keypair {
|
||||
export function sr25519PairFromSeed (seed: HexString | Uint8Array | string): Keypair {
|
||||
const seedU8a = u8aToU8a(seed);
|
||||
|
||||
assert(seedU8a.length === 32, () => `Expected a seed matching 32 bytes, found ${seedU8a.length}`);
|
||||
|
||||
return schnorrkelKeypairFromU8a(
|
||||
return sr25519PairFromU8a(
|
||||
sr25519KeypairFromSeed(seedU8a)
|
||||
);
|
||||
}
|
||||
+1
-1
@@ -10,7 +10,7 @@ const SEC_LEN = 64;
|
||||
const PUB_LEN = 32;
|
||||
const TOT_LEN = SEC_LEN + PUB_LEN;
|
||||
|
||||
export function schnorrkelKeypairFromU8a (full: HexString | Uint8Array | string): Keypair {
|
||||
export function sr25519PairFromU8a (full: HexString | Uint8Array | string): Keypair {
|
||||
const fullU8a = u8aToU8a(full);
|
||||
|
||||
assert(fullU8a.length === TOT_LEN, () => `Expected keypair with ${TOT_LEN} bytes, found ${fullU8a.length}`);
|
||||
+1
-1
@@ -5,6 +5,6 @@ import type { Keypair } from '../../types';
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
export function schnorrkelKeypairToU8a ({ publicKey, secretKey }: Keypair): Uint8Array {
|
||||
export function sr25519KeypairToU8a ({ publicKey, secretKey }: Keypair): Uint8Array {
|
||||
return u8aConcat(secretKey, publicKey).slice();
|
||||
}
|
||||
+4
-4
@@ -5,15 +5,15 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import type { Keypair } from '../types';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519Sign } from '@polkadot/wasm-crypto';
|
||||
import { sr25519Sign as wasmSign } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name schnorrkelSign
|
||||
* @name sr25519Sign
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function schnorrkelSign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
export function sr25519Sign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
assert(publicKey?.length === 32, 'Expected a valid publicKey, 32-bytes');
|
||||
assert(secretKey?.length === 64, 'Expected a valid secretKey, 64-bytes');
|
||||
|
||||
return sr25519Sign(publicKey, secretKey, u8aToU8a(message));
|
||||
return wasmSign(publicKey, secretKey, u8aToU8a(message));
|
||||
}
|
||||
+10
-10
@@ -5,9 +5,9 @@ import { stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { schnorrkelKeypairFromSeed } from './keypair/fromSeed';
|
||||
import { schnorrkelSign } from './sign';
|
||||
import { schnorrkelVerify } from './verify';
|
||||
import { sr25519PairFromSeed } from './keypair/fromSeed';
|
||||
import { sr25519Sign } from './sign';
|
||||
import { sr25519Verify } from './verify';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
@@ -17,21 +17,21 @@ describe('sign and verify', (): void => {
|
||||
});
|
||||
|
||||
it('has 64-byte signatures', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
|
||||
expect(schnorrkelSign(MESSAGE, pair)).toHaveLength(64);
|
||||
expect(sr25519Sign(MESSAGE, pair)).toHaveLength(64);
|
||||
});
|
||||
|
||||
it('can sign and verify a message', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const signature = schnorrkelSign(MESSAGE, pair);
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const signature = sr25519Sign(MESSAGE, pair);
|
||||
|
||||
expect(schnorrkelVerify(MESSAGE, signature, pair.publicKey)).toBe(true);
|
||||
expect(sr25519Verify(MESSAGE, signature, pair.publicKey)).toBe(true);
|
||||
});
|
||||
|
||||
it('throws error when publicKey lengths do not match', (): void => {
|
||||
expect(
|
||||
() => schnorrkelVerify(
|
||||
() => sr25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(64),
|
||||
new Uint8Array(31)
|
||||
@@ -41,7 +41,7 @@ describe('sign and verify', (): void => {
|
||||
|
||||
it('throws error when signature lengths do not match', (): void => {
|
||||
expect(
|
||||
() => schnorrkelVerify(
|
||||
() => sr25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(65),
|
||||
new Uint8Array(32)
|
||||
+4
-4
@@ -4,18 +4,18 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519Verify } from '@polkadot/wasm-crypto';
|
||||
import { sr25519Verify as wasmVerify } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name schnorrkelVerify
|
||||
* @name sr25519Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function schnorrkelVerify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string): boolean {
|
||||
export function sr25519Verify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string): boolean {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
assert(publicKeyU8a.length === 32, () => `Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
|
||||
assert(signatureU8a.length === 64, () => `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
|
||||
return sr25519Verify(signatureU8a, u8aToU8a(message), publicKeyU8a);
|
||||
return wasmVerify(signatureU8a, u8aToU8a(message), publicKeyU8a);
|
||||
}
|
||||
+2
-2
@@ -10,10 +10,10 @@ import { vrfSign } from '@polkadot/wasm-crypto';
|
||||
const EMPTY_U8A = new Uint8Array();
|
||||
|
||||
/**
|
||||
* @name schnorrkelVrfSign
|
||||
* @name sr25519VrfSign
|
||||
* @description Sign with sr25519 vrf signing (deterministic)
|
||||
*/
|
||||
export function schnorrkelVrfSign (message: HexString | Uint8Array | string, { secretKey }: Partial<Keypair>, context: HexString | string | Uint8Array = EMPTY_U8A, extra: HexString | string | Uint8Array = EMPTY_U8A): Uint8Array {
|
||||
export function sr25519VrfSign (message: HexString | Uint8Array | string, { secretKey }: Partial<Keypair>, context: HexString | string | Uint8Array = EMPTY_U8A, extra: HexString | string | Uint8Array = EMPTY_U8A): Uint8Array {
|
||||
assert(secretKey?.length === 64, 'Invalid secretKey, expected 64-bytes');
|
||||
|
||||
return vrfSign(secretKey, u8aToU8a(context), u8aToU8a(message), u8aToU8a(extra));
|
||||
+19
-19
@@ -5,9 +5,9 @@ import { stringToU8a, u8aEq } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { schnorrkelKeypairFromSeed } from './keypair/fromSeed';
|
||||
import { schnorrkelVrfSign } from './vrfSign';
|
||||
import { schnorrkelVrfVerify } from './vrfVerify';
|
||||
import { sr25519PairFromSeed } from './keypair/fromSeed';
|
||||
import { sr25519VrfSign } from './vrfSign';
|
||||
import { sr25519VrfVerify } from './vrfVerify';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
@@ -17,46 +17,46 @@ describe('vrf sign and verify', (): void => {
|
||||
});
|
||||
|
||||
it('has 96-byte proofs', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
|
||||
expect(schnorrkelVrfSign(MESSAGE, pair)).toHaveLength(96);
|
||||
expect(sr25519VrfSign(MESSAGE, pair)).toHaveLength(96);
|
||||
});
|
||||
|
||||
it('signing is deterministic', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof1 = schnorrkelVrfSign(MESSAGE, pair);
|
||||
const proof2 = schnorrkelVrfSign(MESSAGE, pair);
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const proof1 = sr25519VrfSign(MESSAGE, pair);
|
||||
const proof2 = sr25519VrfSign(MESSAGE, pair);
|
||||
|
||||
expect(u8aEq(proof1.subarray(0, 32), proof2.subarray(0, 32))).toBe(true);
|
||||
});
|
||||
|
||||
it('can sign and verify a message', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof = schnorrkelVrfSign(MESSAGE, pair);
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const proof = sr25519VrfSign(MESSAGE, pair);
|
||||
|
||||
expect(schnorrkelVrfVerify(MESSAGE, proof, pair.publicKey)).toBe(true);
|
||||
expect(sr25519VrfVerify(MESSAGE, proof, pair.publicKey)).toBe(true);
|
||||
});
|
||||
|
||||
it('can sign and verify a message (with context)', (): void => {
|
||||
const context = 'my context';
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof = schnorrkelVrfSign(MESSAGE, pair, context);
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const proof = sr25519VrfSign(MESSAGE, pair, context);
|
||||
|
||||
expect(schnorrkelVrfVerify(MESSAGE, proof, pair.publicKey, context)).toBe(true);
|
||||
expect(sr25519VrfVerify(MESSAGE, proof, pair.publicKey, context)).toBe(true);
|
||||
});
|
||||
|
||||
it('can sign and verify a message (with context & extra)', (): void => {
|
||||
const context = 'my context';
|
||||
const extra = 'some extra transcript data';
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof = schnorrkelVrfSign(MESSAGE, pair, context, extra);
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const proof = sr25519VrfSign(MESSAGE, pair, context, extra);
|
||||
|
||||
expect(schnorrkelVrfVerify(MESSAGE, proof, pair.publicKey, context, extra)).toBe(true);
|
||||
expect(sr25519VrfVerify(MESSAGE, proof, pair.publicKey, context, extra)).toBe(true);
|
||||
});
|
||||
|
||||
it('throws error when publicKey lengths do not match', (): void => {
|
||||
expect(
|
||||
() => schnorrkelVrfVerify(
|
||||
() => sr25519VrfVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(96),
|
||||
new Uint8Array(31)
|
||||
@@ -66,7 +66,7 @@ describe('vrf sign and verify', (): void => {
|
||||
|
||||
it('throws error when proof lengths do not match', (): void => {
|
||||
expect(
|
||||
() => schnorrkelVrfVerify(
|
||||
() => sr25519VrfVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(99),
|
||||
new Uint8Array(32)
|
||||
+2
-2
@@ -9,10 +9,10 @@ import { vrfVerify } from '@polkadot/wasm-crypto';
|
||||
const EMPTY_U8A = new Uint8Array();
|
||||
|
||||
/**
|
||||
* @name schnorrkelVrfVerify
|
||||
* @name sr25519VrfVerify
|
||||
* @description Verify with sr25519 vrf verification
|
||||
*/
|
||||
export function schnorrkelVrfVerify (message: HexString | Uint8Array | string, signOutput: HexString | string | Uint8Array, publicKey: HexString | Uint8Array | string, context: HexString | string | Uint8Array = EMPTY_U8A, extra: HexString | string | Uint8Array = EMPTY_U8A): boolean {
|
||||
export function sr25519VrfVerify (message: HexString | Uint8Array | string, signOutput: HexString | string | Uint8Array, publicKey: HexString | Uint8Array | string, context: HexString | string | Uint8Array = EMPTY_U8A, extra: HexString | string | Uint8Array = EMPTY_U8A): boolean {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const proofU8a = u8aToU8a(signOutput);
|
||||
|
||||
Reference in New Issue
Block a user