mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-25 16:27:57 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import type { Keypair } from '../types.js';
|
||||
|
||||
import { u8aToHex } from '@pezkuwi/util';
|
||||
|
||||
import { sr25519Agreement, sr25519PairFromSeed } from './index.js';
|
||||
|
||||
describe('agreement', (): void => {
|
||||
let pairA: Keypair;
|
||||
let pairB: Keypair;
|
||||
|
||||
beforeEach((): void => {
|
||||
pairA = sr25519PairFromSeed('0x98b3d305d5a5eace562387e47e59badd4d77e3f72cabfb10a60f8a197059f0a8');
|
||||
pairB = sr25519PairFromSeed('0x9732eea001851ff862d949a1699c9971f3a26edbede2ad7922cbbe9a0701f366');
|
||||
});
|
||||
|
||||
it('matches a known agreement (both ways)', (): void => {
|
||||
const TEST = '0xb03a0b198c34c16f35cae933d88b16341b4cef3e84e851f20e664c6a30527f4e';
|
||||
|
||||
expect(
|
||||
u8aToHex(sr25519Agreement(pairA.secretKey, pairB.publicKey))
|
||||
).toEqual(TEST);
|
||||
expect(
|
||||
u8aToHex(sr25519Agreement(pairB.secretKey, pairA.publicKey))
|
||||
).toEqual(TEST);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { getSharedSecret } from '@scure/sr25519';
|
||||
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
/**
|
||||
* @name sr25519Agreement
|
||||
* @description Key agreement between other's public key and self secret key
|
||||
*/
|
||||
export function sr25519Agreement (secretKey: string | Uint8Array, publicKey: string | Uint8Array): Uint8Array {
|
||||
const secretKeyU8a = u8aToU8a(secretKey);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
|
||||
if (publicKeyU8a.length !== 32) {
|
||||
throw new Error(`Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
|
||||
} else if (secretKeyU8a.length !== 64) {
|
||||
throw new Error(`Invalid secretKey, received ${secretKeyU8a.length} bytes, expected 64`);
|
||||
}
|
||||
|
||||
return getSharedSecret(secretKeyU8a, publicKeyU8a);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../types.js';
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { isU8a } from '@pezkuwi/util';
|
||||
|
||||
export function createDeriveFn (derive: (pair: Uint8Array, cc: Uint8Array) => Uint8Array): (keypair: Keypair, chainCode: Uint8Array) => Keypair {
|
||||
return (keypair: Keypair, chainCode: Uint8Array): Keypair => {
|
||||
if (!isU8a(chainCode) || chainCode.length !== 32) {
|
||||
throw new Error('Invalid chainCode passed to derive');
|
||||
}
|
||||
|
||||
const secretKey = derive(keypair.secretKey, chainCode);
|
||||
const publicKey = sr25519.getPublicKey(secretKey);
|
||||
|
||||
return { publicKey, secretKey };
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { createDeriveFn } from './derive.js';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
export const sr25519DeriveHard = /*#__PURE__*/ createDeriveFn(sr25519.HDKD.secretHard);
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { isU8a, u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
export function sr25519DerivePublic (publicKey: string | Uint8Array, chainCode: Uint8Array): Uint8Array {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
|
||||
if (!isU8a(chainCode) || chainCode.length !== 32) {
|
||||
throw new Error('Invalid chainCode passed to derive');
|
||||
} else if (publicKeyU8a.length !== 32) {
|
||||
throw new Error(`Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
|
||||
}
|
||||
|
||||
return sr25519.HDKD.publicSoft(publicKeyU8a, chainCode);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { createDeriveFn } from './derive.js';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
export const sr25519DeriveSoft = /*#__PURE__*/ createDeriveFn(sr25519.HDKD.secretSoft);
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { sr25519Agreement } from './agreement.js';
|
||||
export { sr25519DeriveHard } from './deriveHard.js';
|
||||
export { sr25519DerivePublic } from './derivePublic.js';
|
||||
export { sr25519DeriveSoft } from './deriveSoft.js';
|
||||
export { sr25519PairFromSeed } from './pair/fromSeed.js';
|
||||
export { sr25519Sign } from './sign.js';
|
||||
export { sr25519Verify } from './verify.js';
|
||||
export { sr25519VrfSign } from './vrfSign.js';
|
||||
export { sr25519VrfVerify } from './vrfVerify.js';
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringToU8a, u8aToHex } from '@pezkuwi/util';
|
||||
|
||||
import { mnemonicToMiniSecret } from '../../mnemonic/index.js';
|
||||
import { sr25519PairFromSeed } from '../index.js';
|
||||
import tests from './testing.spec.js';
|
||||
|
||||
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]),
|
||||
secretKey: new Uint8Array([240, 16, 102, 96, 195, 221, 162, 63, 22, 218, 169, 172, 91, 129, 27, 150, 48, 119, 245, 188, 10, 248, 159, 133, 128, 79, 13, 232, 228, 36, 240, 80, 249, 141, 102, 243, 148, 66, 80, 111, 249, 71, 253, 145, 31, 24, 199, 167, 165, 218, 99, 154, 99, 232, 211, 180, 226, 51, 247, 65, 67, 217, 81, 193])
|
||||
};
|
||||
|
||||
it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
|
||||
expect(
|
||||
sr25519PairFromSeed(TEST)
|
||||
).toEqual(RESULT);
|
||||
});
|
||||
|
||||
tests.forEach(([mnemonic, , , secret], index): void => {
|
||||
it(`creates valid against known (${index})`, (): void => {
|
||||
const seed = mnemonicToMiniSecret(mnemonic, 'Substrate');
|
||||
const pair = sr25519PairFromSeed(seed);
|
||||
|
||||
expect(
|
||||
u8aToHex(pair.secretKey)
|
||||
).toEqual(secret);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../../types.js';
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
/**
|
||||
* @name sr25519PairFromSeed
|
||||
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
*/
|
||||
export function sr25519PairFromSeed (seed: string | Uint8Array): Keypair {
|
||||
const seedU8a = u8aToU8a(seed);
|
||||
|
||||
if (seedU8a.length !== 32) {
|
||||
throw new Error(`Expected a seed matching 32 bytes, found ${seedU8a.length}`);
|
||||
}
|
||||
|
||||
const sec = sr25519.secretFromSeed(seedU8a);
|
||||
const pub = sr25519.getPublicKey(sec);
|
||||
|
||||
return {
|
||||
publicKey: pub,
|
||||
secretKey: sec
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../../types.js';
|
||||
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
const SEC_LEN = 64;
|
||||
const PUB_LEN = 32;
|
||||
const TOT_LEN = SEC_LEN + PUB_LEN;
|
||||
|
||||
export function sr25519PairFromU8a (full: string | Uint8Array): Keypair {
|
||||
const fullU8a = u8aToU8a(full);
|
||||
|
||||
if (fullU8a.length !== TOT_LEN) {
|
||||
throw new Error(`Expected keypair with ${TOT_LEN} bytes, found ${fullU8a.length}`);
|
||||
}
|
||||
|
||||
return {
|
||||
publicKey: fullU8a.slice(SEC_LEN, TOT_LEN),
|
||||
secretKey: fullU8a.slice(0, SEC_LEN)
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
// mnemonic, entropy, seed, secret (expanded)
|
||||
type Test = [string, string, string, string];
|
||||
|
||||
const tests: Test[] = [
|
||||
[
|
||||
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
|
||||
'0x00000000000000000000000000000000',
|
||||
'0x44e9d125f037ac1d51f0a7d3649689d422c2af8b1ec8e00d71db4d7bf6d127e33f50c3d5c84fa3e5399c72d6cbbbbc4a49bf76f76d952f479d74655a2ef2d453',
|
||||
'0xb0b3174fe43c15938bb0d0cc5b6f7ac7295f557ee1e6fdeb24fb73f4e0cb2b6ec40ffb9da4af6d411eae8e292750fd105ff70fe93f337b5b590f5a9d9030c750'
|
||||
],
|
||||
[
|
||||
'legal winner thank year wave sausage worth useful legal winner thank yellow',
|
||||
'0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f',
|
||||
'0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b780416850f1e26c1f4b8d88ece681cb058ab66d6182bc2ce5a03181f7b74c27576b5c8bf',
|
||||
'0x20666c9dd63c5b04a6a14377579af14aba60707752d134726304d0804992e26f9092c47fbb9e14c02fd53c702c8a3cfca4735638599da5c4362e0d0560dceb58'
|
||||
],
|
||||
[
|
||||
'letter advice cage absurd amount doctor acoustic avoid letter advice cage above',
|
||||
'0x80808080808080808080808080808080',
|
||||
'0x27f3eb595928c60d5bc91a4d747da40ed236328183046892ed6cd5aa9ae38122acd1183adf09a89839acb1e6eaa7fb563cc958a3f9161248d5a036e0d0af533d',
|
||||
'0x709e8254d0a9543c6b35b145dd23349e6369d487a1d10b0cfe09c05ff521f4691ad8bb8221339af38fc48510ec2dfc3104bb94d38f1fa241ceb252943df7b6b5'
|
||||
],
|
||||
[
|
||||
'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong',
|
||||
'0xffffffffffffffffffffffffffffffff',
|
||||
'0x227d6256fd4f9ccaf06c45eaa4b2345969640462bbb00c5f51f43cb43418c7a753265f9b1e0c0822c155a9cabc769413ecc14553e135fe140fc50b6722c6b9df',
|
||||
'0x88206f4b4102ad30ee40b4b5943c5259db77fd576d95d79eeea00160197e406308821814dea9442675a5d3fa375b3bd65ffe92be43e07dbf6bb4ab84e9d4449d'
|
||||
],
|
||||
[
|
||||
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon agent',
|
||||
'0x000000000000000000000000000000000000000000000000',
|
||||
'0x44e9d125f037ac1d51f0a7d3649689d422c2af8b1ec8e00d71db4d7bf6d127e33f50c3d5c84fa3e5399c72d6cbbbbc4a49bf76f76d952f479d74655a2ef2d453',
|
||||
'0xb0b3174fe43c15938bb0d0cc5b6f7ac7295f557ee1e6fdeb24fb73f4e0cb2b6ec40ffb9da4af6d411eae8e292750fd105ff70fe93f337b5b590f5a9d9030c750'
|
||||
],
|
||||
[
|
||||
'legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal will',
|
||||
'0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f',
|
||||
'0xcb1d50e14101024a88905a098feb1553d4306d072d7460e167a60ccb3439a6817a0afc59060f45d999ddebc05308714733c9e1e84f30feccddd4ad6f95c8a445',
|
||||
'0x50dcb74f223740d6a256000a2f1ccdb60044b39ce3aad71a3bd7761848d5f55d5a34f96e0b96ecb45d7a142e07ddfde734f9525f9f88310ab50e347da5789d3e'
|
||||
],
|
||||
[
|
||||
'letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter always',
|
||||
'0x808080808080808080808080808080808080808080808080',
|
||||
'0x9ddecf32ce6bee77f867f3c4bb842d1f0151826a145cb4489598fe71ac29e3551b724f01052d1bc3f6d9514d6df6aa6d0291cfdf997a5afdb7b6a614c88ab36a',
|
||||
'0x88112947a30e864b511838b6daf6e1e13801ae003d6d9b73eb5892c355f7e37ab3bb71200092d004467b06fe67bc153ee4e2bb7af2f544815b0dde276d2dae75'
|
||||
],
|
||||
[
|
||||
'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo when',
|
||||
'0xffffffffffffffffffffffffffffffffffffffffffffffff',
|
||||
'0x8971cb290e7117c64b63379c97ed3b5c6da488841bd9f95cdc2a5651ac89571e2c64d391d46e2475e8b043911885457cd23e99a28b5a18535fe53294dc8e1693',
|
||||
'0x4859cdeda3f957b7ffcd2d59257c30e43996796f38e1be5c6136c9bf3744e047ce9a52c11793c98d0dc8caee927576ce46ef2e5f4b3f1d5e4b1344b2c31ebe8e'
|
||||
],
|
||||
[
|
||||
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art',
|
||||
'0x0000000000000000000000000000000000000000000000000000000000000000',
|
||||
'0x44e9d125f037ac1d51f0a7d3649689d422c2af8b1ec8e00d71db4d7bf6d127e33f50c3d5c84fa3e5399c72d6cbbbbc4a49bf76f76d952f479d74655a2ef2d453',
|
||||
'0xb0b3174fe43c15938bb0d0cc5b6f7ac7295f557ee1e6fdeb24fb73f4e0cb2b6ec40ffb9da4af6d411eae8e292750fd105ff70fe93f337b5b590f5a9d9030c750'
|
||||
],
|
||||
[
|
||||
'legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth title',
|
||||
'0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f',
|
||||
'0x3037276a5d05fcd7edf51869eb841bdde27c574dae01ac8cfb1ea476f6bea6ef57ab9afe14aea1df8a48f97ae25b37d7c8326e49289efb25af92ba5a25d09ed3',
|
||||
'0xe8962ace15478f69fa42ddd004aad2c285c9f5a02e0712860e83fbec041f89489046aa57b21db2314d93aeb4a2d7cbdab21d4856e8e151894abd17fb04ae65e1'
|
||||
],
|
||||
[
|
||||
'letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic bless',
|
||||
'0x8080808080808080808080808080808080808080808080808080808080808080',
|
||||
'0x2c9c6144a06ae5a855453d98c3dea470e2a8ffb78179c2e9eb15208ccca7d831c97ddafe844ab933131e6eb895f675ede2f4e39837bb5769d4e2bc11df58ac42',
|
||||
'0x78667314bf1e52e38d29792cdf294efcaddadc4fa9ce48c5f2bef4daad7ed95d1db960d6f6f895c1a9d2a3ddcc0398ba6578580ea1f03f65ea9a68e97cf3f840'
|
||||
],
|
||||
[
|
||||
'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo vote',
|
||||
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
|
||||
'0x047e89ef7739cbfe30da0ad32eb1720d8f62441dd4f139b981b8e2d0bd412ed4eb14b89b5098c49db2301d4e7df4e89c21e53f345138e56a5e7d63fae21c5939',
|
||||
'0xe09f4ae0d4e22f6c9bbc4251c880e73d93d10fdf7f152c393ce36e4e942b3155a6f4b48e6c6ebce902a10d4ab46ef59cd154c15aeb8f7fcd4e1e26342d40806d'
|
||||
],
|
||||
[
|
||||
'ozone drill grab fiber curtain grace pudding thank cruise elder eight picnic',
|
||||
'0x9e885d952ad362caeb4efe34a8e91bd2',
|
||||
'0xf4956be6960bc145cdab782e649a5056598fd07cd3f32ceb73421c3da27833241324dc2c8b0a4d847eee457e6d4c5429f5e625ece22abaa6a976e82f1ec5531d',
|
||||
'0xb0eb046f48eacb7ad6c4da3ff92bfc29c9ad471bae3e554d5d63e58827160c70c7e5165598761f96b5659ab28c474f50e89ee13c67e30bca40fdcf4335835649'
|
||||
],
|
||||
[
|
||||
'gravity machine north sort system female filter attitude volume fold club stay feature office ecology stable narrow fog',
|
||||
'0x6610b25967cdcca9d59875f5cb50b0ea75433311869e930b',
|
||||
'0xfbcc5229ade0c0ff018cb7a329c5459f91876e4dde2a97ddf03c832eab7f26124366a543f1485479c31a9db0d421bda82d7e1fe562e57f3533cb1733b001d84d',
|
||||
'0xd8da734285a13967647dd906288e1ac871e1945d0c6b72fa259de4051a9b75431be0c4eb40b1ca38c780f445e3e2809282b9efef4dcc3538355e68094f1e79fa'
|
||||
],
|
||||
[
|
||||
'hamster diagram private dutch cause delay private meat slide toddler razor book happy fancy gospel tennis maple dilemma loan word shrug inflict delay length',
|
||||
'0x68a79eaca2324873eacc50cb9c6eca8cc68ea5d936f98787c60c7ebc74e6ce7c',
|
||||
'0x7c60c555126c297deddddd59f8cdcdc9e3608944455824dd604897984b5cc369cad749803bb36eb8b786b570c9cdc8db275dbe841486676a6adf389f3be3f076',
|
||||
'0x883b72fa7fb06b6abd0fc2cdb0018b3578e086e93074256bbbb8c68e53c04a56391bc0d19d7b2fa22a8148ccbe191d969c4323faca1935a576cc1b24301f203a'
|
||||
],
|
||||
[
|
||||
'scheme spot photo card baby mountain device kick cradle pact join borrow',
|
||||
'0xc0ba5a8e914111210f2bd131f3d5e08d',
|
||||
'0xc12157bf2506526c4bd1b79a056453b071361538e9e2c19c28ba2cfa39b5f23034b974e0164a1e8acd30f5b4c4de7d424fdb52c0116bfc6a965ba8205e6cc121',
|
||||
'0x7039a64150089f8d43188af964c7b8e2b8c9ba20aede085baca5672e978a47576c7193c3e557f37cdeeee5e5131b854e4309efc55259b050474e1f0884a7a621'
|
||||
],
|
||||
[
|
||||
'horn tenant knee talent sponsor spell gate clip pulse soap slush warm silver nephew swap uncle crack brave',
|
||||
'0x6d9be1ee6ebd27a258115aad99b7317b9c8d28b6d76431c3',
|
||||
'0x23766723e970e6b79dec4d5e4fdd627fd27d1ee026eb898feb9f653af01ad22080c6f306d1061656d01c4fe9a14c05f991d2c7d8af8730780de4f94cd99bd819',
|
||||
'0xe07a1f3073edad5b63585cdf1d5e6f8e50e3145de550fc8eb1fb430cce62d76d251904272c5d25fd634615d413bb31a2bc7b5d6eeb2f6ddc68a2b95ac6bd49bc'
|
||||
],
|
||||
[
|
||||
'panda eyebrow bullet gorilla call smoke muffin taste mesh discover soft ostrich alcohol speed nation flash devote level hobby quick inner drive ghost inside',
|
||||
'0x9f6a2878b2520799a44ef18bc7df394e7061a224d2c33cd015b157d746869863',
|
||||
'0xf4c83c86617cb014d35cd87d38b5ef1c5d5c3d58a73ab779114438a7b358f457e0462c92bddab5a406fe0e6b97c71905cf19f925f356bc673ceb0e49792f4340',
|
||||
'0x607f8595266ac0d4aa91bf4fddbd2a868889317f40099979be9743c46c418976e6ff3717bd11b94b418f91c8b88eae142cecb19104820997ddf5a379dd9da5ae'
|
||||
],
|
||||
[
|
||||
'cat swing flag economy stadium alone churn speed unique patch report train',
|
||||
'0x23db8160a31d3e0dca3688ed941adbf3',
|
||||
'0x719d4d4de0638a1705bf5237262458983da76933e718b2d64eb592c470f3c5d222e345cc795337bb3da393b94375ff4a56cfcd68d5ea25b577ee9384d35f4246',
|
||||
'0xd078b66bb357f1f06e897a6fdfa2f3dfb0da05836ded1fd0793373068b7e854e783a548a6d194f142e1ba78bf42a49fa58e3673b363ba6f6494efffa28f168df'
|
||||
],
|
||||
[
|
||||
'light rule cinnamon wrap drastic word pride squirrel upgrade then income fatal apart sustain crack supply proud access',
|
||||
'0x8197a4a47f0425faeaa69deebc05ca29c0a5b5cc76ceacc0',
|
||||
'0x7ae1291db32d16457c248567f2b101e62c5549d2a64cd2b7605d503ec876d58707a8d663641e99663bc4f6cc9746f4852e75e7e54de5bc1bd3c299c9a113409e',
|
||||
'0x5095fe4d0144b06e82aa4753d595fd10de9bba3733eba8ce0784417182317e725fac31b2fb53f4856a5e38866501425b485f4d2eaf2666a9f20ae68f4331ed2c'
|
||||
],
|
||||
[
|
||||
'all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform',
|
||||
'0x066dca1a2bb7e8a1db2832148ce9933eea0f3ac9548d793112d9a95c9407efad',
|
||||
'0xa911a5f4db0940b17ecb79c4dcf9392bf47dd18acaebdd4ef48799909ebb49672947cc15f4ef7e8ef47103a1a91a6732b821bda2c667e5b1d491c54788c69391',
|
||||
'0x8844cb50f3ba8030ab61afee623534836d4ea3677d42bae470fc5e251ea0ca7ec9ea65c8c40be191c7c8683165848279ced81f3a121c9450078a496b6c59f610'
|
||||
],
|
||||
[
|
||||
'vessel ladder alter error federal sibling chat ability sun glass valve picture',
|
||||
'0xf30f8c1da665478f49b001d94c5fc452',
|
||||
'0x4e2314ca7d9eebac6fe5a05a5a8d3546bc891785414d82207ac987926380411e559c885190d641ff7e686ace8c57db6f6e4333c1081e3d88d7141a74cf339c8f',
|
||||
'0x18917f0c7480c95cd4d98bdc7df773c366d33590252707da1358eb58b43a7b765e3c513878541bfbfb466bb4206f581edf9bf601409c72afac130bcc8b5661b5'
|
||||
],
|
||||
[
|
||||
'scissors invite lock maple supreme raw rapid void congress muscle digital elegant little brisk hair mango congress clump',
|
||||
'0xc10ec20dc3cd9f652c7fac2f1230f7a3c828389a14392f05',
|
||||
'0x7a83851102849edc5d2a3ca9d8044d0d4f00e5c4a292753ed3952e40808593251b0af1dd3c9ed9932d46e8608eb0b928216a6160bd4fc775a6e6fbd493d7c6b2',
|
||||
'0xb0bf86b0955413fc95144bab124e82042d0cce9c292c1bfd0874ae5a95412977e7bc109aeef33c7c90be952a83f3fe528419776520de721ef6ec9e814749c3fc'
|
||||
],
|
||||
[
|
||||
'void come effort suffer camp survey warrior heavy shoot primary clutch crush open amazing screen patrol group space point ten exist slush involve unfold',
|
||||
'0xf585c11aec520db57dd353c69554b21a89b20fb0650966fa0a9d6f74fd989d8f',
|
||||
'0x938ba18c3f521f19bd4a399c8425b02c716844325b1a65106b9d1593fbafe5e0b85448f523f91c48e331995ff24ae406757cff47d11f240847352b348ff436ed',
|
||||
'0xc07ba4a979657576f4f7446e3bd2672c87131fa0f472a8bc1f2e9b28c11fb04c66da12cd280662196a5888d8a77178dab8034ed42b11d1654a31db6e1ff4d4c5'
|
||||
]
|
||||
];
|
||||
|
||||
// eslint-disable-next-line jest/no-export
|
||||
export default tests;
|
||||
|
||||
it('has a test', (): void => {
|
||||
expect(true).toEqual(true);
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../../types.js';
|
||||
|
||||
import { u8aConcat } from '@pezkuwi/util';
|
||||
|
||||
export function sr25519KeypairToU8a ({ publicKey, secretKey }: Keypair): Uint8Array {
|
||||
return u8aConcat(secretKey, publicKey).slice();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringToU8a } from '@pezkuwi/util';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a.js';
|
||||
import { sr25519PairFromSeed } from './pair/fromSeed.js';
|
||||
import { sr25519Sign } from './sign.js';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
describe('sign', (): void => {
|
||||
it('has 64-byte signatures', (): void => {
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
|
||||
expect(sr25519Sign(MESSAGE, pair)).toHaveLength(64);
|
||||
});
|
||||
|
||||
it('has non-deterministic signatures', (): void => {
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const a = sr25519Sign(MESSAGE, pair);
|
||||
const b = sr25519Sign(MESSAGE, pair);
|
||||
|
||||
expect(a).not.toEqual(b);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../types.js';
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
/**
|
||||
* @name sr25519Sign
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function sr25519Sign (message: string | Uint8Array, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
if (publicKey?.length !== 32) {
|
||||
throw new Error('Expected a valid publicKey, 32-bytes');
|
||||
} else if (secretKey?.length !== 64) {
|
||||
throw new Error('Expected a valid secretKey, 64-bytes');
|
||||
}
|
||||
|
||||
return sr25519.sign(secretKey, u8aToU8a(message));
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringToU8a } from '@pezkuwi/util';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a.js';
|
||||
import { sr25519PairFromSeed } from './pair/fromSeed.js';
|
||||
import { sr25519Sign } from './sign.js';
|
||||
import { sr25519Verify } from './verify.js';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
describe('verify', (): void => {
|
||||
it('can sign and verify a message', (): void => {
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const signature = sr25519Sign(MESSAGE, pair);
|
||||
|
||||
expect(sr25519Verify(MESSAGE, signature, pair.publicKey)).toBe(true);
|
||||
});
|
||||
|
||||
it('throws error when publicKey lengths do not match', (): void => {
|
||||
expect(
|
||||
() => sr25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(64),
|
||||
new Uint8Array(31)
|
||||
)
|
||||
).toThrow(/Invalid publicKey/);
|
||||
});
|
||||
|
||||
it('throws error when signature lengths do not match', (): void => {
|
||||
expect(
|
||||
() => sr25519Verify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(65),
|
||||
new Uint8Array(32)
|
||||
)
|
||||
).toThrow(/Invalid signature/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
/**
|
||||
* @name sr25519Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function sr25519Verify (message: string | Uint8Array, signature: string | Uint8Array, publicKey: string | Uint8Array): boolean {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
if (publicKeyU8a.length !== 32) {
|
||||
throw new Error(`Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
|
||||
} else if (signatureU8a.length !== 64) {
|
||||
throw new Error(`Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
}
|
||||
|
||||
return sr25519.verify(u8aToU8a(message), signatureU8a, publicKeyU8a);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../types.js';
|
||||
|
||||
import { randomBytes } from '@noble/hashes/utils';
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
const EMPTY_U8A = new Uint8Array();
|
||||
|
||||
/**
|
||||
* @name sr25519VrfSign
|
||||
* @description Sign with sr25519 vrf signing (deterministic)
|
||||
*/
|
||||
export function sr25519VrfSign (message: string | Uint8Array, { secretKey }: Partial<Keypair>, context: string | Uint8Array = EMPTY_U8A, extra: string | Uint8Array = EMPTY_U8A): Uint8Array {
|
||||
if (secretKey?.length !== 64) {
|
||||
throw new Error('Invalid secretKey, expected 64-bytes');
|
||||
}
|
||||
|
||||
return sr25519.vrf.sign(u8aToU8a(message), secretKey, u8aToU8a(context), u8aToU8a(extra), randomBytes);
|
||||
// return vrfSign(secretKey, u8aToU8a(context), u8aToU8a(message), u8aToU8a(extra));
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { stringToU8a, u8aEq } from '@pezkuwi/util';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a.js';
|
||||
import { sr25519PairFromSeed } from './pair/fromSeed.js';
|
||||
import { sr25519VrfSign } from './vrfSign.js';
|
||||
import { sr25519VrfVerify } from './vrfVerify.js';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
describe('vrf sign and verify', (): void => {
|
||||
it('has 96-byte proofs', (): void => {
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
|
||||
expect(sr25519VrfSign(MESSAGE, pair)).toHaveLength(96);
|
||||
});
|
||||
|
||||
it('signing is deterministic', (): void => {
|
||||
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 = sr25519PairFromSeed(randomAsU8a());
|
||||
const proof = sr25519VrfSign(MESSAGE, pair);
|
||||
|
||||
expect(sr25519VrfVerify(MESSAGE, proof, pair.publicKey)).toBe(true);
|
||||
});
|
||||
|
||||
it('can sign and verify a message (with context)', (): void => {
|
||||
const context = 'my context';
|
||||
const pair = sr25519PairFromSeed(randomAsU8a());
|
||||
const proof = sr25519VrfSign(MESSAGE, pair, context);
|
||||
|
||||
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 = sr25519PairFromSeed(randomAsU8a());
|
||||
const proof = sr25519VrfSign(MESSAGE, pair, context, extra);
|
||||
|
||||
expect(sr25519VrfVerify(MESSAGE, proof, pair.publicKey, context, extra)).toBe(true);
|
||||
});
|
||||
|
||||
it('throws error when publicKey lengths do not match', (): void => {
|
||||
expect(
|
||||
() => sr25519VrfVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(96),
|
||||
new Uint8Array(31)
|
||||
)
|
||||
).toThrow(/Invalid publicKey/);
|
||||
});
|
||||
|
||||
it('throws error when proof lengths do not match', (): void => {
|
||||
expect(
|
||||
() => sr25519VrfVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(99),
|
||||
new Uint8Array(32)
|
||||
)
|
||||
).toThrow(/Invalid vrfSign/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import * as sr25519 from '@scure/sr25519';
|
||||
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
|
||||
const EMPTY_U8A = new Uint8Array();
|
||||
|
||||
/**
|
||||
* @name sr25519VrfVerify
|
||||
* @description Verify with sr25519 vrf verification
|
||||
*/
|
||||
export function sr25519VrfVerify (message: string | Uint8Array, signOutput: string | Uint8Array, publicKey: string | Uint8Array, context: string | Uint8Array = EMPTY_U8A, extra: string | Uint8Array = EMPTY_U8A): boolean {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const proofU8a = u8aToU8a(signOutput);
|
||||
|
||||
if (publicKeyU8a.length !== 32) {
|
||||
throw new Error('Invalid publicKey, expected 32-bytes');
|
||||
} else if (proofU8a.length !== 96) {
|
||||
throw new Error('Invalid vrfSign output, expected 96 bytes');
|
||||
}
|
||||
|
||||
return sr25519.vrf.verify(u8aToU8a(message), proofU8a, publicKeyU8a, u8aToU8a(context), u8aToU8a(extra));
|
||||
}
|
||||
Reference in New Issue
Block a user