Skip to content

Commit

Permalink
[NayNay] Updating to new sdk (#43)
Browse files Browse the repository at this point in the history
* wip: addresses are the bane of my existence

* fixed up transfers, added progress bar

* converting hex to bigint to be able use the right value when transferring

* wip: started changes to cli for new sdk, finally got the sdk to link to this project

* wip: update balance check to work with new sdk

* wip: updated transfer with new sdk methods; updated new key creation with correct methods from new sdk

* Mixmix/types+lint (#44)

* Improves README readability. (#42)

* Improves README readability.

* Update README.md

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

---------

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* remove deps, move files, add license, fix package.json

* linting hooks, tidy

* mostly fix types?

---------

Co-authored-by: Johnny <9611008+johnnymatthews@users.noreply.github.com>
Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* removing zaps; changing language around wallet

* wip: getting register ready

* ignor built files

* wip: "singletonish" entropy and keyring

* wip: pulling in frankies changes with a bit of cleanup

* wip: minor fixes, removing mnemonic from key generation

* wip: setting up keyrings

* wip: updated main to initialize with selecting or create new account

* wip: initializing entropy in main, passing new entropy to flows

* wip: can get acct on cli initialization; entropy intitialized; entropy passed to balance flow for now

* wip: remvoed use of wallet namespace (switched to manage accounts); main is now updated to what is expected when talking about having a selected acct or force user to create a new one

* wip: balance is now working with the new flow

* wip: cleanup of manage accounts folder

* wip: updating register to use account selected from config

* Mixmix/debug (#45)

* Improves README readability. (#42)

* Improves README readability.

* Update README.md

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

---------

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* remove deps, move files, add license, fix package.json

* linting hooks, tidy

* mostly fix types?

* replace console.log > debug

* improve debug by stringifying output

* fixups

* more tidy

* undo some changes

* Update src/common/initializeEntropy.ts

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

---------

Co-authored-by: Johnny <9611008+johnnymatthews@users.noreply.github.com>
Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* wip: registration looks to be working with properly created acct

* wip: user programs added fucntion to check if program exists on endpoint

* wip: registration and removing a program

* install pre-release sdk

* wip: trying to debug accounts their were some bugs i was finding when creating a new account i also added the abbilty to create a debug account when importing by adding `#debug` to the end of the seed

* wip: cleanup from frankies changes

* wip: more cleanup and added no op program to filesystem

* wip: registering is working with default key proxy and adding verifying keys to list

* wip: silently handling error, logging an error message

* wip: debugging undefined error in subscriber

* wip: removing hardcoded debug from initialize entropy, already available on account data

* wip: new release for sdk

* wip: fixing account flows

* replace console.log with "print"

* wip

* wip

* got balances woorking

* fix register

* sdk upgrade: has key fix potentioally, also seems like their was some types that were not updated

* fix accounts?

* fix accounts

* temp: jhonny plz coppy console.log

* remove temp commit

* fix refference

* wip: fixed the selectAccount appearing in config, removed account selection from transfer, updated cli progress bar for transfer, updated user programs and imprived error handling

* wip: cleanup account selection

* wip: adding types to cli for account config

* wip: more error handling for register, added barebones program to filesystem

* wip: defaulting to debug mode for now

* wip: getting singing to work

---------

Co-authored-by: Nayyir Jutha <nayyir@entropy.xyz>
Co-authored-by: mix irving <mix@protozoa.nz>
Co-authored-by: Johnny <9611008+johnnymatthews@users.noreply.github.com>
Co-authored-by: frankie <frankie.diamond@gmail.com>
  • Loading branch information
5 people authored May 27, 2024
1 parent d50ba18 commit ea025de
Show file tree
Hide file tree
Showing 38 changed files with 4,343 additions and 5,382 deletions.
10 changes: 0 additions & 10 deletions .eslint.js

This file was deleted.

5 changes: 2 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ module.exports = {
root: true,
rules: {
indent: ['error', 2],
'space-before-function-paren': ['error', 'always'],
'space-before-function-paren': 2,
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/ban-ts-comment': 1,
'@typescript-eslint/no-explicit-any': 'off',

'@typescript-eslint/no-explicit-any': 'warn'
},
ignorePatterns: ['node_modules/', 'dist/', 'examples/', '.yarnrc.yml'],
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,9 @@ dist
# End of https://www.toptal.com/developers/gitignore/api/macos,node

*.log


# built files:
src/*/*/*.js
src/*/*.js
src/*.js
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
npx tsc && echo "✔ tsc..." && \
npx lint-staged
661 changes: 661 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

95 changes: 0 additions & 95 deletions index.ts

This file was deleted.

46 changes: 31 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
{
"name": "devnet_cli_tool",
"name": "@entropyxyz/cli",
"version": "1.0.0",
"description": "",
"scripts": {
"entropy": "yarn run start",
"test": "echo \"Error: no test specified\" && exit 1",
"lint:fix": "eslint . --ext .ts --fix",
"lint": "eslint . --ext .ts",
"tsc": "tsc",
"start": "ts-node index.ts"
"start": "ts-node src/index.ts",
"start:debug": "DEBUG=@entropyxyz/cli yarn start",
"entropy": "npm run start",
"test": "npm run type-check && npm run lint",
"lint": "eslint . --ext .ts --fix",
"type-check": "tsc --project tsconfig.json",
"prepare": "husky",
"postinstall": "husky",
"prepack": "pinst --disable",
"postpack": "pinst --enable",
"reload-local-sdk": "yarn unlink @entropyxyz/sdk && yarn link @entropyxyz/sdk"
},
"repository": {
"type": "git",
"url": "git+https://github.com/entropyxyz/devnet_cli_tool.git"
"url": "git+https://github.com/entropyxyz/cli.git"
},
"keywords": [],
"author": "Jesse Abramowitz",
"license": "ISC",
"license": "AGPL-3.0-only",
"bugs": {
"url": "https://github.com/entropyxyz/devnet_cli_tool/issues"
"url": "https://github.com/entropyxyz/cli/issues"
},
"homepage": "https://github.com/entropyxyz/devnet_cli_tool#readme",
"homepage": "https://github.com/entropyxyz/cli#readme",
"dependencies": {
"@entropyxyz/sdk": "^0.1.4",
"@entropyxyz/sdk": "^0.1.5-3",
"@ethereumjs/util": "^9.0.2",
"@polkadot/util-crypto": "^12.5.1",
"@types/node": "^20.12.12",
"alchemy-sdk": "^3.1.2",
"axios": "^0.27.2",
"ansi-colors": "^4.1.3",
"cli-progress": "^3.12.0",
"debug": "^4.3.4",
"dotenv": "^16.4.1",
"ethers": "^5.7.2",
"inquirer": "8.0.0",
"ts-node": "^10.9.1",
"ts-node": "^10.9.2",
"typescript": "^4.8.4",
"viem": "^2.7.8",
"x25519": "^0.1.0"
},
"devDependencies": {
"@swc/core": "^1.4.0",
"@swc/helpers": "^0.5.6",
"@types/cli-progress": "^3",
"@types/inquirer": "^9.0.2",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"eslint": "^8.56.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"pinst": "^3.0.0",
"regenerator-runtime": "^0.14.1"
},
"packageManager": "yarn@1.22.1",
"lint-staged": {
"*.ts": "eslint --fix"
}
}
130 changes: 97 additions & 33 deletions src/common/initializeEntropy.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,141 @@
import Entropy from "@entropyxyz/sdk"
import { getWallet } from '@entropyxyz/sdk/dist/keys'
import { EntropyAccount } from "@entropyxyz/sdk"
import { decrypt } from "../flows/password"
import Entropy, { wasmGlobalsReady } from "@entropyxyz/sdk"
// TODO: fix importing of types from @entropy/sdk/keys
// @ts-ignore
import Keyring from "@entropyxyz/sdk/keys"
import inquirer from "inquirer"
import { decrypt, encrypt } from "../flows/password"
import { debug } from "../common/utils"
import * as config from "../config"

export const initializeEntropy = async ({data}, endpoint: string): Promise<Entropy> => {
if (Object.keys(data).length === 0) {
const entropy = new Entropy({ endpoint })
await entropy.ready
return entropy
}
// TODO: unused
// let defaultAccount // have a main account to use
// let entropys

// a cache of keyrings
const keyrings = {
default: undefined // this is the "selected account" keyring
}

export function getKeyring (address) {
if (!address && keyrings.default) return keyrings.default
if (address && keyrings[address]) return keyrings[address]
return keyrings.default
}


// export function setupKeyrings (config) {
// const { accounts } = config;
//
// }

export const initializeEntropy = async ({ keyMaterial }, endpoint: string): Promise<Entropy> => {

// if (defaultAccount && defaultAccount.seed === keyMaterial.seed) return entropys[defaultAccount.registering.address]
await wasmGlobalsReady()
let password

let accountData
if (data && typeof data === 'object' && 'type' in data && 'seed' in data) {
accountData = data
} else if (typeof data === 'string') {
if (keyMaterial && typeof keyMaterial === 'object' && 'seed' in keyMaterial) {
accountData = keyMaterial
} else if (typeof keyMaterial === 'string') {

let decryptedData
let attempts = 0

// TO-DO: this should be a generator function not a while loop
while (attempts < 3) {
const answers = await inquirer.prompt([
{
type: 'password',
name: 'password',
message: 'Enter password to decrypt data:',
message: 'Enter password to decrypt keyMaterial:',
mask: '*',
}
])

try {
decryptedData = decrypt(data, answers.password)
decryptedData = decrypt(keyMaterial, answers.password)
//@ts-ignore
if (!decryptedData || typeof decryptedData !== 'object' || !('seed' in decryptedData)) {
throw new Error("Failed to decrypt data or decrypted data is invalid")
throw new Error("Failed to decrypt keyMaterial or decrypted keyMaterial is invalid")
}

password = answers.password
break
} catch (error) {
console.error("Incorrect password. Try again")
attempts++
if (attempts >= 3) {
throw new Error("Failed to decrypt data after 3 attempts.")
throw new Error("Failed to decrypt keyMaterial after 3 attempts.")
}
}
}

accountData = decryptedData as { seed: string; type: string }
accountData = decryptedData
} else {
throw new Error("Data format is not recognized as either encrypted or unencrypted")
}
console.log('accountData', accountData);

if (!accountData.seed) {
const entropy = new Entropy({ endpoint })
await entropy.ready
return entropy
if (!accountData.seed || !accountData.admin) {
throw new Error("Data format is not recognized as either encrypted or unencrypted")
}

if (accountData && accountData.admin && !accountData.registration) {
accountData.registration = accountData.admin
accountData.registration.used = true
const store = await config.get()
store.accounts.map((account) => {
if (account.address === accountData.admin.address) {
let data = accountData
if (typeof account.data === 'string' ) data = encrypt(accountData, password)
account = {
...account,
data,
}
}
return account
})
// re save the entire config
await config.set(store)
}

const signer = await getWallet(accountData.seed)
let selectedAccount
const storedKeyring = getKeyring(accountData.admin.address)
if(!storedKeyring) {
const keyring = new Keyring({ ...accountData, debug: true })
keyring.accounts.on('#account-update', async (newAccoutData) => {
const store = await config.get()
store.accounts.map((account) => {
if (account.address === selectedAccount.address) {
let data = newAccoutData
if (typeof account.data === 'string' ) data = encrypt(newAccoutData, password)
account = {
...account,
data,
}
}
return account
})
// re save the entire config
await config.set(store)

})
keyrings.default = keyring
debug(keyring)

const entropyAccount: EntropyAccount = {
sigRequestKey: signer,
programModKey: signer,
programDeployKey: signer,
// TO-DO: fix in sdk: admin should be on kering.accounts by default
// /*WANT*/ keyrings[keyring.admin.address] = keyring
keyrings[keyring.getAccount().admin.address] = keyring
selectedAccount = keyring
} else {
keyrings.default = storedKeyring
selectedAccount = storedKeyring
}

const entropy = new Entropy({ account: entropyAccount, endpoint})
const entropy = new Entropy({ keyring: selectedAccount, endpoint })
await entropy.ready

if (!entropy.account?.sigRequestKey?.pair) {
if (!entropy?.keyring?.accounts?.registration?.seed) {
throw new Error("Keys are undefined")
}

return entropy
}
}
Loading

0 comments on commit ea025de

Please sign in to comment.