From 94b2feecad9840d5de58685ffe729251c8f886ef Mon Sep 17 00:00:00 2001 From: lbqds Date: Fri, 15 Sep 2023 18:59:43 +0800 Subject: [PATCH 1/2] Support private keys of string type --- packages/cli/src/deployment.test.ts | 12 +++++++++++- packages/cli/src/deployment.ts | 15 +++++++++++---- packages/cli/src/types.ts | 4 +--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/deployment.test.ts b/packages/cli/src/deployment.test.ts index f636bd6ac..4febf4158 100644 --- a/packages/cli/src/deployment.test.ts +++ b/packages/cli/src/deployment.test.ts @@ -18,7 +18,6 @@ along with the library. If not, see . import { PrivateKeyWallet } from '@alephium/web3-wallet' import { Deployments, DeploymentsPerAddress, recordEqual, validatePrivateKeys } from './deployment' -import { NodeProvider } from '@alephium/web3' import { web3 } from '@alephium/web3' describe('deployments', () => { @@ -65,14 +64,25 @@ describe('deployments', () => { it('should validate private keys', () => { web3.setCurrentNodeProvider('http://127.0.0.1') const privateKeys = [0, 1, 2, 3].map((group) => PrivateKeyWallet.Random(group).privateKey) + privateKeys.forEach((pk) => expect(validatePrivateKeys(pk).length).toEqual(1)) + + expect(() => validatePrivateKeys(`${privateKeys[0]},${privateKeys[1].slice(0, 63) + 'z'}`)).toThrowError( + 'Invalid private key at index 1, expected a hex-string of length 64' + ) + expect(() => validatePrivateKeys(`${privateKeys[0].slice(0, 62)},${privateKeys[1]}`)).toThrowError( + 'Invalid private key at index 0, expected a hex-string of length 64' + ) expect(() => validatePrivateKeys([])).toThrowError('No private key specified') + expect(() => validatePrivateKeys('')).toThrowError('No private key specified') Array.from([1, 2, 3, 4]).forEach((size) => { const keys = privateKeys.slice(0, size).sort((a, b) => (a > b ? 1 : -1)) expect(validatePrivateKeys(keys).length).toEqual(size) + expect(validatePrivateKeys(keys.join(',')).length).toEqual(size) keys.push(privateKeys[size - 1]) expect(() => validatePrivateKeys(keys)).toThrowError(`Duplicated private keys on group ${size - 1}`) + expect(() => validatePrivateKeys(keys.join(','))).toThrowError(`Duplicated private keys on group ${size - 1}`) }) }) }) diff --git a/packages/cli/src/deployment.ts b/packages/cli/src/deployment.ts index f01a1eae6..a9fa41c70 100644 --- a/packages/cli/src/deployment.ts +++ b/packages/cli/src/deployment.ts @@ -35,7 +35,8 @@ import { NetworkId, ContractInstance, ExecutableScript, - ProjectArtifact + ProjectArtifact, + isHexString } from '@alephium/web3' import { PrivateKeyWallet } from '@alephium/web3-wallet' import path from 'path' @@ -484,11 +485,17 @@ async function validateChainParams(networkId: number, groups: number[]): Promise } } -export function validatePrivateKeys(privateKeys: string[]): PrivateKeyWallet[] { - if (privateKeys.length === 0) { +export function validatePrivateKeys(privateKeys: string[] | string): PrivateKeyWallet[] { + const pks = typeof privateKeys === 'string' ? privateKeys.split(',') : privateKeys + if (pks.length === 0 || privateKeys === '') { throw new Error('No private key specified') } - const signers = privateKeys.map((privateKey) => new PrivateKeyWallet({ privateKey })) + const signers = pks.map((privateKey, index) => { + if (isHexString(privateKey) && privateKey.length === 64) { + return new PrivateKeyWallet({ privateKey }) + } + throw new Error(`Invalid private key at index ${index}, expected a hex-string of length 64`) + }) const groups = signers.map((signer) => signer.group) const sameGroups = groups.filter((group, index) => groups.indexOf(group) !== index) if (sameGroups.length > 0) { diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index 26973a91a..5006642be 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -30,8 +30,6 @@ import { web3, Project, DEFAULT_COMPILER_OPTIONS, - SignerProvider, - Script, NetworkId, ContractInstance } from '@alephium/web3' @@ -41,7 +39,7 @@ import path from 'path' export interface Network { networkId?: number nodeUrl: string - privateKeys: string[] + privateKeys: string[] | string deploymentStatusFile?: string confirmations?: number settings: Settings From 6ea013b2fd0d37709bad4d8d6fd9c017352e81d9 Mon Sep 17 00:00:00 2001 From: lbqds Date: Wed, 20 Sep 2023 16:52:59 +0800 Subject: [PATCH 2/2] Add tests for private key length --- .../web3-wallet/src/privatekey-wallet.test.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 packages/web3-wallet/src/privatekey-wallet.test.ts diff --git a/packages/web3-wallet/src/privatekey-wallet.test.ts b/packages/web3-wallet/src/privatekey-wallet.test.ts new file mode 100644 index 000000000..adac09aac --- /dev/null +++ b/packages/web3-wallet/src/privatekey-wallet.test.ts @@ -0,0 +1,30 @@ +/* +Copyright 2018 - 2022 The Alephium Authors +This file is part of the alephium project. + +The library is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the library. If not, see . +*/ + +import { web3 } from '@alephium/web3' +import { PrivateKeyWallet } from './privatekey-wallet' + +describe('privatekey wallet', () => { + it('test the length of private key', () => { + web3.setCurrentNodeProvider('http://127.0.0.1:22973') + for (let i = 0; i < 100; i++) { + const wallet = PrivateKeyWallet.Random() + expect(wallet.privateKey.length).toEqual(64) + } + }) +})