From 18da7ea5f15857b9dc8ede8ca63ff0f4c352a3ee Mon Sep 17 00:00:00 2001 From: jinoosss <112360739+jinoosss@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:55:20 +0900 Subject: [PATCH] refactor: remove legacy clients (#537) --- packages/adena-module/package.json | 2 - .../adena-module/src/utils/messages-legacy.ts | 283 ------------------ .../src/wallet/keyring/hd-wallet-keyring.ts | 3 +- .../src/wallet/keyring/keyring-util.ts | 19 +- .../src/wallet/keyring/ledger-keyring.ts | 3 +- .../src/wallet/keyring/private-key-keyring.ts | 5 +- .../src/wallet/keyring/web3-auth-keyring.ts | 3 +- yarn.lock | 117 -------- 8 files changed, 9 insertions(+), 426 deletions(-) delete mode 100644 packages/adena-module/src/utils/messages-legacy.ts diff --git a/packages/adena-module/package.json b/packages/adena-module/package.json index 2cbe18d5..db4241e5 100644 --- a/packages/adena-module/package.json +++ b/packages/adena-module/package.json @@ -44,9 +44,7 @@ "dependencies": { "@cosmjs/ledger-amino": "^0.32.4", "@gnolang/gno-js-client": "1.3.0", - "@gnolang/gno-js-client-legacy": "npm:@gnolang/gno-js-client@1.2.3", "@gnolang/tm2-js-client": "1.2.1", - "@gnolang/tm2-js-client-legacy": "npm:@gnolang/tm2-js-client@1.1.7", "@ledgerhq/hw-transport": "^6.30.4", "@ledgerhq/hw-transport-mocker": "^6.28.4", "@ledgerhq/hw-transport-webhid": "^6.28.4", diff --git a/packages/adena-module/src/utils/messages-legacy.ts b/packages/adena-module/src/utils/messages-legacy.ts deleted file mode 100644 index fc836434..00000000 --- a/packages/adena-module/src/utils/messages-legacy.ts +++ /dev/null @@ -1,283 +0,0 @@ -import { Any, PubKeySecp256k1, Tx, TxFee, TxSignature } from '@gnolang/tm2-js-client-legacy'; -import { MsgCall, MsgAddPackage, MsgSend, MsgEndpoint } from '@gnolang/gno-js-client-legacy'; -import { MemPackage, MemFile, MsgRun } from '@gnolang/gno-js-client-legacy/bin/proto/gno/vm'; -import { fromBase64 } from '../encoding'; - -export interface Document { - chain_id: string; - account_number: string; - sequence: string; - fee: { - amount: { - denom: string; - amount: string; - }[]; - gas: string; - granter?: string; - payer?: string; - }; - msgs: { - type: string; - value: any; - }[]; - memo: string; -} - -export const decodeTxMessages = (messages: Any[]): any[] => { - return messages.map((m: Any) => { - switch (m.typeUrl) { - case MsgEndpoint.MSG_CALL: { - const decodedMessage = MsgCall.decode(m.value); - const messageJson = MsgCall.toJSON(decodedMessage) as any; - return { - '@type': m.typeUrl, - ...messageJson, - send: messageJson?.send || '', - }; - } - case MsgEndpoint.MSG_SEND: { - const decodedMessage = MsgSend.decode(m.value); - const messageJson = MsgSend.toJSON(decodedMessage) as object; - return { - '@type': m.typeUrl, - ...messageJson, - }; - } - case MsgEndpoint.MSG_ADD_PKG: { - const decodedMessage = MsgAddPackage.decode(m.value); - const messageJson = MsgAddPackage.toJSON(decodedMessage) as object; - return { - '@type': m.typeUrl, - ...messageJson, - }; - } - case MsgEndpoint.MSG_RUN: { - const decodedMessage = MsgRun.decode(m.value); - const messageJson = MsgRun.toJSON(decodedMessage) as object; - return { - '@type': m.typeUrl, - ...messageJson, - }; - } - default: - throw new Error(`unsupported message type ${m.typeUrl}`); - } - }); -}; - -function createMemPackage(memPackage: RawMemPackage) { - return MemPackage.create({ - name: memPackage.Name, - path: memPackage.Path, - files: memPackage.Files.map((file: any) => - MemFile.create({ - name: file.Name, - body: file.Body, - }), - ), - }); -} - -function encodeMessageValue(message: { type: string; value: any }) { - switch (message.type) { - case MsgEndpoint.MSG_ADD_PKG: { - const value = message.value; - const msgAddPackage = MsgAddPackage.create({ - creator: value.creator, - deposit: value.deposit || null, - package: value.package ? createMemPackage(value.package) : undefined, - }); - return Any.create({ - typeUrl: MsgEndpoint.MSG_ADD_PKG, - value: MsgAddPackage.encode(msgAddPackage).finish(), - }); - } - case MsgEndpoint.MSG_CALL: { - const args: string[] = message.value.args - ? message.value.args.length === 0 - ? null - : message.value.args - : null; - const result = MsgCall.create({ - args: args, - caller: message.value.caller, - func: message.value.func, - pkg_path: message.value.pkg_path, - send: message.value.send || '', - }); - return Any.create({ - typeUrl: MsgEndpoint.MSG_CALL, - value: MsgCall.encode(result).finish(), - }); - } - case MsgEndpoint.MSG_SEND: { - return Any.create({ - typeUrl: MsgEndpoint.MSG_SEND, - value: MsgSend.encode(MsgSend.create(message.value)).finish(), - }); - } - case MsgEndpoint.MSG_RUN: { - const value = message.value; - const msgRun = MsgRun.create({ - caller: value.caller, - send: value.send || null, - package: value.package ? createMemPackage(value.package) : undefined, - }); - return Any.create({ - typeUrl: MsgEndpoint.MSG_RUN, - value: MsgRun.encode(msgRun).finish(), - }); - } - default: { - return Any.create({ - typeUrl: MsgEndpoint.MSG_CALL, - value: MsgCall.encode(MsgCall.fromJSON(message.value)).finish(), - }); - } - } -} - -export function documentToTx(document: Document): Tx { - const messages: Any[] = document.msgs.map(encodeMessageValue); - return { - messages, - fee: TxFee.create({ - gasWanted: document.fee.gas, - gasFee: document.fee.amount - .map((feeAmount) => `${feeAmount.amount}${feeAmount.denom}`) - .join(','), - }), - signatures: [], - memo: document.memo, - }; -} - -export function txToDocument(tx: Tx) { - return Tx.toJSON(tx); -} - -export interface RawBankSendMessage { - '@type': string; - from_address: string; - to_address: string; - amount: string; -} - -export interface RawVmCallMessage { - '@type': string; - caller: string; - func: string; - send: string; - pkg_path: string; - args: string[]; -} - -export interface RawVmAddPackageMessage { - '@type': string; - creator: string; - deposit: string; - package: { - Name: string; - Path: string; - Files: { - Name: string; - Body: string; - }[]; - }; -} - -export interface RawVmRunMessage { - '@type': string; - caller: string; - send: string; - package: { - Name: string; - Path: string; - Files: { - Name: string; - Body: string; - }[]; - }; -} - -export interface RawMemPackage { - Name: string; - Path: string; - Files: { - Name: string; - Body: string; - }[]; -} - -export type RawTxMessageType = - | RawBankSendMessage - | RawVmCallMessage - | RawVmAddPackageMessage - | RawVmRunMessage; - -export interface RawTx { - msg: RawTxMessageType[]; - fee: { gas_wanted: string; gas_fee: string }; - signatures: { - pub_key: { - '@type': string; - value: string; - }; - signature: string; - }[]; - memo: string; -} - -/** - * Change transaction json string to a Signed Tx. - * - * @param str - * @returns Tx | null - */ -export const strToSignedTx = (str: string): Tx | null => { - let rawTx = null; - try { - rawTx = JSON.parse(str); - } catch (e) { - console.error(e); - } - - if (rawTx === null) return null; - - try { - const document = rawTx as RawTx; - const messages: Any[] = document.msg - .map((msg) => ({ - type: msg['@type'], - value: { ...msg }, - })) - .map(encodeMessageValue); - return { - messages, - fee: TxFee.create({ - gasWanted: document.fee.gas_wanted, - gasFee: document.fee.gas_fee, - }), - signatures: document.signatures.map((signature) => { - const publicKeyBytes = fromBase64(signature?.pub_key?.value || ''); - const wrappedPublicKeyValue: PubKeySecp256k1 = { - key: publicKeyBytes, - }; - const publicKeyTypeUrl = signature?.pub_key['@type'] || ''; - const encodedPublicKeyBytes = PubKeySecp256k1.encode(wrappedPublicKeyValue).finish(); - const signatureBytes = fromBase64(signature?.signature || ''); - return TxSignature.create({ - pubKey: { - typeUrl: publicKeyTypeUrl, - value: encodedPublicKeyBytes, - }, - signature: signatureBytes, - }); - }), - memo: document.memo, - }; - } catch (e) { - console.error(e); - return null; - } -}; diff --git a/packages/adena-module/src/wallet/keyring/hd-wallet-keyring.ts b/packages/adena-module/src/wallet/keyring/hd-wallet-keyring.ts index 68dc6000..61b686f9 100644 --- a/packages/adena-module/src/wallet/keyring/hd-wallet-keyring.ts +++ b/packages/adena-module/src/wallet/keyring/hd-wallet-keyring.ts @@ -6,7 +6,6 @@ import { Provider, TransactionEndpoint, } from '@gnolang/tm2-js-client'; -import { Wallet as Tm2WalletLegacy } from '@gnolang/tm2-js-client-legacy'; import { v4 as uuidv4 } from 'uuid'; import { Bip39, EnglishMnemonic } from '../../crypto'; import { useTm2Wallet, decodeTxMessages, Document, documentToTx, makeSignedTx } from './../..'; @@ -66,7 +65,7 @@ export class HDWalletKeyring implements Keyring { return this.signByWallet(wallet, document); } - private async signByWallet(wallet: Tm2Wallet | Tm2WalletLegacy, document: Document) { + private async signByWallet(wallet: Tm2Wallet, document: Document) { const signedTx = await makeSignedTx(wallet, document); return { signed: signedTx, diff --git a/packages/adena-module/src/wallet/keyring/keyring-util.ts b/packages/adena-module/src/wallet/keyring/keyring-util.ts index 0216f957..dbb42439 100644 --- a/packages/adena-module/src/wallet/keyring/keyring-util.ts +++ b/packages/adena-module/src/wallet/keyring/keyring-util.ts @@ -5,14 +5,7 @@ import { LedgerKeyring } from './ledger-keyring'; import { PrivateKeyKeyring } from './private-key-keyring'; import { Web3AuthKeyring } from './web3-auth-keyring'; import { Tx, Wallet as Tm2Wallet } from '@gnolang/tm2-js-client'; -import { Wallet as Tm2WalletLegacy } from '@gnolang/tm2-js-client-legacy'; import { Document, documentToTx, decodeTxMessages } from './../../utils/messages'; -import { - documentToTx as documentToTxLegacy, - decodeTxMessages as decodeTxMessagesLegacy, -} from './../../utils/messages-legacy'; - -const LEGACY_NETWORKS = ['test3']; export function isHDWalletKeyring(keyring: Keyring): keyring is HDWalletKeyring { return keyring.type === 'HD_WALLET'; @@ -49,17 +42,13 @@ export function hasPrivateKey( return false; } -export function useTm2Wallet(document: Document): typeof Tm2Wallet | typeof Tm2WalletLegacy { - if (LEGACY_NETWORKS.includes(document.chain_id)) { - return Tm2WalletLegacy; - } +export function useTm2Wallet(document: Document): typeof Tm2Wallet { return Tm2Wallet; } -export function makeSignedTx(wallet: Tm2Wallet | Tm2WalletLegacy, document: Document): Promise { - const isLegacy = LEGACY_NETWORKS.includes(document.chain_id); - const tx = isLegacy ? documentToTxLegacy(document) : documentToTx(document); - const decodeTxMessageFunction = isLegacy ? decodeTxMessagesLegacy : decodeTxMessages; +export function makeSignedTx(wallet: Tm2Wallet, document: Document): Promise { + const tx = documentToTx(document); + const decodeTxMessageFunction = decodeTxMessages; return wallet.signTransaction(tx, decodeTxMessageFunction); } diff --git a/packages/adena-module/src/wallet/keyring/ledger-keyring.ts b/packages/adena-module/src/wallet/keyring/ledger-keyring.ts index 3e185904..9bcdb734 100644 --- a/packages/adena-module/src/wallet/keyring/ledger-keyring.ts +++ b/packages/adena-module/src/wallet/keyring/ledger-keyring.ts @@ -1,6 +1,5 @@ import { v4 as uuidv4 } from 'uuid'; import { Provider, TransactionEndpoint, Wallet as Tm2Wallet } from '@gnolang/tm2-js-client'; -import { Wallet as Tm2WalletLegacy } from '@gnolang/tm2-js-client-legacy'; import { Keyring, KeyringData, KeyringType } from './keyring'; import { generateHDPath, Tx } from '@gnolang/tm2-js-client'; import { LedgerConnector } from '@cosmjs/ledger-amino'; @@ -46,7 +45,7 @@ export class LedgerKeyring implements Keyring { return this.signByWallet(wallet, document); } - private async signByWallet(wallet: Tm2Wallet | Tm2WalletLegacy, document: Document) { + private async signByWallet(wallet: Tm2Wallet, document: Document) { const signedTx = await makeSignedTx(wallet, document); return { signed: signedTx, diff --git a/packages/adena-module/src/wallet/keyring/private-key-keyring.ts b/packages/adena-module/src/wallet/keyring/private-key-keyring.ts index 7776bf3e..bd1c09e6 100644 --- a/packages/adena-module/src/wallet/keyring/private-key-keyring.ts +++ b/packages/adena-module/src/wallet/keyring/private-key-keyring.ts @@ -1,8 +1,7 @@ import { Provider, TransactionEndpoint, Tx, Wallet as Tm2Wallet } from '@gnolang/tm2-js-client'; -import { Wallet as Tm2WalletLegacy } from '@gnolang/tm2-js-client-legacy'; import { v4 as uuidv4 } from 'uuid'; import { Keyring, KeyringData, KeyringType } from './keyring'; -import { Document, documentToTx, decodeTxMessages, useTm2Wallet, makeSignedTx } from './../..'; +import { Document, useTm2Wallet, makeSignedTx } from './../..'; export class PrivateKeyKeyring implements Keyring { public readonly id: string; @@ -34,7 +33,7 @@ export class PrivateKeyKeyring implements Keyring { return this.signByWallet(wallet, document); } - private async signByWallet(wallet: Tm2Wallet | Tm2WalletLegacy, document: Document) { + private async signByWallet(wallet: Tm2Wallet, document: Document) { const signedTx = await makeSignedTx(wallet, document); return { signed: signedTx, diff --git a/packages/adena-module/src/wallet/keyring/web3-auth-keyring.ts b/packages/adena-module/src/wallet/keyring/web3-auth-keyring.ts index 523d019d..6c19bf47 100644 --- a/packages/adena-module/src/wallet/keyring/web3-auth-keyring.ts +++ b/packages/adena-module/src/wallet/keyring/web3-auth-keyring.ts @@ -1,5 +1,4 @@ import { Provider, TransactionEndpoint, Tx, Wallet as Tm2Wallet } from '@gnolang/tm2-js-client'; -import { Wallet as Tm2WalletLegacy } from '@gnolang/tm2-js-client-legacy'; import { v4 as uuidv4 } from 'uuid'; import { Keyring, KeyringData, KeyringType } from './keyring'; @@ -36,7 +35,7 @@ export class Web3AuthKeyring implements Keyring { return this.signByWallet(wallet, document); } - private async signByWallet(wallet: Tm2Wallet | Tm2WalletLegacy, document: Document) { + private async signByWallet(wallet: Tm2Wallet, document: Document) { const signedTx = await makeSignedTx(wallet, document); return { signed: signedTx, diff --git a/yarn.lock b/yarn.lock index 0bbe085a..b5dba293 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1692,18 +1692,6 @@ __metadata: languageName: node linkType: hard -"@cosmjs/amino@npm:^0.32.0, @cosmjs/amino@npm:^0.32.2": - version: 0.32.2 - resolution: "@cosmjs/amino@npm:0.32.2" - dependencies: - "@cosmjs/crypto": ^0.32.2 - "@cosmjs/encoding": ^0.32.2 - "@cosmjs/math": ^0.32.2 - "@cosmjs/utils": ^0.32.2 - checksum: 7c0e82e7956039d4bb41e67c14b4fa5758ef2aae5240d7a37968cf08d95f162cd31ee70b4a274445c4c20f696bdcafe05892f5b31319c847b8b930959518ec21 - languageName: node - linkType: hard - "@cosmjs/amino@npm:^0.32.4": version: 0.32.4 resolution: "@cosmjs/amino@npm:0.32.4" @@ -1716,21 +1704,6 @@ __metadata: languageName: node linkType: hard -"@cosmjs/crypto@npm:^0.32.0, @cosmjs/crypto@npm:^0.32.2": - version: 0.32.2 - resolution: "@cosmjs/crypto@npm:0.32.2" - dependencies: - "@cosmjs/encoding": ^0.32.2 - "@cosmjs/math": ^0.32.2 - "@cosmjs/utils": ^0.32.2 - "@noble/hashes": ^1 - bn.js: ^5.2.0 - elliptic: ^6.5.4 - libsodium-wrappers-sumo: ^0.7.11 - checksum: 7600bd48f718b8c352038bf96af325b9597310ccf51d2885bba4603a567b52d59446d193288f519ddf354a8668bd0ffba899c518642b3c9bb039bf24a7261faa - languageName: node - linkType: hard - "@cosmjs/crypto@npm:^0.32.4": version: 0.32.4 resolution: "@cosmjs/crypto@npm:0.32.4" @@ -1746,17 +1719,6 @@ __metadata: languageName: node linkType: hard -"@cosmjs/encoding@npm:^0.32.2": - version: 0.32.2 - resolution: "@cosmjs/encoding@npm:0.32.2" - dependencies: - base64-js: ^1.3.0 - bech32: ^1.1.4 - readonly-date: ^1.0.0 - checksum: a9ee00b730aa7f422cf3946097cb96a0c9d5d7a9f6b3aa31d8e96e4f815220928226ae2133b76e835c781df0231e9691c8ebf1c98f68468e2f936a1d431e4c30 - languageName: node - linkType: hard - "@cosmjs/encoding@npm:^0.32.4": version: 0.32.4 resolution: "@cosmjs/encoding@npm:0.32.4" @@ -1768,21 +1730,6 @@ __metadata: languageName: node linkType: hard -"@cosmjs/ledger-amino@npm:^0.32.0": - version: 0.32.2 - resolution: "@cosmjs/ledger-amino@npm:0.32.2" - dependencies: - "@cosmjs/amino": ^0.32.2 - "@cosmjs/crypto": ^0.32.2 - "@cosmjs/encoding": ^0.32.2 - "@cosmjs/math": ^0.32.2 - "@cosmjs/utils": ^0.32.2 - ledger-cosmos-js: ^2.1.8 - semver: ^7.5.2 - checksum: d9442379d359c9664984878a6d796eee6d3313b553101d1d08a81eb84b898e4b5864b030f8a56106ef5cb714a8fb9cfb3b6b3dabcd74e3991affc892d795a3a2 - languageName: node - linkType: hard - "@cosmjs/ledger-amino@npm:^0.32.4": version: 0.32.4 resolution: "@cosmjs/ledger-amino@npm:0.32.4" @@ -1798,15 +1745,6 @@ __metadata: languageName: node linkType: hard -"@cosmjs/math@npm:^0.32.2": - version: 0.32.2 - resolution: "@cosmjs/math@npm:0.32.2" - dependencies: - bn.js: ^5.2.0 - checksum: 3ea9c811d1b53b61f24c10ae4e3707fb7f926194202e5c57ea5aa1a41996ffc872ce5599967f74cc6f289d30e064d372b7ff30c1128d682847b865ca597aca59 - languageName: node - linkType: hard - "@cosmjs/math@npm:^0.32.4": version: 0.32.4 resolution: "@cosmjs/math@npm:0.32.4" @@ -1816,13 +1754,6 @@ __metadata: languageName: node linkType: hard -"@cosmjs/utils@npm:^0.32.2": - version: 0.32.2 - resolution: "@cosmjs/utils@npm:0.32.2" - checksum: 71d757eb6da243af9b4f7a64bfa1f64e3253d1a012518aa152b43115fb93b8af8ec60cda469dbc1e847a77cec0f126a64fa681b5b407ee396cbd96f30f4ae480 - languageName: node - linkType: hard - "@cosmjs/utils@npm:^0.32.4": version: 0.32.4 resolution: "@cosmjs/utils@npm:0.32.4" @@ -2183,18 +2114,6 @@ __metadata: languageName: node linkType: hard -"@gnolang/gno-js-client-legacy@npm:@gnolang/gno-js-client@1.2.3": - version: 1.2.3 - resolution: "@gnolang/gno-js-client@npm:1.2.3" - dependencies: - "@cosmjs/ledger-amino": ^0.32.0 - "@gnolang/tm2-js-client": ^1.2.0 - long: ^5.2.3 - protobufjs: ^7.2.3 - checksum: 6dcac75e01d516c37009903ec91fad38d9a00b8dfc40a4a5d5a279a1beefba7b78da8af33761a2d11e29e8ce9e3158e7a2b7239847b9908f964207c9c9039419 - languageName: node - linkType: hard - "@gnolang/gno-js-client@npm:1.3.0": version: 1.3.0 resolution: "@gnolang/gno-js-client@npm:1.3.0" @@ -2207,23 +2126,6 @@ __metadata: languageName: node linkType: hard -"@gnolang/tm2-js-client-legacy@npm:@gnolang/tm2-js-client@1.1.7": - version: 1.1.7 - resolution: "@gnolang/tm2-js-client@npm:1.1.7" - dependencies: - "@cosmjs/amino": ^0.32.0 - "@cosmjs/crypto": ^0.32.0 - "@cosmjs/ledger-amino": ^0.32.0 - "@types/uuid": ^9.0.4 - axios: ^1.4.0 - long: ^5.2.3 - protobufjs: ^7.2.3 - uuid: ^9.0.1 - ws: ^8.16.0 - checksum: 75d36a02a3e52dffa1a0c41629e5eb8fcadca4353a580324f2733dfd153471b7e2678ce92dad1eca91bd5b826de2b86ff3498b05d77b0613242e6f02047ed4d9 - languageName: node - linkType: hard - "@gnolang/tm2-js-client@npm:1.2.1, @gnolang/tm2-js-client@npm:^1.2.1": version: 1.2.1 resolution: "@gnolang/tm2-js-client@npm:1.2.1" @@ -2241,23 +2143,6 @@ __metadata: languageName: node linkType: hard -"@gnolang/tm2-js-client@npm:^1.2.0": - version: 1.2.0 - resolution: "@gnolang/tm2-js-client@npm:1.2.0" - dependencies: - "@cosmjs/amino": ^0.32.0 - "@cosmjs/crypto": ^0.32.0 - "@cosmjs/ledger-amino": ^0.32.0 - "@types/uuid": ^9.0.4 - axios: ^1.4.0 - long: ^5.2.3 - protobufjs: ^7.2.3 - uuid: ^9.0.1 - ws: ^8.16.0 - checksum: a18932afd1649b7f16a3f16a96ded271b7ff264907e02621b4f4d88073f80ded47afab897d715019d2f1457c7a68c194f2e18e3da4e6057e154f55a59522874e - languageName: node - linkType: hard - "@humanwhocodes/config-array@npm:^0.11.13": version: 0.11.14 resolution: "@humanwhocodes/config-array@npm:0.11.14" @@ -7534,9 +7419,7 @@ __metadata: "@babel/preset-typescript": ^7.23.3 "@cosmjs/ledger-amino": ^0.32.4 "@gnolang/gno-js-client": 1.3.0 - "@gnolang/gno-js-client-legacy": "npm:@gnolang/gno-js-client@1.2.3" "@gnolang/tm2-js-client": 1.2.1 - "@gnolang/tm2-js-client-legacy": "npm:@gnolang/tm2-js-client@1.1.7" "@ledgerhq/hw-transport": ^6.30.4 "@ledgerhq/hw-transport-mocker": ^6.28.4 "@ledgerhq/hw-transport-webhid": ^6.28.4