Skip to content

Commit

Permalink
Merge pull request #273 from alephium/support-private-keys-string
Browse files Browse the repository at this point in the history
Support private keys of string type
  • Loading branch information
Lbqds authored Sep 20, 2023
2 parents 299df68 + 6ea013b commit 72ae2e0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
12 changes: 11 additions & 1 deletion packages/cli/src/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.

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', () => {
Expand Down Expand Up @@ -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}`)
})
})
})
15 changes: 11 additions & 4 deletions packages/cli/src/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
NetworkId,
ContractInstance,
ExecutableScript,
ProjectArtifact
ProjectArtifact,
isHexString
} from '@alephium/web3'
import { PrivateKeyWallet } from '@alephium/web3-wallet'
import path from 'path'
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import {
web3,
Project,
DEFAULT_COMPILER_OPTIONS,
SignerProvider,
Script,
NetworkId,
ContractInstance
} from '@alephium/web3'
Expand All @@ -41,7 +39,7 @@ import path from 'path'
export interface Network<Settings = unknown> {
networkId?: number
nodeUrl: string
privateKeys: string[]
privateKeys: string[] | string
deploymentStatusFile?: string
confirmations?: number
settings: Settings
Expand Down
30 changes: 30 additions & 0 deletions packages/web3-wallet/src/privatekey-wallet.test.ts
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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)
}
})
})

0 comments on commit 72ae2e0

Please sign in to comment.