diff --git a/commander/src/bootstrapping/commands/account/create.ts b/commander/src/bootstrapping/commands/account/create.ts index b6bc7021a62..e87bc5b8df1 100644 --- a/commander/src/bootstrapping/commands/account/create.ts +++ b/commander/src/bootstrapping/commands/account/create.ts @@ -30,7 +30,7 @@ interface AccountInfo { const createAccount = (prefix: string): AccountInfo => { const generatedPassphrase = passphrase.Mnemonic.generateMnemonic(); - const { privateKey, publicKey } = cryptography.ed.getKeys(generatedPassphrase); + const { privateKey, publicKey } = cryptography.legacy.getKeys(generatedPassphrase); const blsPrivateKey = cryptography.bls.generatePrivateKey( Buffer.from(generatedPassphrase, 'utf-8'), ); diff --git a/commander/src/bootstrapping/commands/forging/config.ts b/commander/src/bootstrapping/commands/forging/config.ts index 0a06d91baad..2724a4b0238 100644 --- a/commander/src/bootstrapping/commands/forging/config.ts +++ b/commander/src/bootstrapping/commands/forging/config.ts @@ -81,7 +81,8 @@ export class ConfigCommand extends Command { const hashOnion = { count, distance, hashes }; const passphrase = passphraseSource ?? (await getPassphraseFromPrompt('passphrase', true)); - const address = cryptography.address.getAddressFromPassphrase(passphrase).toString('hex'); + const { publicKey } = cryptography.legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + const address = cryptography.address.getAddressFromPublicKey(publicKey).toString('hex'); const password = passwordSource ?? (await getPasswordFromPrompt('password', true)); const { encryptedPassphrase } = await encryptPassphrase(passphrase, password, false); const message = { address, encryptedPassphrase, hashOnion }; diff --git a/commander/src/bootstrapping/commands/transaction/create.ts b/commander/src/bootstrapping/commands/transaction/create.ts index d34fa7c2ec7..b514b905a12 100644 --- a/commander/src/bootstrapping/commands/transaction/create.ts +++ b/commander/src/bootstrapping/commands/transaction/create.ts @@ -30,6 +30,8 @@ import { ModuleMetadataJSON, } from 'lisk-framework'; import { PromiseResolvedType } from '../../../types'; +import { deriveKeypair } from '../../../utils/commons'; +import { DEFAULT_KEY_DERIVATION_PATH } from '../../../utils/config'; import { flagsWithParser } from '../../../utils/flags'; import { getDefaultPath } from '../../../utils/path'; import { getParamsFromPrompt, getPassphraseFromPrompt, getFileParams } from '../../../utils/reader'; @@ -57,6 +59,7 @@ interface CreateFlags { 'sender-public-key'?: string; nonce?: string; file?: string; + 'key-derivation-path': string; } interface Transaction { @@ -98,20 +101,21 @@ const getPassphraseAddressAndPublicKey = async (flags: CreateFlags) => { passphrase = ''; } else { passphrase = flags.passphrase ?? (await getPassphraseFromPrompt('passphrase', true)); - const result = cryptography.address.getAddressAndPublicKeyFromPassphrase(passphrase); - publicKey = result.publicKey; - address = result.address; + const keys = cryptography.legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + publicKey = keys.publicKey; + address = cryptography.address.getAddressFromPublicKey(publicKey); } return { address, passphrase, publicKey }; }; -const validateAndSignTransaction = ( +const validateAndSignTransaction = async ( transaction: Transaction, schema: RegisteredSchema, metadata: ModuleMetadataJSON[], networkIdentifier: string, passphrase: string, + keyDerivationPath: string, noSignature: boolean, ) => { const { params, ...transactionWithoutParams } = transaction; @@ -132,10 +136,11 @@ const validateAndSignTransaction = ( }; if (!noSignature) { + const { privateKey } = await deriveKeypair(passphrase, keyDerivationPath); return transactions.signTransaction( decodedTx, Buffer.from(networkIdentifier, 'hex'), - passphrase, + privateKey, paramsSchema, ); } @@ -161,6 +166,7 @@ const createTransactionOffline = async ( metadata, flags['network-identifier'] as string, passphrase, + flags['key-derivation-path'], flags['no-signature'], ); }; @@ -202,6 +208,7 @@ const createTransactionOnline = async ( metadata, nodeInfo.networkIdentifier, passphrase, + flags['key-derivation-path'], flags['no-signature'], ); }; @@ -265,6 +272,11 @@ export abstract class CreateCommand extends Command { 'Creates the transaction with provided sender publickey, when passphrase is not provided', }), 'data-path': flagsWithParser.dataPath, + 'key-derivation-path': flagParser.string({ + default: DEFAULT_KEY_DERIVATION_PATH, + description: 'Key derivation path to use to derive keypair from passphrase', + char: 'k', + }), pretty: flagsWithParser.pretty, file: flagsWithParser.file, }; diff --git a/commander/src/bootstrapping/commands/transaction/sign.ts b/commander/src/bootstrapping/commands/transaction/sign.ts index d780ef88f32..0b8c8266049 100644 --- a/commander/src/bootstrapping/commands/transaction/sign.ts +++ b/commander/src/bootstrapping/commands/transaction/sign.ts @@ -40,6 +40,8 @@ import { import { getDefaultPath } from '../../../utils/path'; import { isApplicationRunning } from '../../../utils/application'; import { PromiseResolvedType } from '../../../types'; +import { DEFAULT_KEY_DERIVATION_PATH } from '../../../utils/config'; +import { deriveKeypair } from '../../../utils/commons'; interface AuthAccount { nonce: string; @@ -61,6 +63,7 @@ interface SignFlags { 'sender-public-key': string | undefined; 'mandatory-keys': string[]; 'optional-keys': string[]; + 'key-derivation-path': string; } const signTransaction = async ( @@ -92,12 +95,14 @@ const signTransaction = async ( params: paramsObject, }; + const edKeys = cryptography.legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + // sign from multi sig account offline using input keys if (!flags['include-sender'] && !flags['sender-public-key']) { return transactions.signTransaction( decodedTx, networkIdentifierBuffer, - passphrase, + edKeys.privateKey, paramsSchema, ); } @@ -105,7 +110,7 @@ const signTransaction = async ( return transactions.signMultiSignatureTransaction( decodedTx, networkIdentifierBuffer, - passphrase, + edKeys.privateKey, keys, paramsSchema, flags['include-sender'], @@ -165,7 +170,8 @@ const signTransactionOnline = async ( // Sign non multi-sig transaction const transactionObject = decodeTransaction(registeredSchema, metadata, transactionHexStr); const passphrase = flags.passphrase ?? (await getPassphraseFromPrompt('passphrase', true)); - const address = cryptography.address.getAddressFromPassphrase(passphrase); + const edKeys = await deriveKeypair(passphrase, flags['key-derivation-path']); + const address = cryptography.address.getAddressFromPublicKey(edKeys.publicKey); let signedTransaction: Record; @@ -231,6 +237,11 @@ export abstract class SignCommand extends Command { 'network-identifier': flagsWithParser.networkIdentifier, 'sender-public-key': flagsWithParser.senderPublicKey, 'data-path': flagsWithParser.dataPath, + 'key-derivation-path': flagParser.string({ + default: DEFAULT_KEY_DERIVATION_PATH, + description: 'Key derivation path to use to derive keypair from passphrase', + char: 'k', + }), pretty: flagsWithParser.pretty, }; diff --git a/commander/src/commands/message/decrypt.ts b/commander/src/commands/message/decrypt.ts index 194b783f94e..08f5d37b5fb 100644 --- a/commander/src/commands/message/decrypt.ts +++ b/commander/src/commands/message/decrypt.ts @@ -13,7 +13,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { encrypt } from '@liskhq/lisk-cryptography'; +import { encrypt, legacy } from '@liskhq/lisk-cryptography'; import { flags as flagParser } from '@oclif/command'; import BaseCommand from '../../base'; @@ -37,10 +37,12 @@ const processInputs = ( throw new ValidationError('No message was provided.'); } - return encrypt.decryptMessageWithPassphrase( + const keys = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + + return encrypt.decryptMessageWithPrivateKey( message, nonce, - passphrase, + keys.privateKey, Buffer.from(senderPublicKey, 'hex'), ); }; diff --git a/commander/src/commands/message/encrypt.ts b/commander/src/commands/message/encrypt.ts index 75096bcead1..769c0f65d4c 100644 --- a/commander/src/commands/message/encrypt.ts +++ b/commander/src/commands/message/encrypt.ts @@ -13,7 +13,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { encrypt } from '@liskhq/lisk-cryptography'; +import { encrypt, legacy } from '@liskhq/lisk-cryptography'; import { flags as flagParser } from '@oclif/command'; import BaseCommand from '../../base'; @@ -30,11 +30,12 @@ const processInputs = (recipientPublicKey: string, passphrase: string, message?: if (!message) { throw new ValidationError('No message was provided.'); } + const keys = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); return { - ...encrypt.encryptMessageWithPassphrase( + ...encrypt.encryptMessageWithPrivateKey( message, - passphrase, + keys.privateKey, Buffer.from(recipientPublicKey, 'hex'), ), recipientPublicKey, diff --git a/commander/src/commands/message/sign.ts b/commander/src/commands/message/sign.ts index 36c605b9916..83bbf0c48d4 100644 --- a/commander/src/commands/message/sign.ts +++ b/commander/src/commands/message/sign.ts @@ -13,7 +13,7 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { ed } from '@liskhq/lisk-cryptography'; +import { ed, legacy } from '@liskhq/lisk-cryptography'; import { flags as flagParser } from '@oclif/command'; import BaseCommand from '../../base'; @@ -30,7 +30,8 @@ const processInputs = (passphrase: string, message?: string) => { throw new ValidationError('No message was provided.'); } - const signedMessageWithOnePassphrase = ed.signMessageWithPassphrase(message, passphrase); + const keys = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + const signedMessageWithOnePassphrase = ed.signMessageWithPrivateKey(message, keys.privateKey); return { ...signedMessageWithOnePassphrase, publicKey: signedMessageWithOnePassphrase.publicKey.toString('hex'), diff --git a/commander/src/utils/commons.ts b/commander/src/utils/commons.ts index 70d2f637eb2..989c1545826 100644 --- a/commander/src/utils/commons.ts +++ b/commander/src/utils/commons.ts @@ -42,7 +42,22 @@ export const encryptPassphrase = async ( return outputPublicKey ? { encryptedPassphrase, - publicKey: cryptography.ed.getKeys(passphrase).publicKey.toString('hex'), + publicKey: cryptography.legacy.getKeys(passphrase).publicKey.toString('hex'), } : { encryptedPassphrase }; }; + +export const deriveKeypair = async (passphrase: string, keyDerivationPath: string) => { + if (keyDerivationPath === 'legacy') { + return cryptography.legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + } + const privateKey = await cryptography.ed.getKeyPairFromPhraseAndPath( + passphrase, + keyDerivationPath, + ); + const publicKey = cryptography.ed.getPublicKeyFromPrivateKey(privateKey); + return { + publicKey, + privateKey, + }; +}; diff --git a/commander/src/utils/config.ts b/commander/src/utils/config.ts index 235180c6e22..f04109bd6c8 100644 --- a/commander/src/utils/config.ts +++ b/commander/src/utils/config.ts @@ -43,3 +43,5 @@ export const defaultConfig = { }, plugins: {}, }; + +export const DEFAULT_KEY_DERIVATION_PATH = "m/25519'/134'/0'/0'"; diff --git a/commander/src/utils/genesis_creation.ts b/commander/src/utils/genesis_creation.ts index 6ad0236f504..503d0791571 100644 --- a/commander/src/utils/genesis_creation.ts +++ b/commander/src/utils/genesis_creation.ts @@ -15,7 +15,7 @@ */ import { Mnemonic } from '@liskhq/lisk-passphrase'; import { Schema } from '@liskhq/lisk-codec'; -import { bls, address, utils, ed } from '@liskhq/lisk-cryptography'; +import { bls, address, utils, legacy } from '@liskhq/lisk-cryptography'; import { dposGenesisStoreSchema, DPoSModule, @@ -65,7 +65,7 @@ export const generateGenesisBlockDefaultDPoSAssets = (input: GenesisBlockDefault const accountList = []; for (let i = 0; i < input.numberOfAccounts; i += 1) { const passphrase = Mnemonic.generateMnemonic(256); - const keys = ed.getKeys(passphrase); + const keys = legacy.getKeys(passphrase); accountList.push({ publicKey: keys.publicKey, privateKey: keys.privateKey, @@ -77,7 +77,7 @@ export const generateGenesisBlockDefaultDPoSAssets = (input: GenesisBlockDefault const validatorList = []; for (let i = 0; i < input.numberOfValidators; i += 1) { const passphrase = Mnemonic.generateMnemonic(256); - const keys = ed.getKeys(passphrase); + const keys = legacy.getKeys(passphrase); const blsPrivateKey = bls.generatePrivateKey(Buffer.from(passphrase, 'utf-8')); const blsPublicKey = bls.getPublicKeyFromPrivateKey(blsPrivateKey); const blsPoP = bls.popProve(blsPrivateKey); diff --git a/commander/test/bootstrapping/commands/account/create.spec.ts b/commander/test/bootstrapping/commands/account/create.spec.ts index 43552110a15..3be86a54b71 100644 --- a/commander/test/bootstrapping/commands/account/create.spec.ts +++ b/commander/test/bootstrapping/commands/account/create.spec.ts @@ -47,16 +47,16 @@ describe('account:create', () => { await CreateCommand.run([], config); expect(JSON.parse(results[0])).toEqual([ { - publicKey: cryptography.ed.getKeys(defaultMnemonic).publicKey.toString('hex'), - privateKey: cryptography.ed.getKeys(defaultMnemonic).privateKey.toString('hex'), + publicKey: cryptography.legacy.getKeys(defaultMnemonic).publicKey.toString('hex'), + privateKey: cryptography.legacy.getKeys(defaultMnemonic).privateKey.toString('hex'), blsPublicKey: cryptography.bls.getPublicKeyFromPrivateKey(blsPrivateKey).toString('hex'), blsPrivateKey: blsPrivateKey.toString('hex'), address: cryptography.address.getLisk32AddressFromPublicKey( - cryptography.ed.getKeys(defaultMnemonic).publicKey, + cryptography.legacy.getKeys(defaultMnemonic).publicKey, 'lsk', ), binaryAddress: cryptography.address - .getAddressFromPassphrase(defaultMnemonic) + .getAddressFromPublicKey(cryptography.legacy.getKeys(defaultMnemonic).publicKey) .toString('hex'), passphrase: defaultMnemonic, }, @@ -70,32 +70,32 @@ describe('account:create', () => { await CreateCommand.run(['--count', defaultNumber.toString()], config); const result = [ { - publicKey: cryptography.ed.getKeys(defaultMnemonic).publicKey.toString('hex'), - privateKey: cryptography.ed.getKeys(defaultMnemonic).privateKey.toString('hex'), + publicKey: cryptography.legacy.getKeys(defaultMnemonic).publicKey.toString('hex'), + privateKey: cryptography.legacy.getKeys(defaultMnemonic).privateKey.toString('hex'), blsPublicKey: cryptography.bls.getPublicKeyFromPrivateKey(blsPrivateKey).toString('hex'), blsPrivateKey: blsPrivateKey.toString('hex'), address: cryptography.address.getLisk32AddressFromPublicKey( - cryptography.ed.getKeys(defaultMnemonic).publicKey, + cryptography.legacy.getKeys(defaultMnemonic).publicKey, 'lsk', ), binaryAddress: cryptography.address - .getAddressFromPassphrase(defaultMnemonic) + .getAddressFromPublicKey(cryptography.legacy.getKeys(defaultMnemonic).publicKey) .toString('hex'), passphrase: defaultMnemonic, }, { - publicKey: cryptography.ed.getKeys(secondDefaultMnemonic).publicKey.toString('hex'), - privateKey: cryptography.ed.getKeys(secondDefaultMnemonic).privateKey.toString('hex'), + publicKey: cryptography.legacy.getKeys(secondDefaultMnemonic).publicKey.toString('hex'), + privateKey: cryptography.legacy.getKeys(secondDefaultMnemonic).privateKey.toString('hex'), blsPublicKey: cryptography.bls .getPublicKeyFromPrivateKey(secondBlsPrivateKey) .toString('hex'), blsPrivateKey: secondBlsPrivateKey.toString('hex'), address: cryptography.address.getLisk32AddressFromPublicKey( - cryptography.ed.getKeys(secondDefaultMnemonic).publicKey, + cryptography.legacy.getKeys(secondDefaultMnemonic).publicKey, 'lsk', ), binaryAddress: cryptography.address - .getAddressFromPassphrase(secondDefaultMnemonic) + .getAddressFromPublicKey(cryptography.legacy.getKeys(secondDefaultMnemonic).publicKey) .toString('hex'), passphrase: secondDefaultMnemonic, }, diff --git a/commander/test/bootstrapping/commands/forging/config.spec.ts b/commander/test/bootstrapping/commands/forging/config.spec.ts index a90c5650f8a..578a7a3d340 100644 --- a/commander/test/bootstrapping/commands/forging/config.spec.ts +++ b/commander/test/bootstrapping/commands/forging/config.spec.ts @@ -69,7 +69,7 @@ describe('forging:config command', () => { jest.spyOn(process.stdout, 'write').mockImplementation(val => stdout.push(val as string) > -1); jest.spyOn(process.stderr, 'write').mockImplementation(val => stderr.push(val as string) > -1); jest.spyOn(ConfigCommand.prototype, 'printJSON').mockReturnValue(); - jest.spyOn(cryptography.ed, 'getKeys').mockReturnValue(defaultKeys as never); + jest.spyOn(cryptography.legacy, 'getKeys').mockReturnValue(defaultKeys as never); jest.spyOn(fs, 'ensureDirSync').mockReturnValue(); jest.spyOn(fs, 'writeJSONSync').mockReturnValue(); jest diff --git a/commander/test/bootstrapping/commands/passphrase/encrypt.spec.ts b/commander/test/bootstrapping/commands/passphrase/encrypt.spec.ts index 9276eb1993d..886f2b09d0b 100644 --- a/commander/test/bootstrapping/commands/passphrase/encrypt.spec.ts +++ b/commander/test/bootstrapping/commands/passphrase/encrypt.spec.ts @@ -56,7 +56,7 @@ describe('passphrase:encrypt', () => { jest.spyOn(process.stdout, 'write').mockImplementation(val => stdout.push(val as string) > -1); jest.spyOn(process.stderr, 'write').mockImplementation(val => stderr.push(val as string) > -1); jest.spyOn(EncryptCommand.prototype, 'printJSON').mockReturnValue(); - jest.spyOn(cryptography.ed, 'getKeys').mockReturnValue(defaultKeys as never); + jest.spyOn(cryptography.legacy, 'getKeys').mockReturnValue(defaultKeys as never); jest .spyOn(cryptography.encrypt, 'encryptMessageWithPassword') .mockResolvedValue(encryptedPassphraseObject as never); diff --git a/commander/test/bootstrapping/commands/transaction/create.spec.ts b/commander/test/bootstrapping/commands/transaction/create.spec.ts index 95f65bbca76..3b2345f10e6 100644 --- a/commander/test/bootstrapping/commands/transaction/create.spec.ts +++ b/commander/test/bootstrapping/commands/transaction/create.spec.ts @@ -35,7 +35,7 @@ describe('transaction:create command', () => { '{"votes":[{"delegateAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","amount":100},{"delegateAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","amount":-50}]}'; const unVoteParams = '{"votes":[{"delegateAddress":"ab0041a7d3f7b2c290b5b834d46bdc7b7eb85815","amount":-50}]}'; - const { publicKey } = cryptography.address.getAddressAndPublicKeyFromPassphrase(passphrase); + const { publicKey } = cryptography.legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); const senderPublicKey = publicKey.toString('hex'); const mockEncodedTransaction = Buffer.from('encoded transaction'); const mockJSONTransaction = { diff --git a/commander/test/commands/message/encrypt.spec.ts b/commander/test/commands/message/encrypt.spec.ts index c57558c37f7..f819e7e6859 100644 --- a/commander/test/commands/message/encrypt.spec.ts +++ b/commander/test/commands/message/encrypt.spec.ts @@ -49,7 +49,7 @@ describe('message:encrypt', () => { jest.spyOn(process.stderr, 'write').mockImplementation(val => stderr.push(val as string) > -1); jest.spyOn(inquirer, 'prompt').mockResolvedValue({ passphrase: defaultInputs }); jest.spyOn(readerUtils, 'readFileSource').mockResolvedValue(message); - jest.spyOn(cryptography.encrypt, 'encryptMessageWithPassphrase').mockReturnValue({ + jest.spyOn(cryptography.encrypt, 'encryptMessageWithPrivateKey').mockReturnValue({ encryptedMessage: defaultEncryptedMessage.message, nonce: defaultEncryptedMessage.nonce, }); diff --git a/commander/test/helpers/transactions.ts b/commander/test/helpers/transactions.ts index 9af51701b2a..ff2defb116b 100644 --- a/commander/test/helpers/transactions.ts +++ b/commander/test/helpers/transactions.ts @@ -158,7 +158,7 @@ export const createTransferTransaction = ({ }, }, networkIdentifier, - account.passphrase, + Buffer.from(account.privateKey, 'hex'), tokenTransferParamsSchema, ) as any; diff --git a/elements/lisk-api-client/src/transaction.ts b/elements/lisk-api-client/src/transaction.ts index 160e9cd58aa..3d2af967999 100644 --- a/elements/lisk-api-client/src/transaction.ts +++ b/elements/lisk-api-client/src/transaction.ts @@ -17,7 +17,7 @@ import { signMultiSignatureTransaction, computeMinFee, } from '@liskhq/lisk-transactions'; -import { address as cryptoAddress } from '@liskhq/lisk-cryptography'; +import { address as cryptoAddress, ed } from '@liskhq/lisk-cryptography'; import { validator } from '@liskhq/lisk-validator'; import { codec } from '@liskhq/lisk-codec'; import { @@ -87,7 +87,7 @@ export class Transaction { params: T; signatures?: string[]; }, - passphrase: string, + privateKeyHex: string, options?: { includeSenderSignature?: boolean; multisignatureKeys?: { @@ -98,7 +98,9 @@ export class Transaction { ): Promise> { const txInput = input; const networkIdentifier = Buffer.from(this._nodeInfo.networkIdentifier, 'hex'); - const { publicKey, address } = cryptoAddress.getAddressAndPublicKeyFromPassphrase(passphrase); + const privateKey = Buffer.from(privateKeyHex, 'hex'); + const publicKey = ed.getPublicKeyFromPrivateKey(privateKey); + const address = cryptoAddress.getAddressFromPublicKey(publicKey); let authAccount: AuthAccount | undefined; try { authAccount = await this._channel.invoke('auth_getAuthAccount', { @@ -165,7 +167,7 @@ export class Transaction { const signedTx = signMultiSignatureTransaction( rawTx, networkIdentifier, - passphrase, + privateKey, { mandatoryKeys: authAccount.mandatoryKeys.map(k => Buffer.from(k, 'hex')), optionalKeys: authAccount.optionalKeys.map(k => Buffer.from(k, 'hex')), @@ -179,7 +181,7 @@ export class Transaction { const signedTx = signMultiSignatureTransaction( rawTx, networkIdentifier, - passphrase, + privateKey, { mandatoryKeys: authAccount.mandatoryKeys.map(k => Buffer.from(k, 'hex')), optionalKeys: authAccount.optionalKeys.map(k => Buffer.from(k, 'hex')), @@ -189,7 +191,7 @@ export class Transaction { ); return this.toJSON(signedTx) as DecodedTransactionJSON; } - const signedTx = signTransaction(rawTx, networkIdentifier, passphrase, commandSchema); + const signedTx = signTransaction(rawTx, networkIdentifier, privateKey, commandSchema); return this.toJSON(signedTx) as DecodedTransactionJSON; } @@ -213,7 +215,7 @@ export class Transaction { public async sign( transaction: Record, - passphrases: string[], + privateKeyHexes: string[], options?: { includeSenderSignature?: boolean; multisignatureKeys?: { @@ -234,11 +236,12 @@ export class Transaction { address: address.toString('hex'), }); if (authAccount.numberOfSignatures > 0) { - for (const passphrase of passphrases) { + for (const privateKeyHex of privateKeyHexes) { + const privateKey = Buffer.from(privateKeyHex, 'hex'); signMultiSignatureTransaction( decodedTx, networkIdentifier, - passphrase, + privateKey, { mandatoryKeys: authAccount.mandatoryKeys.map(k => Buffer.from(k, 'hex')), optionalKeys: authAccount.optionalKeys.map(k => Buffer.from(k, 'hex')), @@ -250,11 +253,12 @@ export class Transaction { return this.toJSON(decodedTx); } if (options?.multisignatureKeys && options?.includeSenderSignature) { - for (const passphrase of passphrases) { + for (const privateKeyHex of privateKeyHexes) { + const privateKey = Buffer.from(privateKeyHex, 'hex'); signMultiSignatureTransaction( decodedTx, networkIdentifier, - passphrase, + privateKey, { mandatoryKeys: options.multisignatureKeys.mandatoryKeys.map(k => Buffer.from(k, 'hex')), optionalKeys: options.multisignatureKeys.optionalKeys.map(k => Buffer.from(k, 'hex')), @@ -268,7 +272,7 @@ export class Transaction { const signedTx = signTransaction( decodedTx, networkIdentifier, - passphrases[0], + Buffer.from(privateKeyHexes[0], 'hex'), commandSchema, ) as DecodedTransaction; return this.toJSON(signedTx); diff --git a/elements/lisk-api-client/test/unit/transaction.spec.ts b/elements/lisk-api-client/test/unit/transaction.spec.ts index a1fc42a6855..82a01e11e7d 100644 --- a/elements/lisk-api-client/test/unit/transaction.spec.ts +++ b/elements/lisk-api-client/test/unit/transaction.spec.ts @@ -14,7 +14,7 @@ */ import { when } from 'jest-when'; -import { utils, address } from '@liskhq/lisk-cryptography'; +import { utils, legacy } from '@liskhq/lisk-cryptography'; import { Transaction } from '../../src/transaction'; import { metadata, nodeInfo, schema, tx } from '../utils/transaction'; @@ -25,8 +25,14 @@ describe('transaction', () => { 'trim elegant oven term access apple obtain error grain excite lawn neck', 'faculty inspire crouch quit sorry vague hard ski scrap jaguar garment limb', ]; + const privateKeys = passphrases.map(p => + legacy.getPrivateAndPublicKeyFromPassphrase(p).privateKey.toString('hex'), + ); const passphrase1 = 'trim elegant oven term access apple obtain error grain excite lawn neck'; - const { publicKey: publicKey1 } = address.getAddressAndPublicKeyFromPassphrase(passphrase1); + const privateKey1 = legacy + .getPrivateAndPublicKeyFromPassphrase(passphrase1) + .privateKey.toString('hex'); + const { publicKey: publicKey1 } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase1); const publicKey2 = Buffer.from( 'fa406b6952d377f0278920e3eb8da919e4cf5c68b02eeba5d8b3334fdc0369b6', 'hex', @@ -143,7 +149,7 @@ describe('transaction', () => { describe('create', () => { describe('when called with a valid transaction', () => { it('should return created tx', async () => { - const returnedTx = await transaction.create(validTransaction, passphrase1); + const returnedTx = await transaction.create(validTransaction, privateKey1); expect(returnedTx.signatures).toHaveLength(1); expect(returnedTx.signatures).toMatchSnapshot(); }); @@ -152,7 +158,7 @@ describe('transaction', () => { describe('when called without module id and module name in input', () => { it('should throw error', async () => { await expect( - transaction.create({ ...validTransaction, moduleID: undefined }, passphrase1), + transaction.create({ ...validTransaction, moduleID: undefined }, privateKey1), ).rejects.toThrow('Missing moduleID and moduleName'); }); }); @@ -160,7 +166,7 @@ describe('transaction', () => { describe('when called without asset id and asset name in input', () => { it('should throw error', async () => { await expect( - transaction.create({ ...validTransaction, commandID: undefined }, passphrase1), + transaction.create({ ...validTransaction, commandID: undefined }, privateKey1), ).rejects.toThrow('Missing commandID and commandName'); }); }); @@ -170,7 +176,7 @@ describe('transaction', () => { await expect( transaction.create( { ...validTransaction, moduleID: undefined, moduleName: 'newModule' }, - passphrase1, + privateKey1, ), ).rejects.toThrow('Module corresponding to name newModule not registered.'); }); @@ -181,7 +187,7 @@ describe('transaction', () => { await expect( transaction.create( { ...validTransaction, commandID: undefined, commandName: 'newAsset' }, - passphrase1, + privateKey1, ), ).rejects.toThrow('Command corresponding to name newAsset not registered.'); }); @@ -193,7 +199,7 @@ describe('transaction', () => { .calledWith('auth_getAuthAccount', expect.anything()) .mockRejectedValue(new Error('endpoint does not exist') as never); await expect( - transaction.create({ ...validTransaction, nonce: undefined }, passphrase1), + transaction.create({ ...validTransaction, nonce: undefined }, privateKey1), ).rejects.toThrow('Auth module is not registered or does not have "getAuthAccount"'); }); }); @@ -201,7 +207,7 @@ describe('transaction', () => { describe('when called with negative nonce in input', () => { it('should throw error', async () => { await expect( - transaction.create({ ...validTransaction, nonce: BigInt(-2452) }, passphrase1), + transaction.create({ ...validTransaction, nonce: BigInt(-2452) }, privateKey1), ).rejects.toThrow('Nonce must be greater or equal to zero'); }); }); @@ -210,7 +216,7 @@ describe('transaction', () => { it('should return created tx', async () => { const returnedTx = await transaction.create( { ...validTransaction, nonce: BigInt(0) }, - passphrase1, + privateKey1, ); expect(returnedTx.signatures).toHaveLength(1); expect(returnedTx.signatures).toMatchSnapshot(); @@ -221,7 +227,7 @@ describe('transaction', () => { it('should return created tx', async () => { const returnedTx = await transaction.create( { ...validTransaction, senderPublicKey: undefined }, - passphrase1, + privateKey1, ); expect(returnedTx.signatures).toHaveLength(1); expect(returnedTx.signatures).toMatchSnapshot(); @@ -239,7 +245,7 @@ describe('transaction', () => { when(channelMock.invoke) .calledWith('auth_getAuthAccount', expect.anything()) .mockResolvedValue(multisigAccount as never); - const returnedTx = await transaction.create(validTransaction, passphrase1); + const returnedTx = await transaction.create(validTransaction, privateKey1); expect(returnedTx.signatures).toHaveLength(2); expect(returnedTx.signatures).toMatchSnapshot(); }); @@ -254,7 +260,7 @@ describe('transaction', () => { optionalKeys: [publicKey2].map(k => k.toString('hex')), }, }; - const returnedTx = await transaction.create(validTransaction, passphrase1, options); + const returnedTx = await transaction.create(validTransaction, privateKey1, options); expect(returnedTx.signatures).toHaveLength(1); expect(returnedTx.signatures).toMatchSnapshot(); }); @@ -264,7 +270,7 @@ describe('transaction', () => { describe('sign', () => { describe('when called with a valid transation', () => { it('should return some signed transaction', () => { - const returnedTx = transaction.sign(validTransaction, passphrases); + const returnedTx = transaction.sign(validTransaction, privateKeys); expect(returnedTx).toBeDefined(); }); }); @@ -280,7 +286,7 @@ describe('transaction', () => { when(channelMock.invoke) .calledWith('auth_getAuthAccount', expect.anything()) .mockResolvedValue(multisigAccount as never); - const returnedTx = await transaction.sign(validTransaction, passphrases); + const returnedTx = await transaction.sign(validTransaction, privateKeys); expect(returnedTx.signatures).toHaveLength(2); expect(returnedTx.signatures).toMatchSnapshot(); }); @@ -295,7 +301,7 @@ describe('transaction', () => { optionalKeys: [publicKey2].map(k => k.toString('hex')), }, }; - const returnedTx = await transaction.sign(validTransaction, passphrases, options); + const returnedTx = await transaction.sign(validTransaction, privateKeys, options); expect(returnedTx.signatures).toHaveLength(2); expect(returnedTx.signatures).toMatchSnapshot(); }); diff --git a/elements/lisk-chain/test/utils/block.ts b/elements/lisk-chain/test/utils/block.ts index df4a420328b..4ef7ea1b645 100644 --- a/elements/lisk-chain/test/utils/block.ts +++ b/elements/lisk-chain/test/utils/block.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. */ -import { utils, address, ed } from '@liskhq/lisk-cryptography'; +import { utils, address, legacy } from '@liskhq/lisk-cryptography'; import { Mnemonic } from '@liskhq/lisk-passphrase'; import { MerkleTree } from '@liskhq/lisk-tree'; import * as genesis from '../fixtures/genesis_block.json'; @@ -26,7 +26,7 @@ export const defaultNetworkIdentifier = Buffer.from( const getKeyPair = (): { publicKey: Buffer; privateKey: Buffer } => { const passphrase = Mnemonic.generateMnemonic(); - return ed.getPrivateAndPublicKeyFromPassphrase(passphrase); + return legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); }; export const createFakeBlockHeader = (header?: Partial): BlockHeader => diff --git a/elements/lisk-chain/test/utils/transaction.ts b/elements/lisk-chain/test/utils/transaction.ts index d674d73d4d6..ce0ea059895 100644 --- a/elements/lisk-chain/test/utils/transaction.ts +++ b/elements/lisk-chain/test/utils/transaction.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. */ -import { utils, ed } from '@liskhq/lisk-cryptography'; +import { utils, ed, legacy } from '@liskhq/lisk-cryptography'; import { defaultNetworkIdentifier } from './block'; import { Transaction } from '../../src/transaction'; import { TAG_TRANSACTION } from '../../src'; @@ -41,7 +41,7 @@ export const getTransaction = (input?: { nonce?: bigint }): Transaction => { TAG_TRANSACTION, defaultNetworkIdentifier, tx.getSigningBytes(), - genesisAddress.passphrase, + legacy.getPrivateAndPublicKeyFromPassphrase(genesisAddress.passphrase).privateKey, ); tx.signatures.push(signature); diff --git a/elements/lisk-client/test/lisk-cryptography/address.spec.ts b/elements/lisk-client/test/lisk-cryptography/address.spec.ts index cd9817eb997..ed642ba9f63 100644 --- a/elements/lisk-client/test/lisk-cryptography/address.spec.ts +++ b/elements/lisk-client/test/lisk-cryptography/address.spec.ts @@ -19,8 +19,6 @@ const { address: { getAddressFromPublicKey, getLisk32AddressFromPublicKey, - getAddressAndPublicKeyFromPassphrase, - getAddressFromPassphrase, getAddressFromPrivateKey, validateLisk32Address, getAddressFromLisk32Address, @@ -29,7 +27,6 @@ const { } = cryptography; describe('keys', () => { - const defaultPassphrase = 'secret'; const defaultPrivateKey = Buffer.from( '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09', 'hex', @@ -40,25 +37,6 @@ describe('keys', () => { ); const defaultAddress = Buffer.from('3a971fd02b4a07fc20aad1936d3cb1d263b96e0f', 'hex'); - const defaultAddressAndPublicKey = { - publicKey: defaultPublicKey, - address: defaultAddress, - }; - - describe('#getAddressAndPublicKeyFromPassphrase', () => { - it('should create correct address and publicKey', () => { - expect(getAddressAndPublicKeyFromPassphrase(defaultPassphrase)).toEqual( - defaultAddressAndPublicKey, - ); - }); - }); - - describe('#getAddressFromPassphrase', () => { - it('should create correct address', () => { - expect(getAddressFromPassphrase(defaultPassphrase)).toEqual(defaultAddress); - }); - }); - describe('#getAddressFromPrivateKey', () => { it('should create correct address', () => { expect(getAddressFromPrivateKey(defaultPrivateKey.slice(0, 64))).toEqual(defaultAddress); diff --git a/elements/lisk-client/test/lisk-cryptography/ed.spec.ts b/elements/lisk-client/test/lisk-cryptography/ed.spec.ts index 499464de979..388ffdcdc45 100644 --- a/elements/lisk-client/test/lisk-cryptography/ed.spec.ts +++ b/elements/lisk-client/test/lisk-cryptography/ed.spec.ts @@ -17,18 +17,15 @@ import { cryptography } from '../../src'; const { ed: { - signMessageWithPassphrase, + signMessageWithPrivateKey, verifyMessageWithPublicKey, printSignedMessage, signAndPrintMessage, signData, - signDataWithPassphrase, signDataWithPrivateKey, verifyData, getKeyPairFromPhraseAndPath, getPublicKeyFromPrivateKey, - getKeys, - getPrivateAndPublicKeyFromPassphrase, }, utils: { createMessageTag }, } = cryptography; @@ -46,7 +43,6 @@ describe('sign', () => { const MAX_UINT32 = 4294967295; const tag = createMessageTag('TST'); const networkIdentifier = Buffer.from('a5df2ed79994178c10ac168d6d977ef45cd525e95b7a8624', 'hex'); - const defaultPassphrase = 'minute omit local rare sword knee banner pair rib museum shadow juice'; const defaultPrivateKey = '314852d7afb0d4c283692fef8a2cb40e30c7a5df2ed79994178c10ac168d6d977ef45cd525e95b7a86244bbd4eb4550914ad06301013958f4dd64d32ef7bc588'; const defaultPublicKey = '7ef45cd525e95b7a86244bbd4eb4550914ad06301013958f4dd64d32ef7bc588'; @@ -80,7 +76,10 @@ ${defaultSignature} describe('#signMessageWithPassphrase', () => { it('should create a signed message using a secret passphrase', () => { - const signedMessage = signMessageWithPassphrase(defaultMessage, defaultPassphrase); + const signedMessage = signMessageWithPrivateKey( + defaultMessage, + Buffer.from(defaultPrivateKey, 'hex'), + ); expect(signedMessage).toEqual(defaultSignedMessage); }); }); @@ -121,38 +120,6 @@ ${defaultSignature} }); }); - describe('#getPrivateAndPublicKeyFromPassphrase', () => { - let keyPair: any; - - beforeEach(() => { - keyPair = getPrivateAndPublicKeyFromPassphrase(defaultPassphrase); - }); - - it('should generate the correct publicKey from a passphrase', () => { - expect(keyPair).toHaveProperty('publicKey', Buffer.from(defaultPublicKey, 'hex')); - }); - - it('should generate the correct privateKey from a passphrase', () => { - expect(keyPair).toHaveProperty('privateKey', Buffer.from(defaultPrivateKey, 'hex')); - }); - }); - - describe('#getKeys', () => { - let keyPair: any; - - beforeEach(() => { - keyPair = getKeys(defaultPassphrase); - }); - - it('should generate the correct publicKey from a passphrase', () => { - expect(keyPair).toHaveProperty('publicKey', Buffer.from(defaultPublicKey, 'hex')); - }); - - it('should generate the correct privateKey from a passphrase', () => { - expect(keyPair).toHaveProperty('privateKey', Buffer.from(defaultPrivateKey, 'hex')); - }); - }); - describe('#printSignedMessage', () => { it('should wrap a single signed message into a printed Lisk template', () => { const printedMessage = printSignedMessage({ @@ -166,7 +133,10 @@ ${defaultSignature} describe('#signAndPrintMessage', () => { it('should sign the message once and wrap it into a printed Lisk template', () => { - const signedAndPrintedMessage = signAndPrintMessage(defaultMessage, defaultPassphrase); + const signedAndPrintedMessage = signAndPrintMessage( + defaultMessage, + Buffer.from(defaultPrivateKey, 'hex'), + ); expect(signedAndPrintedMessage).toBe(defaultPrintedMessage); }); }); @@ -175,20 +145,12 @@ ${defaultSignature} let signature: Buffer; beforeEach(async () => { - signature = signData(tag, networkIdentifier, defaultData, defaultPassphrase); - return Promise.resolve(); - }); - - it('should sign a transaction', () => { - expect(signature).toEqual(Buffer.from(defaultDataSignature, 'hex')); - }); - }); - - describe('#signDataWithPassphrase', () => { - let signature: Buffer; - - beforeEach(async () => { - signature = signDataWithPassphrase(tag, networkIdentifier, defaultData, defaultPassphrase); + signature = signData( + tag, + networkIdentifier, + defaultData, + Buffer.from(defaultPrivateKey, 'hex'), + ); return Promise.resolve(); }); diff --git a/elements/lisk-client/test/lisk-cryptography/encrypt.spec.ts b/elements/lisk-client/test/lisk-cryptography/encrypt.spec.ts index 9369ce53ca3..2023e7ab568 100644 --- a/elements/lisk-client/test/lisk-cryptography/encrypt.spec.ts +++ b/elements/lisk-client/test/lisk-cryptography/encrypt.spec.ts @@ -48,10 +48,10 @@ describe('encrypt', () => { beforeEach(async () => { jest - .spyOn(cryptography.address, 'getAddressAndPublicKeyFromPassphrase') + .spyOn(cryptography.legacy, 'getPrivateAndPublicKeyFromPassphrase') .mockImplementation(() => { return { - address: defaultPrivateKey, + privateKey: defaultPrivateKey, publicKey: defaultPublicKey, }; }); diff --git a/elements/lisk-client/test/lisk-cryptography/utils.spec.ts b/elements/lisk-client/test/lisk-cryptography/utils.spec.ts index d36fd5f66bc..bdc5c4cd6c8 100644 --- a/elements/lisk-client/test/lisk-cryptography/utils.spec.ts +++ b/elements/lisk-client/test/lisk-cryptography/utils.spec.ts @@ -18,7 +18,6 @@ import { cryptography } from '../../src'; const { utils: { generateHashOnionSeed, - bufferToHex, hexToBuffer, intToBuffer, getNetworkIdentifier, @@ -31,13 +30,6 @@ describe('buffer', () => { const defaultBuffer = Buffer.from('\xe5\xe4\xf6'); const defaultHex = 'c3a5c3a4c3b6'; - describe('#bufferToHex', () => { - it('should create a hex string from a Buffer', () => { - const hex = bufferToHex(defaultBuffer); - expect(hex).toBe(defaultHex); - }); - }); - describe('#hexToBuffer', () => { it('should create a Buffer from a hex string', () => { const buffer = hexToBuffer(defaultHex); diff --git a/elements/lisk-cryptography/src/address.ts b/elements/lisk-cryptography/src/address.ts index 7680a2e5bb0..af046eaa130 100644 --- a/elements/lisk-cryptography/src/address.ts +++ b/elements/lisk-cryptography/src/address.ts @@ -13,7 +13,6 @@ * */ import { BINARY_ADDRESS_LENGTH, DEFAULT_LISK32_ADDRESS_PREFIX } from './constants'; -import { getKeys } from './ed'; import { getPublicKey } from './nacl'; import { hash } from './utils'; @@ -61,24 +60,6 @@ export const getAddressFromPublicKey = (publicKey: Buffer): Buffer => { return truncatedBuffer; }; -export const getAddressAndPublicKeyFromPassphrase = ( - passphrase: string, -): { readonly address: Buffer; readonly publicKey: Buffer } => { - const { publicKey } = getKeys(passphrase); - const address = getAddressFromPublicKey(publicKey); - - return { - address, - publicKey, - }; -}; - -export const getAddressFromPassphrase = (passphrase: string): Buffer => { - const { publicKey } = getKeys(passphrase); - - return getAddressFromPublicKey(publicKey); -}; - export const getAddressFromPrivateKey = (privateKey: Buffer): Buffer => { const publicKey = getPublicKey(privateKey); @@ -136,14 +117,6 @@ export const getLisk32AddressFromPublicKey = ( prefix = DEFAULT_LISK32_ADDRESS_PREFIX, ): string => `${prefix}${addressToLisk32(getAddressFromPublicKey(publicKey))}`; -export const getLisk32AddressFromPassphrase = ( - passphrase: string, - prefix = DEFAULT_LISK32_ADDRESS_PREFIX, -): string => { - const { publicKey } = getAddressAndPublicKeyFromPassphrase(passphrase); - return getLisk32AddressFromPublicKey(publicKey, prefix); -}; - const LISK32_ADDRESS_LENGTH = 41; const LISK32_CHARSET = 'zxvcpmbn3465o978uyrtkqew2adsjhfg'; diff --git a/elements/lisk-cryptography/src/bls.ts b/elements/lisk-cryptography/src/bls.ts index 4df7b832c37..2b233ecd8ef 100644 --- a/elements/lisk-cryptography/src/bls.ts +++ b/elements/lisk-cryptography/src/bls.ts @@ -57,14 +57,14 @@ const writeBit = (buf: Buffer, bit: number, val: boolean): void => { } }; -export const signBLS = ( +export const signData = ( tag: string, networkIdentifier: Buffer, data: Buffer, privateKey: Buffer, ): Buffer => blsSign(privateKey, hash(tagMessage(tag, networkIdentifier, data))); -export const verifyBLS = ( +export const verifyData = ( tag: string, networkIdentifier: Buffer, data: Buffer, diff --git a/elements/lisk-cryptography/src/ed.ts b/elements/lisk-cryptography/src/ed.ts index 524af4f2cd5..0ad1a219a8e 100644 --- a/elements/lisk-cryptography/src/ed.ts +++ b/elements/lisk-cryptography/src/ed.ts @@ -51,13 +51,6 @@ export const digestMessage = (message: string): Buffer => { export const getPublicKeyFromPrivateKey = (pk: Buffer): Buffer => getPublicKey(pk); -export const getPrivateAndPublicKeyFromPassphrase = (passphrase: string) => { - const hashed = hash(passphrase, 'utf8'); - return getKeyPair(hashed); -}; - -export const getKeys = getPrivateAndPublicKeyFromPassphrase; - const getMasterKeyFromSeed = (seed: Buffer) => { const hmac = crypto.createHmac('sha512', ED25519_CURVE); const digest = hmac.update(seed).digest(); @@ -97,18 +90,18 @@ export const getKeyPairFromPhraseAndPath = async ( return getKeyPair(node.key).privateKey; }; -export interface SignedMessageWithOnePassphrase { +export interface SignedMessageWithPrivateKey { readonly message: string; readonly publicKey: Buffer; readonly signature: Buffer; } -export const signMessageWithPassphrase = ( +export const signMessageWithPrivateKey = ( message: string, - passphrase: string, -): SignedMessageWithOnePassphrase => { + privateKey: Buffer, +): SignedMessageWithPrivateKey => { const msgBytes = digestMessage(message); - const { privateKey, publicKey } = getPrivateAndPublicKeyFromPassphrase(passphrase); + const publicKey = getPublicKey(privateKey); const signature = signDetached(msgBytes, privateKey); return { @@ -122,7 +115,7 @@ export const verifyMessageWithPublicKey = ({ message, publicKey, signature, -}: SignedMessageWithOnePassphrase): boolean => { +}: SignedMessageWithPrivateKey): boolean => { const msgBytes = digestMessage(message); if (publicKey.length !== NACL_SIGN_PUBLICKEY_LENGTH) { @@ -160,8 +153,8 @@ export const printSignedMessage = ({ message, signature, publicKey }: SignedMess .filter(Boolean) .join('\n'); -export const signAndPrintMessage = (message: string, passphrase: string): string => { - const signedMessage = signMessageWithPassphrase(message, passphrase); +export const signAndPrintMessage = (message: string, privateKey: Buffer): string => { + const signedMessage = signMessageWithPrivateKey(message, privateKey); return printSignedMessage(signedMessage); }; @@ -173,18 +166,7 @@ export const signDataWithPrivateKey = ( privateKey: Buffer, ): Buffer => signDetached(hash(tagMessage(tag, networkIdentifier, data)), privateKey); -export const signDataWithPassphrase = ( - tag: string, - networkIdentifier: Buffer, - data: Buffer, - passphrase: string, -): Buffer => { - const { privateKey } = getPrivateAndPublicKeyFromPassphrase(passphrase); - - return signDataWithPrivateKey(tag, networkIdentifier, data, privateKey); -}; - -export const signData = signDataWithPassphrase; +export const signData = signDataWithPrivateKey; export const verifyData = ( tag: string, diff --git a/elements/lisk-cryptography/src/encrypt.ts b/elements/lisk-cryptography/src/encrypt.ts index 5eb6e145d88..08875563018 100644 --- a/elements/lisk-cryptography/src/encrypt.ts +++ b/elements/lisk-cryptography/src/encrypt.ts @@ -16,12 +16,8 @@ import { argon2id } from 'hash-wasm'; import * as querystring from 'querystring'; import * as crypto from 'crypto'; import * as ed2curve from 'ed2curve'; - -// eslint-disable-next-line import/no-cycle -import { getPrivateAndPublicKeyFromPassphrase } from './ed'; -// eslint-disable-next-line import/no-cycle import { box, getRandomBytes, openBox } from './nacl'; -import { bufferToHex, hexToBuffer } from './utils'; +import { hexToBuffer } from './utils'; const PBKDF2_ITERATIONS = 1e6; const PBKDF2_KEYLEN = 32; @@ -37,13 +33,12 @@ export interface EncryptedMessageWithNonce { readonly nonce: string; } -export const encryptMessageWithPassphrase = ( +export const encryptMessageWithPrivateKey = ( message: string, - passphrase: string, + senderPrivateKey: Buffer, recipientPublicKey: Buffer, ): EncryptedMessageWithNonce => { - const { privateKey: senderPrivateKeyBytes } = getPrivateAndPublicKeyFromPassphrase(passphrase); - const convertedPrivateKey = Buffer.from(ed2curve.convertSecretKey(senderPrivateKeyBytes)); + const convertedPrivateKey = Buffer.from(ed2curve.convertSecretKey(senderPrivateKey)); const messageInBytes = Buffer.from(message, 'utf8'); const nonceSize = 24; const nonce = getRandomBytes(nonceSize); @@ -58,8 +53,8 @@ export const encryptMessageWithPassphrase = ( const cipherBytes = box(messageInBytes, nonce, convertedPublicKey, convertedPrivateKey); - const nonceHex = bufferToHex(nonce); - const encryptedMessage = bufferToHex(cipherBytes); + const nonceHex = nonce.toString('hex'); + const encryptedMessage = cipherBytes.toString('hex'); return { nonce: nonceHex, @@ -67,14 +62,13 @@ export const encryptMessageWithPassphrase = ( }; }; -export const decryptMessageWithPassphrase = ( +export const decryptMessageWithPrivateKey = ( cipherHex: string, nonce: string, - passphrase: string, + recipientPrivateKey: Buffer, senderPublicKey: Buffer, ): string => { - const { privateKey: recipientPrivateKeyBytes } = getPrivateAndPublicKeyFromPassphrase(passphrase); - const convertedPrivateKey = Buffer.from(ed2curve.convertSecretKey(recipientPrivateKeyBytes)); + const convertedPrivateKey = Buffer.from(ed2curve.convertSecretKey(recipientPrivateKey)); const cipherBytes = hexToBuffer(cipherHex); const nonceBytes = hexToBuffer(nonce); diff --git a/elements/lisk-cryptography/src/index.ts b/elements/lisk-cryptography/src/index.ts index 42f27878be2..82d58fe352b 100644 --- a/elements/lisk-cryptography/src/index.ts +++ b/elements/lisk-cryptography/src/index.ts @@ -17,6 +17,7 @@ export * as constants from './constants'; export * as encrypt from './encrypt'; export * as address from './address'; export * as legacyAddress from './legacy_address'; +export * as legacy from './legacy'; export * as bls from './bls'; export * as ed from './ed'; export * as utils from './utils'; diff --git a/elements/lisk-cryptography/src/legacy.ts b/elements/lisk-cryptography/src/legacy.ts new file mode 100644 index 00000000000..5ae3e2a828f --- /dev/null +++ b/elements/lisk-cryptography/src/legacy.ts @@ -0,0 +1,24 @@ +/* + * Copyright © 2022 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + * + */ + +import { getKeyPair } from './nacl'; +import { hash } from './utils'; + +export const getPrivateAndPublicKeyFromPassphrase = (passphrase: string) => { + const hashed = hash(passphrase, 'utf8'); + return getKeyPair(hashed); +}; + +export const getKeys = getPrivateAndPublicKeyFromPassphrase; diff --git a/elements/lisk-cryptography/src/legacy_address.ts b/elements/lisk-cryptography/src/legacy_address.ts index 802391b17a2..01b4d8cb5e7 100644 --- a/elements/lisk-cryptography/src/legacy_address.ts +++ b/elements/lisk-cryptography/src/legacy_address.ts @@ -13,7 +13,7 @@ * */ -import { getKeys } from './ed'; +import { getKeys } from './legacy'; import { getPublicKey } from './nacl'; import { hash } from './utils'; diff --git a/elements/lisk-cryptography/src/utils.ts b/elements/lisk-cryptography/src/utils.ts index 2e79a618930..cbd3c03bf47 100644 --- a/elements/lisk-cryptography/src/utils.ts +++ b/elements/lisk-cryptography/src/utils.ts @@ -160,8 +160,6 @@ export const intToBuffer = ( return buffer; }; -export const bufferToHex = (buffer: Buffer): string => Buffer.from(buffer).toString('hex'); - const hexRegex = /^[0-9a-f]+/i; export const hexToBuffer = (hex: string, argumentName = 'Argument'): Buffer => { if (typeof hex !== 'string') { @@ -179,8 +177,6 @@ export const hexToBuffer = (hex: string, argumentName = 'Argument'): Buffer => { return Buffer.from(matchedHex, 'hex'); }; -export const stringToBuffer = (str: string): Buffer => Buffer.from(str, 'utf8'); - const HASH_SIZE = 16; const INPUT_SIZE = 64; const defaultCount = 1000000; diff --git a/elements/lisk-cryptography/test/address.spec.ts b/elements/lisk-cryptography/test/address.spec.ts index b4e36dd6e4b..29fe51594af 100644 --- a/elements/lisk-cryptography/test/address.spec.ts +++ b/elements/lisk-cryptography/test/address.spec.ts @@ -15,18 +15,14 @@ import { getAddressFromPublicKey, getLisk32AddressFromPublicKey, - getAddressAndPublicKeyFromPassphrase, - getAddressFromPassphrase, getAddressFromPrivateKey, validateLisk32Address, getAddressFromLisk32Address, getLisk32AddressFromAddress, - getLisk32AddressFromPassphrase, } from '../src/address'; import * as utils from '../src/utils'; describe('address', () => { - const defaultPassphrase = 'secret'; const defaultPassphraseHash = '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b'; const defaultPrivateKey = Buffer.from( '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09', @@ -38,31 +34,10 @@ describe('address', () => { ); const defaultAddress = Buffer.from('2bb80d537b1da3e38bd30361aa855686bde0eacd', 'hex'); - const defaultAddressAndPublicKey = { - publicKey: defaultPublicKey, - address: defaultAddress, - }; - beforeEach(() => { - jest.spyOn(utils, 'bufferToHex'); - jest.spyOn(utils, 'hash').mockReturnValue(Buffer.from(defaultPassphraseHash, 'hex')); }); - describe('#getAddressAndPublicKeyFromPassphrase', () => { - it('should create correct address and publicKey', () => { - expect(getAddressAndPublicKeyFromPassphrase(defaultPassphrase)).toEqual( - defaultAddressAndPublicKey, - ); - }); - }); - - describe('#getAddressFromPassphrase', () => { - it('should create correct address', () => { - expect(getAddressFromPassphrase(defaultPassphrase)).toEqual(defaultAddress); - }); - }); - describe('#getAddressFromPrivateKey', () => { it('should create correct address', () => { expect(getAddressFromPrivateKey(defaultPrivateKey.slice(0, 64))).toEqual(defaultAddress); @@ -94,16 +69,6 @@ describe('address', () => { }); }); - describe('#getLisk32AddressFromPassphrase', () => { - it('should generate valid lisk32 address from passphrase', () => { - const passphrase = - 'garden mass universe joke disorder fish reveal state course lottery near virus'; - const address = getLisk32AddressFromPassphrase(passphrase); - - expect(validateLisk32Address(address)).toBeTrue(); - }); - }); - describe('#validateLisk32Address', () => { describe('Given valid addresses', () => { const addresses = [ diff --git a/elements/lisk-cryptography/test/bls.spec.ts b/elements/lisk-cryptography/test/bls.spec.ts index 9034bc04220..0abe4a5277f 100644 --- a/elements/lisk-cryptography/test/bls.spec.ts +++ b/elements/lisk-cryptography/test/bls.spec.ts @@ -16,16 +16,16 @@ import { createAggSig, - signBLS, + signData, verifyAggSig, verifyWeightedAggSig, - verifyBLS, + verifyData, getPrivateKeyFromPhraseAndPath, } from '../src/bls'; import { getAllFiles, hexToBuffer, loadSpecFile } from './helpers'; describe('bls_lib', () => { - describe('signBLS', () => { + describe('signData', () => { describe.each(getAllFiles(['bls_specs/sign_bls']))('%s', ({ path }) => { it('should generate valid signature', () => { const { @@ -36,14 +36,14 @@ describe('bls_lib', () => { output: string; }>(path); - const signature = signBLS(tag, hexToBuffer(netId), hexToBuffer(message), hexToBuffer(sk)); + const signature = signData(tag, hexToBuffer(netId), hexToBuffer(message), hexToBuffer(sk)); expect(signature.toString('hex')).toEqual(hexToBuffer(output).toString('hex')); }); }); }); - describe('verifyBLS', () => { + describe('verifyData', () => { describe.each(getAllFiles(['bls_specs/verify_bls']))('%s', ({ path }) => { it('should verify signatures', () => { const { @@ -54,7 +54,7 @@ describe('bls_lib', () => { output: string; }>(path); - const result = verifyBLS( + const result = verifyData( tag, hexToBuffer(netId), hexToBuffer(message), diff --git a/elements/lisk-cryptography/test/ed.spec.ts b/elements/lisk-cryptography/test/ed.spec.ts index 9acd601d8fd..f8929b99503 100644 --- a/elements/lisk-cryptography/test/ed.spec.ts +++ b/elements/lisk-cryptography/test/ed.spec.ts @@ -14,24 +14,19 @@ */ import { makeInvalid } from './helpers'; import { - SignedMessageWithOnePassphrase, - signMessageWithPassphrase, + SignedMessageWithPrivateKey, + signMessageWithPrivateKey, verifyMessageWithPublicKey, printSignedMessage, signAndPrintMessage, signData, - signDataWithPassphrase, signDataWithPrivateKey, verifyData, getKeyPairFromPhraseAndPath, getPublicKeyFromPrivateKey, - getPrivateAndPublicKeyFromPassphrase, - getKeys, } from '../src/ed'; import { createMessageTag } from '../src/utils'; import { MAX_UINT32 } from '../src/constants'; -import * as address from '../src/address'; -import { Keypair } from '../src/types'; const changeLength = (buffer: Buffer): Buffer => Buffer.concat([Buffer.from('00', 'hex'), buffer]); @@ -41,7 +36,6 @@ describe('sign', () => { '30c7a5df2ed79994178c10ac168d6d977ef45cd525e95b7a86244bbd4eb455', 'hex', ); - const defaultPassphrase = 'minute omit local rare sword knee banner pair rib museum shadow juice'; const defaultPrivateKey = '314852d7afb0d4c283692fef8a2cb40e30c7a5df2ed79994178c10ac168d6d977ef45cd525e95b7a86244bbd4eb4550914ad06301013958f4dd64d32ef7bc588'; const defaultPublicKey = '7ef45cd525e95b7a86244bbd4eb4550914ad06301013958f4dd64d32ef7bc588'; @@ -63,7 +57,7 @@ ${defaultSignature} const defaultDataSignature = 'be3167eb1bd0b1e37727872a7eaee78a7ec13386d23dc50e7ef589ff0e50d680bc8e039072790b875820b25ea7129a8b6c98850951515fac5cfa56119ce43e00'; - let defaultSignedMessage: SignedMessageWithOnePassphrase; + let defaultSignedMessage: SignedMessageWithPrivateKey; beforeEach(() => { defaultSignedMessage = { @@ -71,55 +65,18 @@ ${defaultSignature} publicKey: Buffer.from(defaultPublicKey, 'hex'), signature: Buffer.from(defaultSignature, 'hex'), }; - - jest.spyOn(address, 'getAddressAndPublicKeyFromPassphrase').mockImplementation(() => { - return { - address: address.getAddressFromPublicKey(Buffer.from(defaultPublicKey, 'hex')), - privateKey: Buffer.from(defaultPrivateKey, 'hex'), - publicKey: Buffer.from(defaultPublicKey, 'hex'), - }; - }); }); - describe('#signMessageWithPassphrase', () => { - it('should create a signed message using a secret passphrase', () => { - const signedMessage = signMessageWithPassphrase(defaultMessage, defaultPassphrase); + describe('#signMessageWithPrivateKey', () => { + it('should create a signed message using a secret private key', () => { + const signedMessage = signMessageWithPrivateKey( + defaultMessage, + Buffer.from(defaultPrivateKey, 'hex'), + ); expect(signedMessage).toEqual(defaultSignedMessage); }); }); - describe('#getPrivateAndPublicKeyFromPassphrase', () => { - let keyPair: Keypair; - - beforeEach(() => { - keyPair = getPrivateAndPublicKeyFromPassphrase(defaultPassphrase); - }); - - it('should generate the correct publicKey from a passphrase', () => { - expect(keyPair).toHaveProperty('publicKey', Buffer.from(defaultPublicKey, 'hex')); - }); - - it('should generate the correct privateKey from a passphrase', () => { - expect(keyPair).toHaveProperty('privateKey', Buffer.from(defaultPrivateKey, 'hex')); - }); - }); - - describe('#getKeys', () => { - let keyPair: Keypair; - - beforeEach(() => { - keyPair = getKeys(defaultPassphrase); - }); - - it('should generate the correct publicKey from a passphrase', () => { - expect(keyPair).toHaveProperty('publicKey', Buffer.from(defaultPublicKey, 'hex')); - }); - - it('should generate the correct privateKey from a passphrase', () => { - expect(keyPair).toHaveProperty('privateKey', Buffer.from(defaultPrivateKey, 'hex')); - }); - }); - describe('#verifyMessageWithPublicKey', () => { it('should detect invalid publicKeys', () => { expect( @@ -169,7 +126,10 @@ ${defaultSignature} describe('#signAndPrintMessage', () => { it('should sign the message once and wrap it into a printed Lisk template', () => { - const signedAndPrintedMessage = signAndPrintMessage(defaultMessage, defaultPassphrase); + const signedAndPrintedMessage = signAndPrintMessage( + defaultMessage, + Buffer.from(defaultPrivateKey, 'hex'), + ); expect(signedAndPrintedMessage).toBe(defaultPrintedMessage); }); }); @@ -178,20 +138,12 @@ ${defaultSignature} let signature: Buffer; beforeEach(async () => { - signature = signData(tag, networkIdentifier, defaultData, defaultPassphrase); - return Promise.resolve(); - }); - - it('should sign a transaction', () => { - expect(signature).toEqual(Buffer.from(defaultDataSignature, 'hex')); - }); - }); - - describe('#signDataWithPassphrase', () => { - let signature: Buffer; - - beforeEach(async () => { - signature = signDataWithPassphrase(tag, networkIdentifier, defaultData, defaultPassphrase); + signature = signData( + tag, + networkIdentifier, + defaultData, + Buffer.from(defaultPrivateKey, 'hex'), + ); return Promise.resolve(); }); diff --git a/elements/lisk-cryptography/test/encrypt.spec.ts b/elements/lisk-cryptography/test/encrypt.spec.ts index 8ddb63cf580..8ecb160c3a4 100644 --- a/elements/lisk-cryptography/test/encrypt.spec.ts +++ b/elements/lisk-cryptography/test/encrypt.spec.ts @@ -15,8 +15,8 @@ import * as ed2curve from 'ed2curve'; import { EncryptedMessageObject, EncryptedMessageWithNonce, - encryptMessageWithPassphrase, - decryptMessageWithPassphrase, + encryptMessageWithPrivateKey, + decryptMessageWithPrivateKey, encryptMessageWithPassword, decryptMessageWithPassword, KDF, @@ -25,7 +25,6 @@ import { stringifyEncryptedMessage, } from '../src/encrypt'; import * as utils from '../src/utils'; -import * as address from '../src/address'; describe('encrypt', () => { const regHexadecimal = /[0-9A-Za-z]/g; @@ -64,14 +63,6 @@ describe('encrypt', () => { Buffer.from('f245e78c83196d73452e55581ef924a1b792d352c142257aa3af13cded2e7905', 'hex'), ); - jest.spyOn(address, 'getAddressAndPublicKeyFromPassphrase').mockImplementation(() => { - return { - address: address.getAddressFromPublicKey(defaultPublicKey), - privateKey: defaultPrivateKey, - publicKey: defaultPublicKey, - }; - }); - hashStub = jest .spyOn(utils, 'hash') .mockReturnValue( @@ -80,13 +71,13 @@ describe('encrypt', () => { return Promise.resolve(); }); - describe('#encryptMessageWithPassphrase', () => { + describe('#encryptMessageWithPrivateKey', () => { let encryptedMessage: EncryptedMessageWithNonce; beforeEach(async () => { - encryptedMessage = encryptMessageWithPassphrase( + encryptedMessage = encryptMessageWithPrivateKey( defaultMessage, - defaultPassphrase, + defaultPrivateKey, defaultPublicKey, ); return Promise.resolve(); @@ -104,12 +95,12 @@ describe('encrypt', () => { }); }); - describe('#decryptMessageWithPassphrase', () => { + describe('#decryptMessageWithPrivateKey', () => { it('should be able to decrypt the message correctly using the receiver’s secret passphrase', () => { - const decryptedMessage = decryptMessageWithPassphrase( + const decryptedMessage = decryptMessageWithPrivateKey( defaultEncryptedMessageWithNonce.encryptedMessage, defaultEncryptedMessageWithNonce.nonce, - defaultPassphrase, + defaultPrivateKey, defaultPublicKey, ); @@ -118,11 +109,11 @@ describe('encrypt', () => { it('should inform the user if the nonce is the wrong length', () => { expect( - decryptMessageWithPassphrase.bind( + decryptMessageWithPrivateKey.bind( null, defaultEncryptedMessageWithNonce.encryptedMessage, defaultEncryptedMessageWithNonce.encryptedMessage.slice(0, 2), - defaultPassphrase, + defaultPrivateKey, defaultPublicKey, ), ).toThrow('Expected nonce to be 24 bytes.'); @@ -130,11 +121,11 @@ describe('encrypt', () => { it('should inform the user if something goes wrong during decryption', () => { expect( - decryptMessageWithPassphrase.bind( + decryptMessageWithPrivateKey.bind( null, defaultEncryptedMessageWithNonce.encryptedMessage.slice(0, 2), defaultEncryptedMessageWithNonce.nonce, - defaultPassphrase, + defaultPrivateKey, defaultPublicKey, ), ).toThrow('Something went wrong during decryption. Is this the full encrypted message?'); diff --git a/elements/lisk-cryptography/test/legacy.spec.ts b/elements/lisk-cryptography/test/legacy.spec.ts new file mode 100644 index 00000000000..52a4b4ec640 --- /dev/null +++ b/elements/lisk-cryptography/test/legacy.spec.ts @@ -0,0 +1,56 @@ +/* + * Copyright © 2020 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + * + */ +import { getKeys, getPrivateAndPublicKeyFromPassphrase } from '../src/legacy'; +import { Keypair } from '../src/types'; + +describe('Legacy', () => { + const defaultPassphrase = + 'purity retire hamster gold unfold stadium hunt truth movie walnut gun bean'; + const defaultPrivateKey = + 'bf338d9da23ccbc140e440f7da96718eb3f8b84ba4fe57e0a7a91f8c602e8015b56b53b205287d52ae63c688bf62e86ad81e8e1e5dd9aaef2e68711152b7a58a'; + const defaultPublicKey = 'b56b53b205287d52ae63c688bf62e86ad81e8e1e5dd9aaef2e68711152b7a58a'; + + describe('#getPrivateAndPublicKeyFromPassphrase', () => { + let keyPair: Keypair; + + beforeEach(() => { + keyPair = getPrivateAndPublicKeyFromPassphrase(defaultPassphrase); + }); + + it('should generate the correct publicKey from a passphrase', () => { + expect(keyPair).toHaveProperty('publicKey', Buffer.from(defaultPublicKey, 'hex')); + }); + + it('should generate the correct privateKey from a passphrase', () => { + expect(keyPair).toHaveProperty('privateKey', Buffer.from(defaultPrivateKey, 'hex')); + }); + }); + + describe('#getKeys', () => { + let keyPair: Keypair; + + beforeEach(() => { + keyPair = getKeys(defaultPassphrase); + }); + + it('should generate the correct publicKey from a passphrase', () => { + expect(keyPair).toHaveProperty('publicKey', Buffer.from(defaultPublicKey, 'hex')); + }); + + it('should generate the correct privateKey from a passphrase', () => { + expect(keyPair).toHaveProperty('privateKey', Buffer.from(defaultPrivateKey, 'hex')); + }); + }); +}); diff --git a/elements/lisk-cryptography/test/utils.spec.ts b/elements/lisk-cryptography/test/utils.spec.ts index 83e995162c9..d47a4978a41 100644 --- a/elements/lisk-cryptography/test/utils.spec.ts +++ b/elements/lisk-cryptography/test/utils.spec.ts @@ -14,7 +14,6 @@ */ import { - bufferToHex, generateHashOnionSeed, hashOnion, hexToBuffer, @@ -30,13 +29,6 @@ describe('utils', () => { const defaultBuffer = Buffer.from('\xe5\xe4\xf6'); const defaultHex = 'c3a5c3a4c3b6'; - describe('#bufferToHex', () => { - it('should create a hex string from a Buffer', () => { - const hex = bufferToHex(defaultBuffer); - expect(hex).toBe(defaultHex); - }); - }); - describe('#hexToBuffer', () => { it('should create a Buffer from a hex string', () => { const buffer = hexToBuffer(defaultHex); @@ -354,50 +346,4 @@ describe('utils', () => { }); }); }); - - // describe('readBit', () => { - // it('should read bits of one byte buffer', () => { - // const binary = '10000101'; - // const buffer = Buffer.alloc(1); - // buffer.writeUIntLE(parseInt(binary, 2), 0, 1); - - // for (const [index, bit] of binary.split('').entries()) { - // expect(readBit(buffer, 7 - index)).toEqual(bit === '1'); - // } - // }); - - // it('should read bits of two byte buffer', () => { - // const binary = '1010001010000101'; - // const buffer = Buffer.alloc(2); - // buffer.writeUIntLE(parseInt(binary, 2), 0, 2); - - // for (const [index, bit] of binary.split('').entries()) { - // expect(readBit(buffer, 15 - index)).toEqual(bit === '1'); - // } - // }); - // }); - - // describe('writeBit', () => { - // it('should write bit of one byte buffer', () => { - // const binary = '10000101'; - // const buffer = Buffer.alloc(1); - // for (const [index, bit] of binary.split('').entries()) { - // writeBit(buffer, 7 - index, bit === '1'); - // } - // const result = buffer.readUIntLE(0, 1).toString(2).padStart(8, '0'); - - // expect(result).toEqual(binary); - // }); - - // it('should write bits of two byte buffer', () => { - // const binary = '1010001010000101'; - // const buffer = Buffer.alloc(2); - // for (const [index, bit] of binary.split('').entries()) { - // writeBit(buffer, 15 - index, bit === '1'); - // } - // const result = buffer.readUIntLE(0, 2).toString(2).padStart(16, '0'); - - // expect(result).toEqual(binary); - // }); - // }); }); diff --git a/elements/lisk-transaction-pool/test/utils/cryptography.ts b/elements/lisk-transaction-pool/test/utils/cryptography.ts index 0531093235b..25314fdfd1e 100644 --- a/elements/lisk-transaction-pool/test/utils/cryptography.ts +++ b/elements/lisk-transaction-pool/test/utils/cryptography.ts @@ -12,11 +12,11 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { utils, address } from '@liskhq/lisk-cryptography'; +import { utils, legacy } from '@liskhq/lisk-cryptography'; export const generateRandomPublicKeys = (amount = 1): Array => new Array(amount).fill(0).map(_ => { - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(Math.random().toString(16)); + const { publicKey } = legacy.getPrivateAndPublicKeyFromPassphrase(Math.random().toString(16)); return publicKey; }); diff --git a/elements/lisk-transactions/src/sign.ts b/elements/lisk-transactions/src/sign.ts index 7d9bfa8e47e..aeef4cafaa6 100644 --- a/elements/lisk-transactions/src/sign.ts +++ b/elements/lisk-transactions/src/sign.ts @@ -14,7 +14,7 @@ */ import { codec, Schema } from '@liskhq/lisk-codec'; -import { utils, ed, address } from '@liskhq/lisk-cryptography'; +import { utils, ed } from '@liskhq/lisk-cryptography'; import { validateTransaction } from './validate'; import { baseTransactionSchema } from './schema'; import { TAG_TRANSACTION } from './constants'; @@ -73,44 +73,6 @@ export const getBytes = ( return transactionBytes; }; -// Validates transaction against schema and returns transaction including signature -export const signTransaction = ( - transactionObject: Record, - networkIdentifier: Buffer, - passphrase: string, - paramsSchema?: object, -): Record => { - if (!networkIdentifier.length) { - throw new Error('Network identifier is required to sign a transaction'); - } - - if (!passphrase) { - throw new Error('Passphrase is required to sign a transaction'); - } - const validationErrors = validateTransaction(transactionObject, paramsSchema); - if (validationErrors) { - throw validationErrors; - } - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(passphrase); - - if ( - !Buffer.isBuffer(transactionObject.senderPublicKey) || - !transactionObject.senderPublicKey.equals(publicKey) - ) { - throw new Error('Transaction senderPublicKey does not match public key from passphrase'); - } - - const signature = ed.signData( - TAG_TRANSACTION, - networkIdentifier, - getSigningBytes(transactionObject, paramsSchema), - passphrase, - ); - // eslint-disable-next-line no-param-reassign - transactionObject.signatures = [signature]; - return { ...transactionObject, id: utils.hash(getBytes(transactionObject, paramsSchema)) }; -}; - const sanitizeSignaturesArray = ( transactionObject: Record, keys: MultiSignatureKeys, @@ -130,78 +92,6 @@ const sanitizeSignaturesArray = ( } }; -// Validates transaction against schema and sign a multi-signature transaction -export const signMultiSignatureTransaction = ( - transactionObject: Record, - networkIdentifier: Buffer, - passphrase: string, - keys: MultiSignatureKeys, - paramsSchema?: object, - includeSenderSignature = false, -): Record => { - if (!networkIdentifier.length) { - throw new Error('Network identifier is required to sign a transaction'); - } - - if (!passphrase) { - throw new Error('Passphrase is required to sign a transaction'); - } - - if (!Array.isArray(transactionObject.signatures)) { - throw new Error('Signatures must be of type array'); - } - - const validationErrors = validateTransaction(transactionObject, paramsSchema); - if (validationErrors) { - throw validationErrors; - } - // Sort keys - keys.mandatoryKeys.sort((publicKeyA, publicKeyB) => publicKeyA.compare(publicKeyB)); - keys.optionalKeys.sort((publicKeyA, publicKeyB) => publicKeyA.compare(publicKeyB)); - - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(passphrase); - const signature = ed.signData( - TAG_TRANSACTION, - networkIdentifier, - getSigningBytes(transactionObject, paramsSchema), - passphrase, - ); - - if ( - includeSenderSignature && - Buffer.isBuffer(transactionObject.senderPublicKey) && - publicKey.equals(transactionObject.senderPublicKey) - ) { - // eslint-disable-next-line no-param-reassign - transactionObject.signatures[0] = signature; - } - - // Locate where this public key should go in the signatures array - const mandatoryKeyIndex = keys.mandatoryKeys.findIndex(aPublicKey => - aPublicKey.equals(publicKey), - ); - const optionalKeyIndex = keys.optionalKeys.findIndex(aPublicKey => aPublicKey.equals(publicKey)); - - // If it's a mandatory Public Key find where to add the signature - if (mandatoryKeyIndex !== -1) { - const signatureOffset = includeSenderSignature ? 1 : 0; - // eslint-disable-next-line no-param-reassign - transactionObject.signatures[mandatoryKeyIndex + signatureOffset] = signature; - } - - if (optionalKeyIndex !== -1) { - const signatureOffset = includeSenderSignature ? 1 : 0; - // eslint-disable-next-line no-param-reassign - transactionObject.signatures[ - keys.mandatoryKeys.length + optionalKeyIndex + signatureOffset - ] = signature; - } - - sanitizeSignaturesArray(transactionObject, keys, includeSenderSignature); - - return { ...transactionObject, id: utils.hash(getBytes(transactionObject, paramsSchema)) }; -}; - export const signTransactionWithPrivateKey = ( transactionObject: Record, networkIdentifier: Buffer, @@ -221,15 +111,10 @@ export const signTransactionWithPrivateKey = ( throw validationErrors; } - const transactionWithNetworkIdentifierBytes = Buffer.concat([ - networkIdentifier, - getSigningBytes(transactionObject, paramsSchema), - ]); - const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, - transactionWithNetworkIdentifierBytes, + getSigningBytes(transactionObject, paramsSchema), privateKey, ); @@ -238,6 +123,8 @@ export const signTransactionWithPrivateKey = ( return { ...transactionObject, id: utils.hash(getBytes(transactionObject, paramsSchema)) }; }; +export const signTransaction = signTransactionWithPrivateKey; + export const signMultiSignatureTransactionWithPrivateKey = ( transactionObject: Record, networkIdentifier: Buffer, @@ -250,7 +137,7 @@ export const signMultiSignatureTransactionWithPrivateKey = ( throw new Error('Network identifier is required to sign a transaction'); } - if (!privateKey.length || privateKey.length !== 64) { + if (!privateKey || privateKey.length !== 64) { throw new Error('Private key must be 64 bytes'); } @@ -262,29 +149,22 @@ export const signMultiSignatureTransactionWithPrivateKey = ( if (validationErrors) { throw validationErrors; } - // Sort keys keys.mandatoryKeys.sort((publicKeyA, publicKeyB) => publicKeyA.compare(publicKeyB)); keys.optionalKeys.sort((publicKeyA, publicKeyB) => publicKeyA.compare(publicKeyB)); - const transactionWithNetworkIdentifierBytes = Buffer.concat([ - networkIdentifier, - getSigningBytes(transactionObject, paramsSchema), - ]); - + const publicKey = ed.getPublicKeyFromPrivateKey(privateKey); const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, - transactionWithNetworkIdentifierBytes, + getSigningBytes(transactionObject, paramsSchema), privateKey, ); - const signerPublicKey = ed.getPublicKeyFromPrivateKey(privateKey); - if ( includeSenderSignature && Buffer.isBuffer(transactionObject.senderPublicKey) && - signerPublicKey.equals(transactionObject.senderPublicKey) + publicKey.equals(transactionObject.senderPublicKey) ) { // eslint-disable-next-line no-param-reassign transactionObject.signatures[0] = signature; @@ -292,11 +172,9 @@ export const signMultiSignatureTransactionWithPrivateKey = ( // Locate where this public key should go in the signatures array const mandatoryKeyIndex = keys.mandatoryKeys.findIndex(aPublicKey => - aPublicKey.equals(signerPublicKey), - ); - const optionalKeyIndex = keys.optionalKeys.findIndex(aPublicKey => - aPublicKey.equals(signerPublicKey), + aPublicKey.equals(publicKey), ); + const optionalKeyIndex = keys.optionalKeys.findIndex(aPublicKey => aPublicKey.equals(publicKey)); // If it's a mandatory Public Key find where to add the signature if (mandatoryKeyIndex !== -1) { @@ -317,3 +195,5 @@ export const signMultiSignatureTransactionWithPrivateKey = ( return { ...transactionObject, id: utils.hash(getBytes(transactionObject, paramsSchema)) }; }; + +export const signMultiSignatureTransaction = signMultiSignatureTransactionWithPrivateKey; diff --git a/elements/lisk-transactions/test/__snapshots__/sign.spec.ts.snap b/elements/lisk-transactions/test/__snapshots__/sign.spec.ts.snap index 69d386c284f..de4895fc26e 100644 --- a/elements/lisk-transactions/test/__snapshots__/sign.spec.ts.snap +++ b/elements/lisk-transactions/test/__snapshots__/sign.spec.ts.snap @@ -96,7 +96,7 @@ Object { } `; -exports[`sign signTransaction should return signed transaction for given params schema 1`] = ` +exports[`sign signTransactionWithPrivateKey should return signed transaction for given params schema 1`] = ` Object { "commandID": Object { "data": Array [ @@ -110,38 +110,38 @@ Object { "fee": 10000000n, "id": Object { "data": Array [ - 134, - 7, - 234, - 87, - 148, - 70, - 82, - 13, - 80, - 124, - 30, + 1, + 65, + 204, + 5, + 97, + 125, + 178, + 202, + 76, + 171, + 88, + 150, + 199, + 139, + 15, + 251, + 242, + 90, + 241, + 9, + 132, + 164, + 255, 217, - 82, + 32, + 158, + 184, + 22, + 252, 25, - 105, - 126, - 48, - 195, - 92, - 119, - 147, - 247, - 140, - 57, - 229, - 153, - 51, - 209, - 110, - 185, - 7, - 214, + 87, + 200, ], "type": "Buffer", }, @@ -224,70 +224,70 @@ Object { "signatures": Array [ Object { "data": Array [ - 187, - 248, - 243, - 18, - 92, + 53, + 144, + 231, + 44, 205, - 206, - 123, - 212, - 0, - 135, - 48, - 12, - 155, - 1, - 199, - 159, - 204, - 169, - 156, - 98, - 167, - 158, - 24, - 226, - 173, 231, - 254, 15, + 117, + 221, + 178, + 93, + 145, + 23, + 166, + 28, + 231, + 147, + 147, + 62, + 146, + 210, + 149, + 207, + 208, + 148, + 210, + 198, + 240, + 132, + 74, + 118, + 107, + 183, + 55, 196, - 47, - 177, - 206, + 6, + 13, + 142, + 150, + 11, + 247, + 146, + 18, + 184, 86, - 194, - 58, + 17, + 51, + 79, + 63, + 138, + 206, + 193, + 14, + 9, + 188, 178, - 187, - 38, - 64, + 0, + 35, 20, - 95, - 135, - 42, - 140, - 194, - 175, - 47, - 80, - 213, - 250, - 117, - 94, - 63, - 46, - 117, - 207, - 80, - 166, - 165, - 4, - 22, - 79, - 3, + 236, + 195, + 100, + 164, + 6, ], "type": "Buffer", }, @@ -295,7 +295,7 @@ Object { } `; -exports[`sign signTransactionWithPrivateKey should return signed transaction for given params schema 1`] = ` +exports[`sign when running scenario "multisignature_registration_transaction" Both mandatory and optional member group should have correct signatures 1`] = ` Object { "commandID": Object { "data": Array [ @@ -306,200 +306,2278 @@ Object { ], "type": "Buffer", }, - "fee": 10000000n, - "id": Object { - "data": Array [ - 209, - 41, - 161, - 125, - 164, - 152, - 209, - 235, - 91, - 149, - 171, - 245, - 229, - 118, - 162, - 53, - 123, - 213, - 99, - 152, - 100, - 17, - 187, - 227, - 220, - 146, - 237, - 42, - 101, - 102, - 133, - 69, - ], - "type": "Buffer", - }, + "fee": 1500000000n, "moduleID": Object { "data": Array [ 0, 0, 0, - 2, + 12, ], "type": "Buffer", }, "nonce": 1n, "params": Object { - "amount": 4008489300000000n, - "data": "", - "recipientAddress": Object { - "data": Array [ - 58, - 151, - 31, - 208, - 43, - 74, - 7, - 252, - 32, - 170, - 209, - 147, - 109, - 60, - 177, - 210, - 99, - 185, - 110, - 15, - ], - "type": "Buffer", - }, + "mandatoryKeys": Array [ + Object { + "data": Array [ + 74, + 103, + 100, + 106, + 68, + 99, + 19, + 219, + 150, + 76, + 57, + 55, + 3, + 89, + 132, + 92, + 82, + 252, + 233, + 34, + 90, + 57, + 41, + 119, + 14, + 244, + 20, + 72, + 194, + 88, + 253, + 57, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 241, + 185, + 244, + 238, + 113, + 181, + 213, + 133, + 125, + 59, + 52, + 109, + 68, + 28, + 169, + 103, + 242, + 120, + 112, + 235, + 238, + 136, + 86, + 157, + 179, + 100, + 253, + 19, + 226, + 138, + 219, + 163, + ], + "type": "Buffer", + }, + ], + "numberOfSignatures": 4, + "optionalKeys": Array [ + Object { + "data": Array [ + 87, + 223, + 92, + 56, + 17, + 150, + 25, + 57, + 248, + 220, + 250, + 133, + 140, + 110, + 174, + 254, + 191, + 170, + 77, + 233, + 66, + 247, + 231, + 3, + 191, + 136, + 18, + 126, + 14, + 233, + 204, + 164, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 250, + 64, + 107, + 105, + 82, + 211, + 119, + 240, + 39, + 137, + 32, + 227, + 235, + 141, + 169, + 25, + 228, + 207, + 92, + 104, + 176, + 46, + 235, + 165, + 216, + 179, + 51, + 79, + 220, + 3, + 105, + 182, + ], + "type": "Buffer", + }, + ], }, "senderPublicKey": Object { "data": Array [ - 241, - 185, - 244, - 238, - 113, - 181, - 213, - 133, - 125, - 59, - 52, - 109, - 68, - 28, - 169, - 103, - 242, - 120, + 11, + 33, + 31, + 206, + 75, + 97, + 80, + 131, 112, - 235, - 238, - 136, - 86, - 157, - 179, + 28, + 184, + 168, + 201, + 148, + 7, + 228, 100, - 253, - 19, - 226, - 138, - 219, - 163, + 178, + 249, + 170, + 79, + 54, + 112, + 149, + 50, + 45, + 225, + 183, + 126, + 95, + 207, + 190, ], "type": "Buffer", }, "signatures": Array [ Object { "data": Array [ - 242, - 205, - 165, - 48, - 14, - 192, - 204, - 160, - 155, - 132, - 151, - 212, - 102, - 117, - 71, - 40, - 128, - 196, - 216, - 211, + 110, + 118, 108, - 194, - 253, + 228, 193, - 52, - 109, - 21, - 125, - 165, - 75, - 24, - 88, - 254, - 36, - 255, - 108, - 196, - 239, + 205, + 174, + 218, + 55, + 63, + 85, + 20, + 41, + 123, + 230, + 189, + 123, + 187, + 137, + 211, + 4, 15, - 30, - 152, - 90, - 103, - 59, - 117, - 147, - 52, - 94, - 235, - 210, - 202, - 28, - 46, - 242, - 50, + 19, + 154, + 126, + 200, 234, + 16, + 77, + 21, + 116, + 19, + 17, + 12, + 85, + 157, + 181, + 58, + 202, + 128, + 96, + 201, + 219, + 180, + 246, + 6, + 18, + 77, + 186, + 209, + 175, + 19, + 188, + 143, + 212, 133, - 102, - 10, - 61, - 211, - 80, - 141, - 11, + 107, + 179, + 112, + 183, + 58, + 8, + 27, + 5, ], "type": "Buffer", }, - ], -} -`; - -exports[`sign when running scenario "multisignature_registration_transaction" Both mandatory and optional member group should have correct signatures 1`] = `[Function]`; - -exports[`sign when running scenario "multisignature_registration_transaction" Only mandatory members should have correct signatures 1`] = `[Function]`; + Object { + "data": Array [ + 59, + 2, + 142, + 184, + 134, + 49, + 103, + 85, + 47, + 148, + 10, + 174, + 20, + 112, + 110, + 78, + 115, + 168, + 149, + 231, + 116, + 79, + 154, + 68, + 133, + 125, + 37, + 153, + 110, + 132, + 223, + 199, + 41, + 222, + 95, + 185, + 16, + 14, + 150, + 227, + 168, + 94, + 138, + 208, + 16, + 232, + 30, + 179, + 139, + 54, + 54, + 155, + 95, + 18, + 158, + 19, + 223, + 32, + 31, + 108, + 185, + 21, + 87, + 1, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 166, + 110, + 207, + 239, + 215, + 196, + 59, + 42, + 201, + 124, + 70, + 46, + 2, + 249, + 146, + 235, + 168, + 24, + 136, + 67, + 129, + 67, + 49, + 205, + 79, + 93, + 86, + 43, + 202, + 49, + 168, + 52, + 52, + 147, + 20, + 66, + 85, + 76, + 106, + 234, + 85, + 253, + 209, + 20, + 198, + 159, + 193, + 232, + 113, + 52, + 66, + 204, + 186, + 141, + 28, + 212, + 92, + 3, + 176, + 58, + 158, + 252, + 8, + 8, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 114, + 43, + 229, + 59, + 178, + 52, + 139, + 169, + 68, + 58, + 188, + 48, + 243, + 188, + 241, + 175, + 56, + 12, + 56, + 59, + 93, + 82, + 25, + 242, + 153, + 21, + 149, + 50, + 221, + 90, + 242, + 177, + 186, + 153, + 102, + 71, + 145, + 169, + 127, + 72, + 76, + 189, + 163, + 126, + 147, + 106, + 79, + 102, + 212, + 30, + 220, + 230, + 154, + 5, + 68, + 36, + 2, + 99, + 121, + 20, + 45, + 144, + 161, + 12, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 16, + 17, + 143, + 240, + 16, + 102, + 43, + 10, + 7, + 19, + 163, + 68, + 116, + 239, + 168, + 46, + 217, + 81, + 161, + 144, + 66, + 32, + 181, + 161, + 194, + 50, + 119, + 50, + 253, + 23, + 149, + 247, + 125, + 163, + 140, + 11, + 18, + 244, + 36, + 147, + 84, + 78, + 117, + 54, + 138, + 126, + 125, + 133, + 55, + 0, + 190, + 187, + 5, + 54, + 23, + 100, + 133, + 103, + 195, + 208, + 60, + 116, + 104, + 11, + ], + "type": "Buffer", + }, + ], +} +`; + +exports[`sign when running scenario "multisignature_registration_transaction" Only mandatory members should have correct signatures 1`] = ` +Object { + "commandID": Object { + "data": Array [ + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + "fee": 1500000000n, + "moduleID": Object { + "data": Array [ + 0, + 0, + 0, + 12, + ], + "type": "Buffer", + }, + "nonce": 1n, + "params": Object { + "mandatoryKeys": Array [ + Object { + "data": Array [ + 74, + 103, + 100, + 106, + 68, + 99, + 19, + 219, + 150, + 76, + 57, + 55, + 3, + 89, + 132, + 92, + 82, + 252, + 233, + 34, + 90, + 57, + 41, + 119, + 14, + 244, + 20, + 72, + 194, + 88, + 253, + 57, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 241, + 185, + 244, + 238, + 113, + 181, + 213, + 133, + 125, + 59, + 52, + 109, + 68, + 28, + 169, + 103, + 242, + 120, + 112, + 235, + 238, + 136, + 86, + 157, + 179, + 100, + 253, + 19, + 226, + 138, + 219, + 163, + ], + "type": "Buffer", + }, + ], + "numberOfSignatures": 2, + "optionalKeys": Array [], + }, + "senderPublicKey": Object { + "data": Array [ + 11, + 33, + 31, + 206, + 75, + 97, + 80, + 131, + 112, + 28, + 184, + 168, + 201, + 148, + 7, + 228, + 100, + 178, + 249, + 170, + 79, + 54, + 112, + 149, + 50, + 45, + 225, + 183, + 126, + 95, + 207, + 190, + ], + "type": "Buffer", + }, + "signatures": Array [ + Object { + "data": Array [ + 239, + 6, + 193, + 98, + 240, + 239, + 84, + 10, + 216, + 233, + 25, + 24, + 100, + 134, + 70, + 251, + 87, + 30, + 205, + 197, + 5, + 144, + 244, + 23, + 223, + 115, + 182, + 50, + 221, + 189, + 255, + 152, + 43, + 39, + 73, + 43, + 139, + 13, + 43, + 199, + 59, + 159, + 31, + 245, + 39, + 170, + 178, + 82, + 212, + 70, + 111, + 153, + 31, + 171, + 118, + 24, + 196, + 225, + 129, + 153, + 253, + 169, + 28, + 14, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 228, + 116, + 65, + 127, + 113, + 127, + 223, + 65, + 196, + 193, + 178, + 248, + 2, + 121, + 231, + 106, + 30, + 60, + 81, + 248, + 49, + 61, + 134, + 224, + 224, + 191, + 223, + 226, + 216, + 155, + 46, + 200, + 22, + 155, + 90, + 59, + 117, + 146, + 249, + 107, + 184, + 174, + 35, + 107, + 24, + 11, + 253, + 125, + 157, + 126, + 95, + 225, + 17, + 47, + 176, + 16, + 61, + 43, + 244, + 9, + 38, + 166, + 162, + 1, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 164, + 36, + 66, + 136, + 110, + 198, + 253, + 211, + 102, + 230, + 175, + 217, + 238, + 248, + 83, + 214, + 81, + 6, + 140, + 212, + 163, + 130, + 163, + 68, + 237, + 9, + 217, + 155, + 216, + 196, + 211, + 165, + 228, + 51, + 255, + 251, + 16, + 240, + 249, + 73, + 31, + 185, + 136, + 232, + 142, + 250, + 123, + 158, + 114, + 38, + 175, + 9, + 73, + 174, + 99, + 228, + 172, + 235, + 31, + 69, + 201, + 20, + 30, + 11, + ], + "type": "Buffer", + }, + ], +} +`; -exports[`sign when running scenario "multisignature_registration_transaction" Only optional members should have correct signatures 1`] = `[Function]`; +exports[`sign when running scenario "multisignature_registration_transaction" Only optional members should have correct signatures 1`] = ` +Object { + "commandID": Object { + "data": Array [ + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + "fee": 1500000000n, + "moduleID": Object { + "data": Array [ + 0, + 0, + 0, + 12, + ], + "type": "Buffer", + }, + "nonce": 1n, + "params": Object { + "mandatoryKeys": Array [], + "numberOfSignatures": 1, + "optionalKeys": Array [ + Object { + "data": Array [ + 87, + 223, + 92, + 56, + 17, + 150, + 25, + 57, + 248, + 220, + 250, + 133, + 140, + 110, + 174, + 254, + 191, + 170, + 77, + 233, + 66, + 247, + 231, + 3, + 191, + 136, + 18, + 126, + 14, + 233, + 204, + 164, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 250, + 64, + 107, + 105, + 82, + 211, + 119, + 240, + 39, + 137, + 32, + 227, + 235, + 141, + 169, + 25, + 228, + 207, + 92, + 104, + 176, + 46, + 235, + 165, + 216, + 179, + 51, + 79, + 220, + 3, + 105, + 182, + ], + "type": "Buffer", + }, + ], + }, + "senderPublicKey": Object { + "data": Array [ + 11, + 33, + 31, + 206, + 75, + 97, + 80, + 131, + 112, + 28, + 184, + 168, + 201, + 148, + 7, + 228, + 100, + 178, + 249, + 170, + 79, + 54, + 112, + 149, + 50, + 45, + 225, + 183, + 126, + 95, + 207, + 190, + ], + "type": "Buffer", + }, + "signatures": Array [ + Object { + "data": Array [ + 2, + 231, + 183, + 32, + 216, + 252, + 152, + 192, + 187, + 216, + 125, + 97, + 9, + 148, + 159, + 174, + 15, + 176, + 199, + 224, + 215, + 207, + 106, + 143, + 17, + 53, + 83, + 246, + 236, + 26, + 234, + 122, + 47, + 96, + 199, + 255, + 114, + 96, + 29, + 197, + 113, + 220, + 161, + 115, + 9, + 4, + 109, + 180, + 66, + 85, + 184, + 191, + 94, + 173, + 130, + 164, + 98, + 77, + 84, + 197, + 108, + 131, + 141, + 9, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 121, + 240, + 14, + 57, + 56, + 54, + 204, + 103, + 100, + 7, + 95, + 144, + 33, + 59, + 33, + 150, + 132, + 219, + 83, + 0, + 106, + 9, + 89, + 27, + 207, + 109, + 219, + 216, + 116, + 142, + 140, + 231, + 135, + 183, + 192, + 222, + 218, + 201, + 119, + 50, + 161, + 9, + 221, + 187, + 95, + 25, + 199, + 160, + 152, + 44, + 136, + 197, + 89, + 255, + 246, + 232, + 83, + 215, + 65, + 84, + 80, + 14, + 218, + 7, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 225, + 240, + 237, + 127, + 48, + 38, + 197, + 7, + 95, + 217, + 9, + 201, + 111, + 5, + 166, + 243, + 94, + 138, + 35, + 236, + 226, + 98, + 236, + 140, + 193, + 248, + 53, + 130, + 177, + 127, + 207, + 114, + 54, + 9, + 90, + 121, + 38, + 72, + 167, + 26, + 129, + 10, + 253, + 175, + 201, + 215, + 137, + 145, + 158, + 156, + 51, + 33, + 75, + 233, + 26, + 175, + 54, + 140, + 227, + 2, + 223, + 220, + 68, + 5, + ], + "type": "Buffer", + }, + ], +} +`; -exports[`sign when running scenario "multisignature_registration_transaction" Second signature case should have correct signatures 1`] = `[Function]`; +exports[`sign when running scenario "multisignature_registration_transaction" Second signature case should have correct signatures 1`] = ` +Object { + "commandID": Object { + "data": Array [ + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + "fee": 1500000000n, + "moduleID": Object { + "data": Array [ + 0, + 0, + 0, + 12, + ], + "type": "Buffer", + }, + "nonce": 1n, + "params": Object { + "mandatoryKeys": Array [ + Object { + "data": Array [ + 11, + 33, + 31, + 206, + 75, + 97, + 80, + 131, + 112, + 28, + 184, + 168, + 201, + 148, + 7, + 228, + 100, + 178, + 249, + 170, + 79, + 54, + 112, + 149, + 50, + 45, + 225, + 183, + 126, + 95, + 207, + 190, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 255, + 48, + 239, + 64, + 183, + 222, + 66, + 17, + 65, + 55, + 190, + 70, + 241, + 0, + 157, + 48, + 229, + 193, + 152, + 9, + 167, + 61, + 90, + 22, + 43, + 201, + 159, + 126, + 118, + 129, + 214, + 61, + ], + "type": "Buffer", + }, + ], + "numberOfSignatures": 2, + "optionalKeys": Array [], + }, + "senderPublicKey": Object { + "data": Array [ + 11, + 33, + 31, + 206, + 75, + 97, + 80, + 131, + 112, + 28, + 184, + 168, + 201, + 148, + 7, + 228, + 100, + 178, + 249, + 170, + 79, + 54, + 112, + 149, + 50, + 45, + 225, + 183, + 126, + 95, + 207, + 190, + ], + "type": "Buffer", + }, + "signatures": Array [ + Object { + "data": Array [ + 74, + 203, + 121, + 134, + 238, + 186, + 59, + 188, + 37, + 4, + 101, + 97, + 149, + 78, + 84, + 112, + 14, + 44, + 185, + 150, + 24, + 211, + 127, + 169, + 151, + 203, + 184, + 44, + 13, + 242, + 15, + 218, + 34, + 130, + 148, + 178, + 171, + 18, + 176, + 152, + 27, + 120, + 15, + 99, + 197, + 13, + 25, + 50, + 168, + 235, + 176, + 148, + 75, + 39, + 53, + 11, + 84, + 138, + 207, + 185, + 124, + 25, + 188, + 3, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 74, + 203, + 121, + 134, + 238, + 186, + 59, + 188, + 37, + 4, + 101, + 97, + 149, + 78, + 84, + 112, + 14, + 44, + 185, + 150, + 24, + 211, + 127, + 169, + 151, + 203, + 184, + 44, + 13, + 242, + 15, + 218, + 34, + 130, + 148, + 178, + 171, + 18, + 176, + 152, + 27, + 120, + 15, + 99, + 197, + 13, + 25, + 50, + 168, + 235, + 176, + 148, + 75, + 39, + 53, + 11, + 84, + 138, + 207, + 185, + 124, + 25, + 188, + 3, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 18, + 205, + 113, + 63, + 182, + 166, + 17, + 115, + 207, + 76, + 144, + 84, + 143, + 49, + 125, + 104, + 249, + 184, + 63, + 162, + 15, + 122, + 209, + 195, + 101, + 184, + 22, + 5, + 144, + 149, + 146, + 163, + 16, + 204, + 61, + 27, + 235, + 74, + 178, + 153, + 194, + 61, + 77, + 145, + 19, + 134, + 50, + 68, + 180, + 124, + 208, + 152, + 4, + 40, + 34, + 37, + 3, + 164, + 126, + 121, + 22, + 25, + 250, + 15, + ], + "type": "Buffer", + }, + ], +} +`; -exports[`sign when running scenario "multisignature_registration_transaction" Sender is a member of the group should have correct signatures 1`] = `[Function]`; +exports[`sign when running scenario "multisignature_registration_transaction" Sender is a member of the group should have correct signatures 1`] = ` +Object { + "commandID": Object { + "data": Array [ + 0, + 0, + 0, + 0, + ], + "type": "Buffer", + }, + "fee": 1500000000n, + "moduleID": Object { + "data": Array [ + 0, + 0, + 0, + 12, + ], + "type": "Buffer", + }, + "nonce": 1n, + "params": Object { + "mandatoryKeys": Array [ + Object { + "data": Array [ + 11, + 33, + 31, + 206, + 75, + 97, + 80, + 131, + 112, + 28, + 184, + 168, + 201, + 148, + 7, + 228, + 100, + 178, + 249, + 170, + 79, + 54, + 112, + 149, + 50, + 45, + 225, + 183, + 126, + 95, + 207, + 190, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 74, + 103, + 100, + 106, + 68, + 99, + 19, + 219, + 150, + 76, + 57, + 55, + 3, + 89, + 132, + 92, + 82, + 252, + 233, + 34, + 90, + 57, + 41, + 119, + 14, + 244, + 20, + 72, + 194, + 88, + 253, + 57, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 241, + 185, + 244, + 238, + 113, + 181, + 213, + 133, + 125, + 59, + 52, + 109, + 68, + 28, + 169, + 103, + 242, + 120, + 112, + 235, + 238, + 136, + 86, + 157, + 179, + 100, + 253, + 19, + 226, + 138, + 219, + 163, + ], + "type": "Buffer", + }, + ], + "numberOfSignatures": 4, + "optionalKeys": Array [ + Object { + "data": Array [ + 87, + 223, + 92, + 56, + 17, + 150, + 25, + 57, + 248, + 220, + 250, + 133, + 140, + 110, + 174, + 254, + 191, + 170, + 77, + 233, + 66, + 247, + 231, + 3, + 191, + 136, + 18, + 126, + 14, + 233, + 204, + 164, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 250, + 64, + 107, + 105, + 82, + 211, + 119, + 240, + 39, + 137, + 32, + 227, + 235, + 141, + 169, + 25, + 228, + 207, + 92, + 104, + 176, + 46, + 235, + 165, + 216, + 179, + 51, + 79, + 220, + 3, + 105, + 182, + ], + "type": "Buffer", + }, + ], + }, + "senderPublicKey": Object { + "data": Array [ + 11, + 33, + 31, + 206, + 75, + 97, + 80, + 131, + 112, + 28, + 184, + 168, + 201, + 148, + 7, + 228, + 100, + 178, + 249, + 170, + 79, + 54, + 112, + 149, + 50, + 45, + 225, + 183, + 126, + 95, + 207, + 190, + ], + "type": "Buffer", + }, + "signatures": Array [ + Object { + "data": Array [ + 71, + 133, + 122, + 148, + 47, + 71, + 33, + 219, + 183, + 77, + 97, + 195, + 240, + 203, + 162, + 38, + 164, + 242, + 98, + 66, + 127, + 147, + 189, + 32, + 119, + 206, + 95, + 35, + 136, + 217, + 160, + 139, + 217, + 130, + 35, + 245, + 118, + 148, + 198, + 94, + 195, + 89, + 186, + 18, + 220, + 39, + 96, + 99, + 10, + 112, + 39, + 233, + 68, + 23, + 239, + 80, + 16, + 124, + 120, + 202, + 11, + 89, + 178, + 4, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 71, + 133, + 122, + 148, + 47, + 71, + 33, + 219, + 183, + 77, + 97, + 195, + 240, + 203, + 162, + 38, + 164, + 242, + 98, + 66, + 127, + 147, + 189, + 32, + 119, + 206, + 95, + 35, + 136, + 217, + 160, + 139, + 217, + 130, + 35, + 245, + 118, + 148, + 198, + 94, + 195, + 89, + 186, + 18, + 220, + 39, + 96, + 99, + 10, + 112, + 39, + 233, + 68, + 23, + 239, + 80, + 16, + 124, + 120, + 202, + 11, + 89, + 178, + 4, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 221, + 203, + 173, + 116, + 127, + 205, + 88, + 64, + 244, + 85, + 31, + 241, + 136, + 152, + 61, + 82, + 160, + 237, + 77, + 209, + 71, + 8, + 12, + 149, + 37, + 174, + 217, + 229, + 236, + 24, + 1, + 71, + 195, + 13, + 114, + 223, + 197, + 219, + 104, + 113, + 232, + 167, + 87, + 141, + 3, + 216, + 44, + 242, + 211, + 218, + 133, + 174, + 218, + 100, + 128, + 33, + 208, + 253, + 120, + 71, + 115, + 187, + 210, + 1, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 245, + 104, + 153, + 229, + 26, + 83, + 61, + 216, + 171, + 82, + 99, + 247, + 177, + 82, + 81, + 107, + 27, + 38, + 12, + 86, + 74, + 98, + 175, + 140, + 213, + 220, + 5, + 14, + 53, + 133, + 102, + 86, + 91, + 134, + 110, + 3, + 228, + 12, + 208, + 177, + 75, + 78, + 190, + 15, + 52, + 187, + 56, + 163, + 38, + 150, + 238, + 52, + 8, + 55, + 14, + 186, + 74, + 241, + 102, + 199, + 155, + 219, + 49, + 2, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 231, + 153, + 161, + 220, + 146, + 31, + 7, + 60, + 201, + 105, + 80, + 194, + 104, + 181, + 21, + 239, + 234, + 182, + 23, + 152, + 247, + 220, + 194, + 53, + 92, + 210, + 81, + 185, + 19, + 192, + 144, + 0, + 29, + 24, + 216, + 199, + 221, + 120, + 26, + 248, + 186, + 252, + 4, + 76, + 173, + 99, + 189, + 94, + 101, + 5, + 141, + 133, + 42, + 116, + 96, + 253, + 26, + 188, + 175, + 31, + 221, + 190, + 81, + 2, + ], + "type": "Buffer", + }, + Object { + "data": Array [ + 18, + 80, + 190, + 72, + 3, + 131, + 47, + 203, + 106, + 70, + 215, + 116, + 207, + 175, + 177, + 221, + 10, + 97, + 38, + 0, + 28, + 174, + 165, + 237, + 157, + 203, + 225, + 111, + 108, + 98, + 44, + 205, + 76, + 44, + 224, + 182, + 232, + 175, + 215, + 11, + 191, + 253, + 170, + 151, + 199, + 100, + 20, + 45, + 239, + 244, + 78, + 209, + 122, + 234, + 154, + 229, + 142, + 218, + 166, + 50, + 79, + 166, + 241, + 1, + ], + "type": "Buffer", + }, + ], +} +`; diff --git a/elements/lisk-transactions/test/fee.spec.ts b/elements/lisk-transactions/test/fee.spec.ts index b0791c09eb0..140be207624 100644 --- a/elements/lisk-transactions/test/fee.spec.ts +++ b/elements/lisk-transactions/test/fee.spec.ts @@ -13,7 +13,7 @@ * */ -import { utils, address } from '@liskhq/lisk-cryptography'; +import { utils, legacy } from '@liskhq/lisk-cryptography'; import { computeMinFee, getBytes } from '../src'; describe('fee', () => { @@ -42,7 +42,7 @@ describe('fee', () => { }, }; const passphrase1 = 'trim elegant oven term access apple obtain error grain excite lawn neck'; - const { publicKey: publicKey1 } = address.getAddressAndPublicKeyFromPassphrase(passphrase1); + const { publicKey: publicKey1 } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase1); const validTransaction = { moduleID: utils.intToBuffer(2, 4), commandID: utils.intToBuffer(0, 4), diff --git a/elements/lisk-transactions/test/sign.spec.ts b/elements/lisk-transactions/test/sign.spec.ts index 159fb96e8e3..8c913ce3dff 100644 --- a/elements/lisk-transactions/test/sign.spec.ts +++ b/elements/lisk-transactions/test/sign.spec.ts @@ -14,12 +14,10 @@ */ import { codec } from '@liskhq/lisk-codec'; -import { ed, utils, address } from '@liskhq/lisk-cryptography'; +import { ed, utils, legacy } from '@liskhq/lisk-cryptography'; import { getSigningBytes, - signTransaction, signTransactionWithPrivateKey, - signMultiSignatureTransaction, signMultiSignatureTransactionWithPrivateKey, } from '../src/sign'; import * as multisigScenario from '../fixtures/transaction_multisignature_registration/multisignature_registration_transaction.json'; @@ -97,10 +95,10 @@ describe('sign', () => { '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09', 'hex', ); - const { publicKey: publicKey1 } = address.getAddressAndPublicKeyFromPassphrase(passphrase1); - const { publicKey: publicKey2 } = address.getAddressAndPublicKeyFromPassphrase(passphrase2); - const { publicKey: publicKey3 } = address.getAddressAndPublicKeyFromPassphrase(passphrase3); - const { publicKey: publicKey4 } = address.getAddressAndPublicKeyFromPassphrase(passphrase4); + const { publicKey: publicKey1 } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase1); + const { publicKey: publicKey2 } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase2); + const { publicKey: publicKey3 } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase3); + const { publicKey: publicKey4 } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase4); const keys = { mandatoryKeys: [publicKey1, publicKey2], optionalKeys: [publicKey3, publicKey4], @@ -166,83 +164,6 @@ describe('sign', () => { }); }); - describe('signTransaction', () => { - it('should throw error for invalid network identifier', () => { - expect(() => - signTransaction(validTransaction, Buffer.alloc(0), passphrase1, validParamsSchema), - ).toThrow('Network identifier is required to sign a transaction'); - }); - - it('should throw error for invalid passphrase', () => { - expect(() => - signTransaction(validTransaction, networkIdentifier, '', validParamsSchema), - ).toThrow('Passphrase is required to sign a transaction'); - }); - - it('should throw error for invalid transaction object', () => { - const invalidTransactionObjects = [ - { ...validTransaction, moduleID: BigInt(8) }, - { ...validTransaction, nonce: 1 }, - { ...validTransaction, fee: 1000000 }, - { ...validTransaction, senderPublicKey: 1 }, - ]; - return invalidTransactionObjects.forEach(transactionObject => - expect(() => - signTransaction(transactionObject, networkIdentifier, passphrase1, validParamsSchema), - ).toThrow(), - ); - }); - - it('should throw error when params is null', () => { - return expect(() => - signTransaction( - { ...validTransaction, params: null }, - networkIdentifier, - passphrase1, - validParamsSchema, - ), - ).toThrow(new Error('Transaction object params must be of type object and not null')); - }); - - it('should throw error for invalid params object', () => { - const invalidParams = [ - { ...validTransaction, params: { ...validTransaction.params, amount: 1000 } }, - { - ...validTransaction, - params: { ...validTransaction.params, recipientAddress: 'dummyAddress' }, - }, - ]; - return invalidParams.forEach(transactionObject => - expect(() => - signTransaction(transactionObject, networkIdentifier, passphrase1, validParamsSchema), - ).toThrow(), - ); - }); - - it('should throw error when transaction senderPublicKey does not match public key from passphrase', () => { - return expect(() => - signTransaction( - validTransaction, - networkIdentifier, - 'this is incorrect passphrase', - validParamsSchema, - ), - ).toThrow('Transaction senderPublicKey does not match public key from passphrase'); - }); - - it('should return signed transaction for given params schema', () => { - const signedTransaction = signTransaction( - { ...validTransaction }, - networkIdentifier, - passphrase1, - validParamsSchema, - ); - expect((signedTransaction.signatures as Array)[0].length).toBeGreaterThan(0); - expect(signedTransaction.signatures).toHaveLength(1); - return expect(signedTransaction).toMatchSnapshot(); - }); - }); - describe('signTransactionWithPrivateKey', () => { it('should throw error for invalid network identifier', () => { expect(() => @@ -340,85 +261,6 @@ describe('sign', () => { }); }); - describe('signMultiSignatureTransaction', () => { - it('should throw error for invalid network identifier', () => { - expect(() => - signMultiSignatureTransaction( - { ...validTransaction }, - Buffer.alloc(0), - passphrase1, - keys, - validParamsSchema, - ), - ).toThrow('Network identifier is required to sign a transaction'); - }); - - it('should throw error for invalid passphrase', () => { - expect(() => - signMultiSignatureTransaction( - { ...validTransaction }, - networkIdentifier, - '', - keys, - validParamsSchema, - ), - ).toThrow('Passphrase is required to sign a transaction'); - }); - - it('should throw error when signatures property is not an array', () => { - expect(() => - signMultiSignatureTransaction( - { ...validTransaction }, - networkIdentifier, - passphrase1, - keys, - validParamsSchema, - ), - ).toThrow('Signatures must be of type array'); - }); - - it('should throw error for invalid transaction object', () => { - const invalidTransactionObjects = [ - { ...validTransaction, type: BigInt(8) }, - { ...validTransaction, nonce: 1 }, - { ...validTransaction, fee: 1000000 }, - { ...validTransaction, senderPublicKey: 1 }, - ]; - return invalidTransactionObjects.forEach(transactionObject => - expect(() => - signMultiSignatureTransaction( - transactionObject, - networkIdentifier, - passphrase1, - keys, - validParamsSchema, - ), - ).toThrow(), - ); - }); - - it('should throw error for invalid params object', () => { - const invalidParams = [ - { ...validTransaction, params: { ...validTransaction.params, amount: 1000 } }, - { - ...validTransaction, - params: { ...validTransaction.params, recipientAddress: 'dummyAddress' }, - }, - ]; - return invalidParams.forEach(transactionObject => - expect(() => - signMultiSignatureTransaction( - transactionObject, - networkIdentifier, - passphrase1, - keys, - validParamsSchema, - ), - ).toThrow(), - ); - }); - }); - describe('signMultiSignatureTransactionWithPrivateKey', () => { it('should throw error for invalid network identifier', () => { expect(() => @@ -522,9 +364,9 @@ describe('sign', () => { }); it('should add sender and mandatory public key signatures in right order for multisignature registration trx', () => { - const account1 = ed.getPrivateAndPublicKeyFromPassphrase(passphrase1); - const account2 = ed.getPrivateAndPublicKeyFromPassphrase(passphrase2); - const account3 = ed.getPrivateAndPublicKeyFromPassphrase(passphrase3); + const account1 = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase1); + const account2 = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase2); + const account3 = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase3); const mandatoryKeys = [account1.publicKey, account2.publicKey]; const optionalKeys = [account3.publicKey]; @@ -551,10 +393,6 @@ describe('sign', () => { multisigRegParams, true, ); - const transactionWithNetworkIdentifierBytes = Buffer.concat([ - networkIdentifier, - getSigningBytes(transactionObject, multisigRegParams), - ]); // Signing with non sender second mandatory key const signedTransactionNonSender = signMultiSignatureTransactionWithPrivateKey( @@ -565,21 +403,17 @@ describe('sign', () => { multisigRegParams, true, ); - const transactionWithNetworkIdentifierBytesNonSender = Buffer.concat([ - networkIdentifier, - getSigningBytes(signedTransaction, multisigRegParams), - ]); const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, - transactionWithNetworkIdentifierBytes, + getSigningBytes(transactionObject, multisigRegParams), account1.privateKey, ); const signatureNonSender = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, - transactionWithNetworkIdentifierBytesNonSender, + getSigningBytes(transactionObject, multisigRegParams), account2.privateKey, ); @@ -589,8 +423,8 @@ describe('sign', () => { }); it('should match the signatures of the mandatory keys in right order for transfer trx', () => { - const account1 = ed.getPrivateAndPublicKeyFromPassphrase(passphrase1); - const account2 = ed.getPrivateAndPublicKeyFromPassphrase(passphrase2); + const account1 = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase1); + const account2 = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase2); // Sender public key of account1 const transaction = { moduleID: utils.intToBuffer(2, 4), @@ -615,15 +449,11 @@ describe('sign', () => { { mandatoryKeys: [account2.publicKey, account1.publicKey], optionalKeys: [] }, validParamsSchema, ); - const transactionWithNetworkIdentifierBytes = Buffer.concat([ - networkIdentifier, - getSigningBytes(transactionObject, validParamsSchema), - ]); const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, - transactionWithNetworkIdentifierBytes, + getSigningBytes(transactionObject, validParamsSchema), account1.privateKey, ); @@ -636,15 +466,10 @@ describe('sign', () => { validParamsSchema, ); - const transactionMandatoryKeyWithNetworkIdentifierBytes = Buffer.concat([ - networkIdentifier, - getSigningBytes(signedTransaction, validParamsSchema), - ]); - const signatureMandatoryAccount = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, - transactionMandatoryKeyWithNetworkIdentifierBytes, + getSigningBytes(signedTransaction, validParamsSchema), account2.privateKey, ); @@ -683,10 +508,10 @@ describe('sign', () => { }; Object.values({ senderAccount, ...testCase.input.members }).forEach((member: any) => - signMultiSignatureTransaction( + signMultiSignatureTransactionWithPrivateKey( signedMultiSigTransaction, _networkIdentifier, - member.passphrase, + legacy.getPrivateAndPublicKeyFromPassphrase(member.passphrase).privateKey, decodedParams, multisigRegParams, true, @@ -694,7 +519,7 @@ describe('sign', () => { ); expect(signedMultiSigTransaction.signatures).toStrictEqual(signatures); - expect(signMultiSignatureTransaction).toMatchSnapshot(); + expect(signedMultiSigTransaction).toMatchSnapshot(); }); }); }); @@ -727,10 +552,10 @@ describe('sign', () => { const _networkIdentifier = Buffer.from(testCase.input.networkIdentifier, 'hex'); Object.values({ senderAccount, ...testCase.input.members }).forEach((member: any) => - signMultiSignatureTransaction( + signMultiSignatureTransactionWithPrivateKey( signedMultiSigTransaction, _networkIdentifier, - member.passphrase, + Buffer.from(member.privateKey, 'hex'), decodedParams, multisigRegParams, true, diff --git a/framework-plugins/lisk-framework-dashboard-plugin/src/ui/pages/MainPage.tsx b/framework-plugins/lisk-framework-dashboard-plugin/src/ui/pages/MainPage.tsx index 8dc9b4a371b..3b22d69d242 100644 --- a/framework-plugins/lisk-framework-dashboard-plugin/src/ui/pages/MainPage.tsx +++ b/framework-plugins/lisk-framework-dashboard-plugin/src/ui/pages/MainPage.tsx @@ -227,13 +227,12 @@ const MainPage: React.FC = () => { const generateNewAccount = () => { const accountPassphrase = (passphrase.Mnemonic.generateMnemonic() as unknown) as string; - const { address, publicKey } = cryptography.address.getAddressAndPublicKeyFromPassphrase( - accountPassphrase, - ); + const keys = cryptography.legacy.getPrivateAndPublicKeyFromPassphrase(accountPassphrase); + const address = cryptography.address.getAddressFromPublicKey(keys.publicKey); const lisk32Address = cryptography.address.getLisk32AddressFromAddress(address); const newAccount: Account = { passphrase: accountPassphrase, - publicKey: publicKey.toString('hex'), + publicKey: keys.publicKey.toString('hex'), binaryAddress: address.toString('hex'), base32Address: lisk32Address, }; @@ -275,9 +274,10 @@ const MainPage: React.FC = () => { // Send Transaction const handleSendTransaction = async (data: SendTransactionOptions) => { try { - const { publicKey, address } = cryptography.address.getAddressAndPublicKeyFromPassphrase( + const { publicKey } = cryptography.legacy.getPrivateAndPublicKeyFromPassphrase( data.passphrase, ); + const address = cryptography.address.getAddressFromPublicKey(publicKey); const moduleMeta = getClient().metadata.find(a => a.id === data.moduleID); if (!moduleMeta) { throw new Error( diff --git a/framework-plugins/lisk-framework-faucet-plugin/src/plugin/endpoint.ts b/framework-plugins/lisk-framework-faucet-plugin/src/plugin/endpoint.ts index 5aab8cca70d..efdb3682813 100644 --- a/framework-plugins/lisk-framework-faucet-plugin/src/plugin/endpoint.ts +++ b/framework-plugins/lisk-framework-faucet-plugin/src/plugin/endpoint.ts @@ -55,7 +55,7 @@ export class Endpoint extends BasePluginEndpoint { 'utf-8', ); - const { publicKey } = cryptography.address.getAddressAndPublicKeyFromPassphrase(passphrase); + const { publicKey } = cryptography.legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); this._state.publicKey = enable ? publicKey : undefined; this._state.passphrase = enable ? passphrase : undefined; diff --git a/framework-plugins/lisk-framework-report-misbehavior-plugin/src/endpoint.ts b/framework-plugins/lisk-framework-report-misbehavior-plugin/src/endpoint.ts index 457afdf062e..7ed82a0d08e 100644 --- a/framework-plugins/lisk-framework-report-misbehavior-plugin/src/endpoint.ts +++ b/framework-plugins/lisk-framework-report-misbehavior-plugin/src/endpoint.ts @@ -24,7 +24,7 @@ import { ReportMisbehaviorPluginConfig, State } from './types'; // eslint-disable-next-line prefer-destructuring const validator: liskValidator.LiskValidator = liskValidator.validator; -const { encrypt, address } = cryptography; +const { encrypt, legacy } = cryptography; export class Endpoint extends BasePluginEndpoint { private _state!: State; @@ -52,7 +52,7 @@ export class Endpoint extends BasePluginEndpoint { 'utf-8', ); - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(passphrase); + const { publicKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); this._state.publicKey = enable ? publicKey : undefined; this._state.passphrase = enable ? passphrase : undefined; diff --git a/framework-plugins/lisk-framework-report-misbehavior-plugin/src/report_misbehavior_plugin.ts b/framework-plugins/lisk-framework-report-misbehavior-plugin/src/report_misbehavior_plugin.ts index 395e39fa227..e17c9a1fa1d 100644 --- a/framework-plugins/lisk-framework-report-misbehavior-plugin/src/report_misbehavior_plugin.ts +++ b/framework-plugins/lisk-framework-report-misbehavior-plugin/src/report_misbehavior_plugin.ts @@ -30,7 +30,7 @@ import { ReportMisbehaviorPluginConfig, State } from './types'; import { configSchema } from './schemas'; import { Endpoint } from './endpoint'; -const { address, ed } = cryptography; +const { address, ed, legacy } = cryptography; const { BlockHeader, Transaction, TAG_TRANSACTION } = chain; export class ReportMisbehaviorPlugin extends BasePlugin { @@ -144,8 +144,10 @@ export class ReportMisbehaviorPlugin extends BasePlugin('auth_getAuthAccount', { - address: address.getAddressFromPassphrase(passphrase).toString('hex'), + address: address.getAddressFromPublicKey(keys.publicKey).toString('hex'), }); const pomTransactionParams = { @@ -164,7 +166,7 @@ export class ReportMisbehaviorPlugin extends BasePlugin { const { aggregationBits, signature, ...rawCertificate } = certificate; - return bls.signBLS( + return bls.signData( MESSAGE_TAG_CERTIFICATE, networkIdentifier, codec.encode(certificateSchema, rawCertificate), @@ -66,7 +66,7 @@ export const verifySingleCertificateSignature = ( validatorsHash: certificate.validatorsHash, }); - return bls.verifyBLS(MESSAGE_TAG_CERTIFICATE, networkIdentifier, message, signature, pk); + return bls.verifyData(MESSAGE_TAG_CERTIFICATE, networkIdentifier, message, signature, pk); }; export const verifyAggregateCertificateSignature = ( diff --git a/framework/src/engine/generator/endpoint.ts b/framework/src/engine/generator/endpoint.ts index b8c2bf03037..11c5a5a422f 100644 --- a/framework/src/engine/generator/endpoint.ts +++ b/framework/src/engine/generator/endpoint.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. */ -import { encrypt, ed, bls, address as cryptoAddress } from '@liskhq/lisk-cryptography'; +import { encrypt, bls, address as cryptoAddress, legacy } from '@liskhq/lisk-cryptography'; import { Batch, Database } from '@liskhq/lisk-db'; import { dataStructures } from '@liskhq/lisk-utils'; import { validator } from '@liskhq/lisk-validator'; @@ -102,7 +102,7 @@ export class Endpoint { const blsSK = bls.generatePrivateKey(Buffer.from(passphrase, 'utf-8')); const keypair = { - ...ed.getPrivateAndPublicKeyFromPassphrase(passphrase), + ...legacy.getPrivateAndPublicKeyFromPassphrase(passphrase), blsSecretKey: blsSK, }; diff --git a/framework/src/engine/generator/generator.ts b/framework/src/engine/generator/generator.ts index fef37b77f2e..dad57ed79de 100644 --- a/framework/src/engine/generator/generator.ts +++ b/framework/src/engine/generator/generator.ts @@ -23,7 +23,7 @@ import { EVENT_KEY_LENGTH, } from '@liskhq/lisk-chain'; import { codec } from '@liskhq/lisk-codec'; -import { encrypt, bls, ed, address as cryptoAddress } from '@liskhq/lisk-cryptography'; +import { encrypt, bls, legacy, address as cryptoAddress } from '@liskhq/lisk-cryptography'; import { Database, Batch, SparseMerkleTree } from '@liskhq/lisk-db'; import { TransactionPool, events } from '@liskhq/lisk-transaction-pool'; import { MerkleTree } from '@liskhq/lisk-tree'; @@ -341,7 +341,7 @@ export class Generator { throw new Error(decryptionError); } - const keypair = ed.getPrivateAndPublicKeyFromPassphrase(passphrase); + const keypair = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); const delegateAddress = cryptoAddress.getAddressFromPublicKey(keypair.publicKey); const blsSK = bls.generatePrivateKey(Buffer.from(passphrase, 'utf-8')); diff --git a/framework/src/testing/block_processing_env.ts b/framework/src/testing/block_processing_env.ts index 559289e35e4..b79bf7cc732 100644 --- a/framework/src/testing/block_processing_env.ts +++ b/framework/src/testing/block_processing_env.ts @@ -18,7 +18,7 @@ import * as fs from 'fs-extra'; import * as path from 'path'; import * as os from 'os'; import { Block, Chain, DataAccess, BlockHeader, Transaction, StateStore } from '@liskhq/lisk-chain'; -import { utils, ed } from '@liskhq/lisk-cryptography'; +import { utils, legacy } from '@liskhq/lisk-cryptography'; import { Database, StateDB } from '@liskhq/lisk-db'; import { objects } from '@liskhq/lisk-utils'; import { codec } from '@liskhq/lisk-codec'; @@ -116,7 +116,7 @@ const createProcessableBlock = async ( for (const tx of transactions) { await engine['_generator']['_pool'].add(tx); } - const { privateKey } = ed.getKeys(passphrase); + const { privateKey } = legacy.getKeys(passphrase); const block = await engine.generateBlock({ generatorAddress: validator, height: previousBlockHeader.height + 1, diff --git a/framework/src/testing/create_block.ts b/framework/src/testing/create_block.ts index b82803b18eb..2da677b2b8c 100644 --- a/framework/src/testing/create_block.ts +++ b/framework/src/testing/create_block.ts @@ -14,7 +14,7 @@ */ /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ import { Block, BlockHeader, Transaction, BlockHeaderAttrs, BlockAssets } from '@liskhq/lisk-chain'; -import { address, utils, ed } from '@liskhq/lisk-cryptography'; +import { address, utils, legacy } from '@liskhq/lisk-cryptography'; import { MerkleTree } from '@liskhq/lisk-tree'; interface CreateBlock { @@ -50,7 +50,7 @@ export const createBlockHeaderWithDefaults = (header?: Partial export const createFakeBlockHeader = (header?: Partial): BlockHeader => { const headerWithDefault = createBlockHeaderWithDefaults(header); - const { privateKey } = ed.getPrivateAndPublicKeyFromPassphrase( + const { privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase( utils.getRandomBytes(10).toString('hex'), ); headerWithDefault.sign(utils.getRandomBytes(32), privateKey); @@ -66,7 +66,7 @@ export const createBlock = async ({ assets, header, }: CreateBlock): Promise => { - const { publicKey, privateKey } = ed.getPrivateAndPublicKeyFromPassphrase(passphrase); + const { publicKey, privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); const txTree = new MerkleTree(); await txTree.init(transactions?.map(tx => tx.id) ?? []); diff --git a/framework/src/testing/create_transaction.ts b/framework/src/testing/create_transaction.ts index dc271b1e16e..14c9725ba71 100644 --- a/framework/src/testing/create_transaction.ts +++ b/framework/src/testing/create_transaction.ts @@ -15,7 +15,7 @@ import { Transaction } from '@liskhq/lisk-chain'; import { codec } from '@liskhq/lisk-codec'; -import { address, ed } from '@liskhq/lisk-cryptography'; +import { legacy } from '@liskhq/lisk-cryptography'; import { validateTransaction } from '@liskhq/lisk-transactions'; import { CommandClass } from './types'; @@ -38,7 +38,7 @@ export const createTransaction = ({ passphrase, networkIdentifier, }: CreateTransactionInput): Transaction => { - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(passphrase ?? ''); + const { publicKey, privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase ?? ''); // eslint-disable-next-line new-cap const commandInstance = new commandClass(); const commandID = commandInstance.id; @@ -73,8 +73,7 @@ export const createTransaction = ({ throw new Error('Network identifier is required to sign a transaction'); } - const keys = ed.getKeys(passphrase); - result.sign(networkIdentifier, keys.privateKey); + result.sign(networkIdentifier, privateKey); return result; }; diff --git a/framework/test/fixtures/blocks.ts b/framework/test/fixtures/blocks.ts index 2d93860ac88..cc3ce3ded63 100644 --- a/framework/test/fixtures/blocks.ts +++ b/framework/test/fixtures/blocks.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. */ -import { utils, address, ed } from '@liskhq/lisk-cryptography'; +import { utils, address, legacy } from '@liskhq/lisk-cryptography'; import { Mnemonic } from '@liskhq/lisk-passphrase'; import { MerkleTree } from '@liskhq/lisk-tree'; import { Block, BlockAssets, BlockHeader, BlockHeaderAttrs, Transaction } from '@liskhq/lisk-chain'; @@ -49,7 +49,7 @@ export const genesisBlock = (): Block => { const getKeyPair = (): { publicKey: Buffer; privateKey: Buffer } => { const passphrase = Mnemonic.generateMnemonic(); - return ed.getPrivateAndPublicKeyFromPassphrase(passphrase); + return legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); }; export const createFakeBlockHeader = (header?: Partial): BlockHeader => diff --git a/framework/test/integration/node/processor/block_process/process_block.spec.ts b/framework/test/integration/node/processor/block_process/process_block.spec.ts index 0b3325af344..e8beb49da19 100644 --- a/framework/test/integration/node/processor/block_process/process_block.spec.ts +++ b/framework/test/integration/node/processor/block_process/process_block.spec.ts @@ -14,7 +14,7 @@ import { Block, Chain, DataAccess, Transaction } from '@liskhq/lisk-chain'; import { regularMerkleTree } from '@liskhq/lisk-tree'; -import { ed } from '@liskhq/lisk-cryptography'; +import { legacy } from '@liskhq/lisk-cryptography'; import { nodeUtils } from '../../../../utils'; import * as testing from '../../../../../src/testing'; import { @@ -148,7 +148,7 @@ describe('Process block', () => { const passphrase = await getPassphraseFromDefaultConfig( invalidBlock.header.generatorAddress, ); - const { privateKey } = ed.getKeys(passphrase); + const { privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); invalidBlock.header.sign(processEnv.getNetworkId(), privateKey); await expect(processEnv.process(invalidBlock)).rejects.toThrow( 'Failed to verify transaction', @@ -199,7 +199,7 @@ describe('Process block', () => { newBlock = await processEnv.createBlock(); (newBlock.header as any).height = 99; const passphrase = await getPassphraseFromDefaultConfig(newBlock.header.generatorAddress); - const { privateKey } = ed.getKeys(passphrase); + const { privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); newBlock.header.sign(processEnv.getNetworkId(), privateKey); }); @@ -295,7 +295,7 @@ describe('Process block', () => { const passphrase = await getPassphraseFromDefaultConfig( invalidBlock.header.generatorAddress, ); - const { privateKey } = ed.getKeys(passphrase); + const { privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); invalidBlock.header.sign(processEnv.getNetworkId(), privateKey); try { await processEnv.process(invalidBlock); diff --git a/framework/test/integration/node/processor/report_misbehavior_transaction.spec.ts b/framework/test/integration/node/processor/report_misbehavior_transaction.spec.ts index dfee63ed337..d201a3d88e1 100644 --- a/framework/test/integration/node/processor/report_misbehavior_transaction.spec.ts +++ b/framework/test/integration/node/processor/report_misbehavior_transaction.spec.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. */ import { Block, BlockHeader } from '@liskhq/lisk-chain'; -import { address, ed } from '@liskhq/lisk-cryptography'; +import { address, legacy } from '@liskhq/lisk-cryptography'; import { nodeUtils } from '../../../utils'; import { @@ -70,12 +70,12 @@ describe('Transaction order', () => { ...header.toObject(), height: 100, }); - const { privateKey } = ed.getKeys(blockGenerator); + const { privateKey, publicKey } = legacy.getPrivateAndPublicKeyFromPassphrase(blockGenerator); conflictingHeader.sign(networkIdentifier, privateKey); const originalBalance = await processEnv.invoke<{ availableBalance: string }>( 'token_getBalance', { - address: address.getAddressFromPassphrase(blockGenerator).toString('hex'), + address: address.getAddressFromPublicKey(publicKey).toString('hex'), tokenID: DEFAULT_TOKEN_ID.toString('hex'), }, ); @@ -94,12 +94,12 @@ describe('Transaction order', () => { const updatedDelegate = await processEnv.invoke<{ pomHeights: number[] }>( 'dpos_getDelegate', { - address: address.getAddressFromPassphrase(blockGenerator).toString('hex'), + address: address.getAddressFromPublicKey(publicKey).toString('hex'), }, ); expect(updatedDelegate.pomHeights).toHaveLength(1); const balance = await processEnv.invoke<{ availableBalance: string }>('token_getBalance', { - address: address.getAddressFromPassphrase(blockGenerator).toString('hex'), + address: address.getAddressFromPublicKey(publicKey).toString('hex'), tokenID: DEFAULT_TOKEN_ID.toString('hex'), }); expect(balance.availableBalance).toEqual( diff --git a/framework/test/integration/testing/block_processing_env.spec.ts b/framework/test/integration/testing/block_processing_env.spec.ts index bd404d10930..c0811c0395d 100644 --- a/framework/test/integration/testing/block_processing_env.spec.ts +++ b/framework/test/integration/testing/block_processing_env.spec.ts @@ -12,7 +12,7 @@ * Removal or modification of this copyright notice is prohibited. */ -import { address } from '@liskhq/lisk-cryptography'; +import { address, legacy } from '@liskhq/lisk-cryptography'; import * as testing from '../../../src/testing'; import { defaultConfig } from '../../../src/testing/fixtures/config'; @@ -79,7 +79,7 @@ describe('getBlockProcessingEnv', () => { // Arrange const lastBlockHeader = processEnv.getLastBlock().header; const passphrase = await processEnv.getNextValidatorPassphrase(lastBlockHeader); - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(passphrase); + const { publicKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); // Act & Assert const block = await processEnv.createBlock(); diff --git a/framework/test/unit/engine/consensus/certificate_generation/commit_pool.spec.ts b/framework/test/unit/engine/consensus/certificate_generation/commit_pool.spec.ts index 28c224d6bcd..44fd85612c6 100644 --- a/framework/test/unit/engine/consensus/certificate_generation/commit_pool.spec.ts +++ b/framework/test/unit/engine/consensus/certificate_generation/commit_pool.spec.ts @@ -807,7 +807,7 @@ describe('CommitPool', () => { const encodedCertificate = codec.encode(certificateSchema, certificate); signatures = privateKeys.map(privateKey => - bls.signBLS(MESSAGE_TAG_CERTIFICATE, networkIdentifier, encodedCertificate, privateKey), + bls.signData(MESSAGE_TAG_CERTIFICATE, networkIdentifier, encodedCertificate, privateKey), ); pubKeySignaturePairs = Array.from({ length: 103 }, (_, i) => ({ diff --git a/framework/test/unit/engine/consensus/certificate_generation/utils.spec.ts b/framework/test/unit/engine/consensus/certificate_generation/utils.spec.ts index ac090eac362..690486d7881 100644 --- a/framework/test/unit/engine/consensus/certificate_generation/utils.spec.ts +++ b/framework/test/unit/engine/consensus/certificate_generation/utils.spec.ts @@ -78,7 +78,7 @@ describe('utils', () => { validatorsHash: Buffer.alloc(0), }; const encodedCertificate = codec.encode(certificateSchema, certificate); - signature = bls.signBLS( + signature = bls.signData( MESSAGE_TAG_CERTIFICATE, networkIdentifier, encodedCertificate, @@ -111,7 +111,7 @@ describe('utils', () => { const encodedCertificate = codec.encode(certificateSchema, certificate); - signature = bls.signBLS( + signature = bls.signData( MESSAGE_TAG_CERTIFICATE, networkIdentifier, encodedCertificate, @@ -192,7 +192,7 @@ describe('utils', () => { const encodedCertificate = codec.encode(certificateSchema, certificate); signatures = privateKeys.map(privateKey => - bls.signBLS(MESSAGE_TAG_CERTIFICATE, networkIdentifier, encodedCertificate, privateKey), + bls.signData(MESSAGE_TAG_CERTIFICATE, networkIdentifier, encodedCertificate, privateKey), ); pubKeySignaturePairs = Array.from({ length: 103 }, (_, i) => ({ diff --git a/framework/test/unit/engine/consensus/consensus.spec.ts b/framework/test/unit/engine/consensus/consensus.spec.ts index f8fb8b6ec75..b5fb858ed05 100644 --- a/framework/test/unit/engine/consensus/consensus.spec.ts +++ b/framework/test/unit/engine/consensus/consensus.spec.ts @@ -17,7 +17,7 @@ import { codec } from '@liskhq/lisk-codec'; import { when } from 'jest-when'; import { Mnemonic } from '@liskhq/lisk-passphrase'; import { InMemoryDatabase } from '@liskhq/lisk-db'; -import { utils, address as cryptoAddress, bls, ed } from '@liskhq/lisk-cryptography'; +import { utils, address as cryptoAddress, bls, legacy } from '@liskhq/lisk-cryptography'; import { ApplyPenaltyError } from '../../../../src/errors'; import { Consensus } from '../../../../src/engine/consensus/consensus'; import { NetworkEndpoint } from '../../../../src/engine/consensus/network_endpoint'; @@ -236,7 +236,8 @@ describe('consensus', () => { describe('certifySingleCommit', () => { const passphrase = Mnemonic.generateMnemonic(256); - const address = cryptoAddress.getAddressFromPassphrase(passphrase); + const { publicKey } = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + const address = cryptoAddress.getAddressFromPublicKey(publicKey); const blsSK = bls.generatePrivateKey(Buffer.from(passphrase, 'utf-8')); const blsPK = bls.getPublicKeyFromPrivateKey(blsSK); const blockHeader = createFakeBlockHeader({ height: 303 }); @@ -866,7 +867,7 @@ describe('consensus', () => { it('should be success when valid signature', async () => { const passphrase = Mnemonic.generateMnemonic(); - const keyPair = ed.getPrivateAndPublicKeyFromPassphrase(passphrase); + const keyPair = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); const blockHeader = createFakeBlockHeader(); (blockHeader as any).generatorAddress = cryptoAddress.getAddressFromPublicKey( diff --git a/framework/test/unit/engine/generator/generator.spec.ts b/framework/test/unit/engine/generator/generator.spec.ts index d1f81dfa4e4..1e3be8c5bdb 100644 --- a/framework/test/unit/engine/generator/generator.spec.ts +++ b/framework/test/unit/engine/generator/generator.spec.ts @@ -13,7 +13,7 @@ */ import { EventEmitter } from 'events'; import { BlockAssets, Chain, Transaction } from '@liskhq/lisk-chain'; -import { bls, utils, address as cryptoAddress, ed } from '@liskhq/lisk-cryptography'; +import { bls, utils, address as cryptoAddress, legacy } from '@liskhq/lisk-cryptography'; import { InMemoryDatabase, Database } from '@liskhq/lisk-db'; import { codec } from '@liskhq/lisk-codec'; import { when } from 'jest-when'; @@ -623,9 +623,10 @@ describe('generator', () => { describe('events CONSENSUS_EVENT_FINALIZED_HEIGHT_CHANGED', () => { const passphrase = Mnemonic.generateMnemonic(256); - const address = cryptoAddress.getAddressFromPassphrase(passphrase); + const keys = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); + const address = cryptoAddress.getAddressFromPublicKey(keys.publicKey); const keypair = { - ...ed.getPrivateAndPublicKeyFromPassphrase(passphrase), + ...keys, blsSecretKey: bls.generatePrivateKey(Buffer.from(passphrase, 'utf-8')), }; const blsPK = bls.getPublicKeyFromPrivateKey(keypair.blsSecretKey); diff --git a/framework/test/unit/modules/auth/auth_module.spec.ts b/framework/test/unit/modules/auth/auth_module.spec.ts index 717c7933812..0e466504258 100644 --- a/framework/test/unit/modules/auth/auth_module.spec.ts +++ b/framework/test/unit/modules/auth/auth_module.spec.ts @@ -13,7 +13,7 @@ */ import { Mnemonic } from '@liskhq/lisk-passphrase'; import { codec } from '@liskhq/lisk-codec'; -import { utils, ed, address as cryptoAddress } from '@liskhq/lisk-cryptography'; +import { utils, ed, address as cryptoAddress, legacy } from '@liskhq/lisk-cryptography'; import { Transaction, transactionSchema, TAG_TRANSACTION, BlockAssets } from '@liskhq/lisk-chain'; import { objects as ObjectUtils } from '@liskhq/lisk-utils'; import { when } from 'jest-when'; @@ -77,7 +77,7 @@ describe('AuthModule', () => { }); passphrase = Mnemonic.generateMnemonic(); - passphraseDerivedKeys = ed.getPrivateAndPublicKeyFromPassphrase(passphrase); + passphraseDerivedKeys = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); const address = cryptoAddress.getAddressFromPublicKey(passphraseDerivedKeys.publicKey); when(subStoreMock) @@ -351,11 +351,11 @@ describe('AuthModule', () => { validTestTransaction = new Transaction(decodedMultiSignature); - const signature = ed.signDataWithPassphrase( + const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getBytes(), - passphrase, + passphraseDerivedKeys.privateKey, ); (transaction.signatures as any).push(signature); @@ -406,11 +406,11 @@ describe('AuthModule', () => { signatures: [], }); - const signature = ed.signDataWithPassphrase( + const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getBytes(), - passphrase, + passphraseDerivedKeys.privateKey, ); (transaction.signatures as any).push(signature); @@ -469,11 +469,11 @@ describe('AuthModule', () => { signatures: [], }); - const signature = ed.signDataWithPassphrase( + const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getBytes(), - passphrase, + passphraseDerivedKeys.privateKey, ); (transaction.signatures as any).push(signature); @@ -533,7 +533,7 @@ describe('AuthModule', () => { for (const aMember of Object.values(members)) { aMember.keys = { - ...ed.getPrivateAndPublicKeyFromPassphrase(aMember.passphrase), + ...legacy.getPrivateAndPublicKeyFromPassphrase(aMember.passphrase), }; aMember.address = cryptoAddress.getAddressFromPublicKey(aMember.keys.publicKey); } @@ -571,29 +571,29 @@ describe('AuthModule', () => { it('should not throw for valid transaction', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); @@ -629,11 +629,11 @@ describe('AuthModule', () => { }); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); @@ -656,29 +656,29 @@ describe('AuthModule', () => { it('should not throw for valid transaction when first optional is present', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); @@ -700,31 +700,31 @@ describe('AuthModule', () => { it('should not throw for valid transaction when second optional is present', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push(Buffer.from('')); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalB.passphrase, + (members as any).optionalB.keys.privateKey, ), ); @@ -745,29 +745,29 @@ describe('AuthModule', () => { it('should throw for transaction where non optional absent signature is not empty buffer', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalB.passphrase, + (members as any).optionalB.keys.privateKey, ), ); const context = testing @@ -791,38 +791,38 @@ describe('AuthModule', () => { it('should throw error if number of provided signatures is bigger than numberOfSignatures in account asset', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalB.passphrase, + (members as any).optionalB.keys.privateKey, ), ); @@ -847,20 +847,20 @@ describe('AuthModule', () => { it('should throw error if number of provided signatures is smaller than numberOfSignatures in account asset', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); @@ -887,31 +887,31 @@ describe('AuthModule', () => { it('should throw for transaction with valid numberOfSignatures but missing mandatory key signature', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push(Buffer.from('')); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalB.passphrase, + (members as any).optionalB.keys.privateKey, ), ); @@ -932,29 +932,29 @@ describe('AuthModule', () => { it('should throw error if any of the mandatory signatures is not valid', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); @@ -977,31 +977,31 @@ describe('AuthModule', () => { it('should throw error if any of the optional signatures is not valid', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push(Buffer.from('')); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalB.passphrase, + (members as any).optionalB.keys.privateKey, ), ); @@ -1029,29 +1029,29 @@ describe('AuthModule', () => { it('should throw error if mandatory signatures are not in order', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); @@ -1078,31 +1078,31 @@ describe('AuthModule', () => { it('should throw error if optional signatures are not in order', async () => { // Arrange (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryA.passphrase, + (members as any).mandatoryA.keys.privateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).mandatoryB.passphrase, + (members as any).mandatoryB.keys.privateKey, ), ); (transaction.signatures as any).push(Buffer.from('')); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - (members as any).optionalA.passphrase, + (members as any).optionalA.keys.privateKey, ), ); diff --git a/framework/test/unit/modules/auth/endpoint.spec.ts b/framework/test/unit/modules/auth/endpoint.spec.ts index b1a2b5cc37e..e6913f9e2d1 100644 --- a/framework/test/unit/modules/auth/endpoint.spec.ts +++ b/framework/test/unit/modules/auth/endpoint.spec.ts @@ -1,6 +1,6 @@ import { NotFoundError, TAG_TRANSACTION, Transaction } from '@liskhq/lisk-chain'; import { codec } from '@liskhq/lisk-codec'; -import { ed, address as cryptoAddress, utils } from '@liskhq/lisk-cryptography'; +import { ed, address as cryptoAddress, utils, legacy } from '@liskhq/lisk-cryptography'; import { when } from 'jest-when'; import { AuthModule } from '../../../../src/modules/auth'; import { AuthEndpoint } from '../../../../src/modules/auth/endpoint'; @@ -26,6 +26,7 @@ describe('AuthEndpoint', () => { [key: string]: { passphrase: string; publicKey?: Buffer; + privateKey?: Buffer; address?: Buffer; }; } @@ -50,11 +51,12 @@ describe('AuthEndpoint', () => { }; for (const account of Object.values(accounts)) { - const { address, publicKey } = cryptoAddress.getAddressAndPublicKeyFromPassphrase( + const { publicKey, privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase( account.passphrase, ); - account.address = address; + account.address = cryptoAddress.getAddressFromPublicKey(publicKey); account.publicKey = publicKey; + account.privateKey = privateKey; } // Existing is an abbr. for existing account @@ -64,7 +66,9 @@ describe('AuthEndpoint', () => { const existingAddress = accounts.targetAccount.address as Buffer; const nonExistingAddress = cryptoAddress.getAddressFromPublicKey(nonExistingSenderPublicKey); - const existingPassphrase = accounts.targetAccount.passphrase; + const existingPrivateKey = legacy.getPrivateAndPublicKeyFromPassphrase( + accounts.targetAccount.passphrase, + ).privateKey; let authModule: AuthModule; let authEndpoint: AuthEndpoint; @@ -156,11 +160,11 @@ describe('AuthEndpoint', () => { signatures: [], }); - const signature = ed.signDataWithPassphrase( + const signature = ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getBytes(), - existingPassphrase, + existingPrivateKey, ); (transaction.signatures as any).push(signature); @@ -203,29 +207,29 @@ describe('AuthEndpoint', () => { }); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - accounts.mandatoryOne.passphrase, + accounts.mandatoryOne.privateKey as Buffer, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - accounts.mandatoryTwo.passphrase, + accounts.mandatoryTwo.privateKey as Buffer, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - accounts.optionalOne.passphrase, + accounts.optionalOne.privateKey as Buffer, ), ); @@ -274,47 +278,47 @@ describe('AuthEndpoint', () => { }); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - existingPassphrase, + existingPrivateKey, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - accounts.mandatoryOne.passphrase, + accounts.mandatoryOne.privateKey as Buffer, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - accounts.mandatoryTwo.passphrase, + accounts.mandatoryTwo.privateKey as Buffer, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - accounts.optionalOne.passphrase, + accounts.optionalOne.privateKey as Buffer, ), ); (transaction.signatures as any).push( - ed.signDataWithPassphrase( + ed.signDataWithPrivateKey( TAG_TRANSACTION, networkIdentifier, transaction.getSigningBytes(), - accounts.optionalTwo.passphrase, + accounts.optionalTwo.privateKey as Buffer, ), ); diff --git a/framework/test/unit/modules/dpos_v2/commands/pom.spec.ts b/framework/test/unit/modules/dpos_v2/commands/pom.spec.ts index 65da5487809..fc53a5ff6bc 100644 --- a/framework/test/unit/modules/dpos_v2/commands/pom.spec.ts +++ b/framework/test/unit/modules/dpos_v2/commands/pom.spec.ts @@ -15,7 +15,7 @@ import { when } from 'jest-when'; import { BlockHeader, blockHeaderSchema, Transaction } from '@liskhq/lisk-chain'; import { objects } from '@liskhq/lisk-utils'; -import { address, utils } from '@liskhq/lisk-cryptography'; +import { address, utils, legacy } from '@liskhq/lisk-cryptography'; import { codec } from '@liskhq/lisk-codec'; import { ReportDelegateMisbehaviorCommand } from '../../../../../src/modules/dpos_v2/commands/pom'; import * as testing from '../../../../../src/testing'; @@ -59,10 +59,10 @@ describe('ReportDelegateMisbehaviorCommand', () => { const header = testing.createFakeBlockHeader({ height: blockHeight, }); - const { - address: delegate1Address, - publicKey: delegate1PublicKey, - } = address.getAddressAndPublicKeyFromPassphrase(utils.getRandomBytes(20).toString('utf8')); + const { publicKey: delegate1PublicKey } = legacy.getPrivateAndPublicKeyFromPassphrase( + utils.getRandomBytes(20).toString('utf8'), + ); + const delegate1Address = address.getAddressFromPublicKey(delegate1PublicKey); const defaultDelegateInfo = { totalVotesReceived: BigInt(100000000), selfVotes: BigInt(0), diff --git a/framework/test/unit/modules/dpos_v2/genesis_block_test_data.ts b/framework/test/unit/modules/dpos_v2/genesis_block_test_data.ts index 6d1aaf42725..53b2874db44 100644 --- a/framework/test/unit/modules/dpos_v2/genesis_block_test_data.ts +++ b/framework/test/unit/modules/dpos_v2/genesis_block_test_data.ts @@ -11,12 +11,12 @@ * * Removal or modification of this copyright notice is prohibited. */ -import { bls, address as cryptoAddress, ed } from '@liskhq/lisk-cryptography'; +import { bls, address as cryptoAddress, legacy } from '@liskhq/lisk-cryptography'; import { Mnemonic } from '@liskhq/lisk-passphrase'; export const validators = new Array(120).fill(0).map((_, i) => { const passphrase = Mnemonic.generateMnemonic(); - const keys = ed.getKeys(passphrase); + const keys = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); const address = cryptoAddress.getAddressFromPublicKey(keys.publicKey); const blsPrivateKey = bls.generatePrivateKey(Buffer.from(passphrase, 'utf-8')); const blsPublicKey = bls.getPublicKeyFromPrivateKey(blsPrivateKey); diff --git a/framework/test/unit/modules/token/commands/transfer.spec.ts b/framework/test/unit/modules/token/commands/transfer.spec.ts index 70e90c239ff..4b4e2aefe90 100644 --- a/framework/test/unit/modules/token/commands/transfer.spec.ts +++ b/framework/test/unit/modules/token/commands/transfer.spec.ts @@ -14,7 +14,7 @@ import { Transaction } from '@liskhq/lisk-chain'; import { codec } from '@liskhq/lisk-codec'; -import { address, utils } from '@liskhq/lisk-cryptography'; +import { address, legacy, utils } from '@liskhq/lisk-cryptography'; import { VerifyStatus } from '../../../../../src'; import { TokenAPI } from '../../../../../src/modules/token/api'; import { TransferCommand } from '../../../../../src/modules/token/commands/transfer'; @@ -167,8 +167,8 @@ describe('Transfer command', () => { describe('execute', () => { let stateStore: PrefixedStateReadWriter; - const sender = address.getAddressAndPublicKeyFromPassphrase('sender'); - const recipient = address.getAddressAndPublicKeyFromPassphrase('recipient'); + const sender = legacy.getPrivateAndPublicKeyFromPassphrase('sender'); + const recipient = legacy.getPrivateAndPublicKeyFromPassphrase('recipient'); const thirdTokenID = Buffer.from([1, 0, 0, 0, 4, 0, 0, 0]); const tokenID = Buffer.from([0, 0, 0, 1, 0, 0, 0, 0]); const senderBalance = BigInt(200000000); @@ -179,22 +179,22 @@ describe('Transfer command', () => { stateStore = new PrefixedStateReadWriter(new InMemoryPrefixedStateDB()); const userStore = stateStore.getStore(MODULE_ID_TOKEN_BUFFER, STORE_PREFIX_USER); await userStore.setWithSchema( - getUserStoreKey(sender.address, localTokenID), + getUserStoreKey(address.getAddressFromPublicKey(sender.publicKey), localTokenID), { availableBalance: senderBalance, lockedBalances: [] }, userStoreSchema, ); await userStore.setWithSchema( - getUserStoreKey(sender.address, secondTokenID), + getUserStoreKey(address.getAddressFromPublicKey(sender.publicKey), secondTokenID), { availableBalance: senderBalance, lockedBalances: [] }, userStoreSchema, ); await userStore.setWithSchema( - getUserStoreKey(sender.address, thirdTokenID), + getUserStoreKey(address.getAddressFromPublicKey(sender.publicKey), thirdTokenID), { availableBalance: senderBalance, lockedBalances: [] }, userStoreSchema, ); await userStore.setWithSchema( - getUserStoreKey(recipient.address, localTokenID), + getUserStoreKey(address.getAddressFromPublicKey(recipient.publicKey), localTokenID), { availableBalance: recipientBalance, lockedBalances: [] }, userStoreSchema, ); @@ -237,7 +237,7 @@ describe('Transfer command', () => { params: codec.encode(transferParamsSchema, { tokenID: thirdTokenID, amount: recipientBalance + BigInt(1), - recipientAddress: recipient.address, + recipientAddress: address.getAddressFromPublicKey(recipient.publicKey), data: '1'.repeat(64), }), signatures: [utils.getRandomBytes(64)], @@ -250,7 +250,7 @@ describe('Transfer command', () => { // Recipient should receive full amount const userStore = stateStore.getStore(MODULE_ID_TOKEN_BUFFER, STORE_PREFIX_USER); const result = await userStore.getWithSchema( - getUserStoreKey(recipient.address, thirdTokenID), + getUserStoreKey(address.getAddressFromPublicKey(recipient.publicKey), thirdTokenID), userStoreSchema, ); expect(result.availableBalance).toEqual(recipientBalance + BigInt(1)); @@ -401,7 +401,7 @@ describe('Transfer command', () => { params: codec.encode(transferParamsSchema, { tokenID, amount, - recipientAddress: recipient.address, + recipientAddress: address.getAddressFromPublicKey(recipient.publicKey), data: '1'.repeat(64), }), signatures: [utils.getRandomBytes(64)], @@ -414,7 +414,7 @@ describe('Transfer command', () => { // Recipient should get full amount const userStore = stateStore.getStore(MODULE_ID_TOKEN_BUFFER, STORE_PREFIX_USER); const result = await userStore.getWithSchema( - getUserStoreKey(recipient.address, localTokenID), + getUserStoreKey(address.getAddressFromPublicKey(recipient.publicKey), localTokenID), userStoreSchema, ); expect(result.availableBalance).toEqual(amount + recipientBalance); diff --git a/framework/test/utils/mocks/account.ts b/framework/test/utils/mocks/account.ts index a80116062b0..d15cc1b29fb 100644 --- a/framework/test/utils/mocks/account.ts +++ b/framework/test/utils/mocks/account.ts @@ -12,18 +12,18 @@ * Removal or modification of this copyright notice is prohibited. * */ -import { address, bls, ed } from '@liskhq/lisk-cryptography'; +import { address, bls, legacy } from '@liskhq/lisk-cryptography'; import { Mnemonic } from '@liskhq/lisk-passphrase'; export const createAccount = () => { const passphrase = Mnemonic.generateMnemonic(256); - const keys = ed.getKeys(passphrase); + const keys = legacy.getPrivateAndPublicKeyFromPassphrase(passphrase); const blsPrivateKey = bls.generatePrivateKey(Buffer.from(passphrase, 'utf-8')); const blsPublicKey = bls.getPublicKeyFromPrivateKey(blsPrivateKey); const blsPoP = bls.popProve(blsPrivateKey); return { passphrase, - address: address.getAddressFromPassphrase(passphrase), + address: address.getAddressFromPublicKey(keys.publicKey), publicKey: keys.publicKey, privateKey: keys.privateKey, blsPrivateKey, diff --git a/framework/test/utils/mocks/transaction.ts b/framework/test/utils/mocks/transaction.ts index cdf6829391c..5fa18eba37b 100644 --- a/framework/test/utils/mocks/transaction.ts +++ b/framework/test/utils/mocks/transaction.ts @@ -15,7 +15,7 @@ import { Transaction, BlockHeader, TAG_TRANSACTION } from '@liskhq/lisk-chain'; import { codec } from '@liskhq/lisk-codec'; -import { utils, bls, ed, address } from '@liskhq/lisk-cryptography'; +import { utils, bls, ed, legacy } from '@liskhq/lisk-cryptography'; import { signMultiSignatureTransaction } from '@liskhq/lisk-transactions'; import { registerMultisignatureParamsSchema } from '../../../src/modules/auth/schemas'; import { @@ -43,7 +43,7 @@ export const createTransferTransaction = (input: { amount: input.amount ?? BigInt('10000000000'), data: '', }); - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(input.passphrase); + const { publicKey, privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(input.passphrase); const tx = new Transaction({ moduleID: utils.intToBuffer(2, 4), @@ -55,7 +55,7 @@ export const createTransferTransaction = (input: { signatures: [], }); tx.signatures.push( - ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), input.passphrase), + ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), privateKey), ); return tx; }; @@ -67,7 +67,7 @@ export const createDelegateRegisterTransaction = (input: { username: string; fee?: bigint; }): Transaction => { - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(input.passphrase); + const { publicKey, privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(input.passphrase); const blsSK = bls.generatePrivateKey(Buffer.from(input.passphrase, 'utf-8')); const blsPK = bls.getPublicKeyFromPrivateKey(blsSK); const blsPop = bls.popProve(blsSK); @@ -88,7 +88,7 @@ export const createDelegateRegisterTransaction = (input: { signatures: [], }); tx.signatures.push( - ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), input.passphrase), + ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), privateKey), ); return tx; }; @@ -103,7 +103,7 @@ export const createDelegateVoteTransaction = (input: { const encodedAsset = codec.encode(voteCommandParamsSchema, { votes: input.votes, }); - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(input.passphrase); + const { publicKey, privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(input.passphrase); const tx = new Transaction({ moduleID: utils.intToBuffer(13, 4), @@ -115,7 +115,7 @@ export const createDelegateVoteTransaction = (input: { signatures: [], }); tx.signatures.push( - ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), input.passphrase), + ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), privateKey), ); return tx; }; @@ -140,13 +140,14 @@ export const createMultiSignRegisterTransaction = (input: { optionalKeys: input.optionalKeys, numberOfSignatures: input.numberOfSignatures, }; - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(input.senderPassphrase); + const { publicKey } = legacy.getPrivateAndPublicKeyFromPassphrase(input.senderPassphrase); const transaction = [...input.passphrases].reduce>( (prev, current) => { + const { privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(current); return signMultiSignatureTransaction( prev, input.networkIdentifier, - current, + privateKey, params, registerMultisignatureParamsSchema, true, @@ -188,10 +189,11 @@ export const createMultisignatureTransferTransaction = (input: { const encodedAsset = codec.encode(command.schema, params); const transaction = input.passphrases.reduce>( (prev, current) => { + const { privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(current); return signMultiSignatureTransaction( prev, input.networkIdentifier, - current, + privateKey, { mandatoryKeys: input.mandatoryKeys, optionalKeys: input.optionalKeys, @@ -226,7 +228,7 @@ export const createReportMisbehaviorTransaction = (input: { header1: input.header1.getBytes(), header2: input.header2.getBytes(), }); - const { publicKey } = address.getAddressAndPublicKeyFromPassphrase(input.passphrase); + const { publicKey, privateKey } = legacy.getPrivateAndPublicKeyFromPassphrase(input.passphrase); const tx = new Transaction({ moduleID: utils.intToBuffer(13, 4), @@ -238,7 +240,7 @@ export const createReportMisbehaviorTransaction = (input: { signatures: [], }); tx.signatures.push( - ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), input.passphrase), + ed.signData(TAG_TRANSACTION, input.networkIdentifier, tx.getSigningBytes(), privateKey), ); return tx; };