Skip to content

Commit

Permalink
Fix CLI (#77)
Browse files Browse the repository at this point in the history
* ensure the wallet is set on the connection. this is done by making _wallet an alias for connection.wallet so that setting in the init() works correctly

* yarn lock

* not only

* back to norm al

* tests all run

* cant do this

---------

Co-authored-by: Aaron <[email protected]>
  • Loading branch information
aaronmgdr and aaronmgdr authored Jan 18, 2024
1 parent 16bb901 commit 4861e71
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/cli/bin/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// eslint-disable-next-line node/shebang, unicorn/prefer-top-level-await
;(async () => {
const oclif = await import('@oclif/core')
console.info('Running in development mode')
await oclif.execute({ development: true, dir: __dirname })
})()
1 change: 1 addition & 0 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
preset: 'ts-jest',
testMatch: ['<rootDir>/src/**/?(*.)+(spec|test).ts'],
setupFiles: ['<rootDir>/src/test-utils/setup.ts'],
setupFilesAfterEnv: ['@celo/dev-utils/lib/matchers'],
globalSetup: '<rootDir>/src/test-utils/setup.global.ts',
globalTeardown: '<rootDir>/src/test-utils/teardown.global.ts',
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"bugs": "https://github.com/celo-org/developer-tooling/issues?utf8=%E2%9C%93&q=label%3Acli+",
"types": "lib/index.d.ts",
"main": "lib/index.js",
"bin": "./bin/run",
"bin": {
"celocli": "./bin/run.js",
"dev": ".bin/dev.js"
},
"keywords": [
"celo",
"celocli",
Expand All @@ -20,12 +23,13 @@
},
"scripts": {
"clean": "tsc -b . --clean",
"dev": "ts-node ./bin/dev.js",
"build": "tsc -b .",
"docs": "./generate_docs.sh",
"lint": "yarn run --top-level eslint -c .eslintrc.js ",
"prepublish": "",
"prepack": "yarn run build && oclif manifest && oclif readme",
"test": "TZ=UTC jest --runInBand"
"test": "TZ=UTC yarn jest --runInBand"
},
"dependencies": {
"@celo/abis": "10.0.0",
Expand Down Expand Up @@ -68,6 +72,7 @@
"devDependencies": {
"@celo/celo-devchain": "^6.0.3",
"@celo/dev-utils": "0.0.1-beta.1",
"@celo/typescript": "workspace:^",
"@types/debug": "^4.1.4",
"@types/fs-extra": "^8.0.0",
"@types/humanize-duration": "^3.27.0",
Expand Down
20 changes: 12 additions & 8 deletions packages/cli/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { stableTokenInfos } from '@celo/contractkit/lib/celo-tokens'
import { AzureHSMWallet } from '@celo/wallet-hsm-azure'
import { AddressValidation, newLedgerWalletWithSetup } from '@celo/wallet-ledger'
import { LocalWallet } from '@celo/wallet-local'
import _TransportNodeHid from '@ledgerhq/hw-transport-node-hid'
import { Command, Flags } from '@oclif/core'
import chalk from 'chalk'
import net from 'net'
import Web3 from 'web3'
import { getGasCurrency, getNodeUrl } from './utils/config'
import { enumEntriesDupWithLowercase, requireNodeIsSynced } from './utils/helpers'

export const gasOptions = {
auto: 'auto',
Auto: 'auto',
Expand Down Expand Up @@ -100,7 +100,6 @@ export abstract class BaseCommand extends Command {

private _web3: Web3 | null = null
private _kit: ContractKit | null = null
private _wallet?: ReadOnlyWallet

async getWeb3() {
if (!this._web3) {
Expand All @@ -114,6 +113,14 @@ export abstract class BaseCommand extends Command {
return this._web3
}

get _wallet(): ReadOnlyWallet | undefined {
return this._wallet
}

set _wallet(wallet: ReadOnlyWallet | undefined) {
this._kit!.connection.wallet = wallet
}

async newWeb3() {
const res = await this.parse()
const nodeUrl = (res.flags && res.flags.node) || getNodeUrl(this.config.configDir)
Expand All @@ -125,7 +132,6 @@ export abstract class BaseCommand extends Command {
async getKit() {
if (!this._kit) {
this._kit = newKitFromWeb3(await this.getWeb3())
this._kit.connection.wallet = this._wallet
}

const res = await this.parse()
Expand All @@ -151,14 +157,12 @@ export abstract class BaseCommand extends Command {
}

if (res.flags.useLedger) {
let transport
try {
// Importing for ledger uses only fixes running jest tests
const _TransportNodeHid = (await import('@ledgerhq/hw-transport-node-hid')).default
// types seem to be suggesting 2 defaults but js is otherwise for TransportNodeHid
const TransportNodeHid: typeof _TransportNodeHid.default =
const TransportNodeHid: typeof _TransportNodeHid =
// @ts-expect-error
_TransportNodeHid.default || _TransportNodeHid
transport = await TransportNodeHid.open('')
const transport = await TransportNodeHid.open('')
const derivationPathIndexes = res.raw.some(
(value) => (value as any).flag === 'ledgerCustomAddresses'
)
Expand Down
55 changes: 55 additions & 0 deletions packages/cli/src/commands/account/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ContractKit, newKitFromWeb3 } from '@celo/contractkit'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { AddressValidation } from '@celo/wallet-ledger/lib/ledger-wallet'
import { LocalWallet } from '@celo/wallet-local'
import Web3 from 'web3'
import { testLocally } from '../../test-utils/cliUtils'
import List from './list'
process.env.NO_SYNCCHECK = 'true'

jest.mock('@celo/wallet-ledger', () => {
return {
AddressValidation,
newLedgerWalletWithSetup: jest.fn(() => {
const wallet = new LocalWallet()
jest.spyOn(wallet, 'getAccounts').mockImplementation(() => {
return [
'0x7457d5E02197480Db681D3fdF256c7acA21bDc12',
'0x91c987bf62D25945dB517BDAa840A6c661374402',
]
})
return wallet
}),
}
})

testWithGanache('account:list', (web3: Web3) => {
let account: string
let accounts: string[]
let kit: ContractKit

beforeEach(async () => {
accounts = await web3.eth.getAccounts()
kit = newKitFromWeb3(web3)
account = accounts[0]

const accountsInstance = await kit.contracts.getAccounts()
await accountsInstance.createAccount().sendAndWaitForReceipt({ from: account })
})
test('shows the list of accounts', async () => {
const spy = jest.spyOn(console, 'log')

await testLocally(List, [])
expect(spy).toHaveBeenCalledWith('All Addresses: ', accounts)
})

test('shows the list of accounts when --useLedger given', async () => {
const spy = jest.spyOn(console, 'log')

await testLocally(List, ['--useLedger'])
expect(spy).toHaveBeenCalledWith('Ledger Addresses: ', [
'0x7457d5E02197480Db681D3fdF256c7acA21bDc12',
'0x91c987bf62D25945dB517BDAa840A6c661374402',
])
})
})
5 changes: 2 additions & 3 deletions packages/cli/src/commands/account/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ export default class AccountList extends BaseCommand {
async run() {
const kit = await this.getKit()
const res = await this.parse(AccountList)

// Retreive accounts from the connected Celo node.
// Retrieve accounts from the connected Celo node.
const allAddresses = !res.flags.local ? await kit.connection.getAccounts() : []

// Get addresses from the local wallet.
const localAddresses = res.flags.local ?? true ? await kit.connection.getLocalAccounts() : []
const localAddresses = res.flags.local ?? true ? kit.connection.getLocalAccounts() : []

// Display the addresses.
const localName = res.flags.useLedger ? 'Ledger' : 'Local'
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/src/test-utils/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jest.mock('@ledgerhq/hw-transport-node-hid', () => {
return {
default: {
open: jest.fn(() => {
return {
send: jest.fn(() => new Promise(() => {})),
decorateAppAPIMethods: jest.fn(),
close: jest.fn(),
}
}),
},
}
})
2 changes: 1 addition & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@celo/typescript/tsconfig.library.json",
"extends": "../typescript/tsconfig.library.json",
"compilerOptions": {
"resolveJsonModule": true,
"moduleResolution": "node16",
Expand Down
6 changes: 4 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,7 @@ __metadata:
"@celo/governance": "npm:^5.0.9-beta.0"
"@celo/identity": "npm:^5.1.1"
"@celo/phone-utils": "npm:^6.0.0"
"@celo/typescript": "workspace:^"
"@celo/utils": "npm:^5.0.6"
"@celo/wallet-hsm-azure": "npm:^5.1.1"
"@celo/wallet-ledger": "npm:^5.1.1"
Expand Down Expand Up @@ -1591,7 +1592,8 @@ __metadata:
web3: "npm:1.10.0"
web3-utils: "npm:^1.10.0"
bin:
celocli: ./bin/run
celocli: ./bin/run.js
dev: .bin/dev.js
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1868,7 +1870,7 @@ __metadata:
languageName: unknown
linkType: soft

"@celo/typescript@npm:0.0.1, @celo/typescript@workspace:packages/typescript":
"@celo/typescript@npm:0.0.1, @celo/typescript@workspace:^, @celo/typescript@workspace:packages/typescript":
version: 0.0.0-use.local
resolution: "@celo/typescript@workspace:packages/typescript"
languageName: unknown
Expand Down

0 comments on commit 4861e71

Please sign in to comment.