Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixmix/debug #45

Merged
merged 13 commits into from
May 21, 2024
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"alchemy-sdk": "^3.1.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",
Expand Down
12 changes: 6 additions & 6 deletions src/common/initializeEntropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const keyrings = {
}
let entropys

export function getKeyring(address) {
export function getKeyring (address) {
if (!address && keyrings.default) return keyrings.default
if (address && keyrings[address]) return keyrings[address]
if (address && !keyrings[address]) throw new Error('No keyring for this account')
Expand All @@ -21,7 +21,7 @@ export function getKeyring(address) {
}


export const initializeEntropy = async ({keyMaterial}, endpoint: string): Promise<Entropy> => {
export const initializeEntropy = async ({ keyMaterial }, endpoint: string): Promise<Entropy> => {
if (defaultAccount && defaultAccount.seed === keyMaterial.seed) return entropys[defaultAccount.registering.address]
await wasmGlobalsReady()

Expand Down Expand Up @@ -70,18 +70,18 @@ export const initializeEntropy = async ({keyMaterial}, endpoint: string): Promis
}

console.log('account keyMaterial', accountData);
let selcted
let selected
if(!keyrings.default) {
const keyring = new Keyring({ ...accountData, debug: true })
keyrings.default = keyring
selcted = keyring
selected = keyring
} else {
const keyring = new Keyring({ ...accountData, debug: true })
keyrings[keyring.registering.address] = keyring
mixmix marked this conversation as resolved.
Show resolved Hide resolved
selcted = keyring
selected = keyring
}

const entropy = new Entropy({ selcted, endpoint})
const entropy = new Entropy({ keyring: selected, endpoint })

await entropy.ready

Expand Down
8 changes: 7 additions & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { decodeAddress, encodeAddress } from "@polkadot/keyring"
import { hexToU8a, isHex } from "@polkadot/util"
import { keccak256 } from "ethereum-cryptography/keccak"
import { Buffer } from 'buffer'
import Debug from 'debug'

const _debug = Debug('@entropyxyz/cli')
export function debug (...args) {
_debug(...args.map(arg => JSON.stringify(arg, null, 2)))
}

// hardcoding for now instead of querying chain
const DECIMALS = 10
Expand All @@ -16,7 +22,7 @@ export function pubToAddress (publicKey: string): string {
const publicKeyBuffer = Buffer.from(publicKey, 'hex')
const hash = keccak256(publicKeyBuffer)
const address = `0x${Buffer.from(hash.subarray(hash.length - 20)).toString('hex')}`
console.log({address})
debug('address:', address)
return address
}

Expand Down
48 changes: 18 additions & 30 deletions src/flows/DeployPrograms/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Entropy from "@entropyxyz/sdk"
import * as util from "@polkadot/util"
import inquirer from "inquirer"
import { initializeEntropy } from "../../common/initializeEntropy"
import { accountChoices } from "../../common/utils"
import { debug, accountChoices } from "../../common/utils"
import { readFileSync } from "fs"
import * as util from "@polkadot/util"

export async function devPrograms ({ accounts, endpoints }, options) {
const endpoint = endpoints[options.ENDPOINT]
Expand All @@ -15,37 +16,33 @@ export async function devPrograms ({ accounts, endpoints }, options) {

const answers = await inquirer.prompt([accountQuestion])
const selectedAccount = answers.selectedAccount
console.log('selectedAccount:', {selectedAccount})
debug('selectedAccount:', {selectedAccount})

const choices = {
"Deploy": deployProgram,
"Get Program Pointers": getProgramPointers,
"Exit": () => console.log("Exiting")
}

const actionChoice = await inquirer.prompt ([
const actionChoice = await inquirer.prompt([
{
type: "list",
name: "action",
message: "Select your action:",
choices: ["Deploy", "Get Program Pointers", "Exit"],
choices: Object.keys(choices)
},
])

const entropy = await initializeEntropy(
{ data: selectedAccount.data },
{ keyMaterial: selectedAccount.data },
endpoint
)

switch (actionChoice.action) {
case "Deploy":
await deployProgram(entropy, selectedAccount)
break
case "Get Program Pointers":
await getProgramPointers(entropy, selectedAccount)
break
case "Exit":
console.log("Exiting.")
break
}
const flow = choices[actionChoice.action]
await flow(entropy, selectedAccount)
mixmix marked this conversation as resolved.
Show resolved Hide resolved
}

async function deployProgram (entropy, account) {
async function deployProgram (entropy: Entropy, account: any) {
const deployQuestions = [
{
type: "input",
Expand Down Expand Up @@ -91,12 +88,12 @@ async function deployProgram (entropy, account) {
console.error("Deployment failed:", deployError)
}

console.log(`Deploying from account: ${account.address}`)
console.log("Deploying from account:", account.address)
}

async function getProgramPointers (entropy, account) {
async function getProgramPointers (entropy: Entropy, account: any) {
const userAddress = account.address
console.log(userAddress)
debug('Account address:',userAddress)
if (!userAddress) return

try {
Expand All @@ -106,12 +103,3 @@ async function getProgramPointers (entropy, account) {
console.error("Failed to retrieve program pointers:", error)
}
}

// function accountChoices (accounts) {
// return accounts
// .map((account) => ({
// name: `${account.name} (${account.address})`,
// value: account,
// }))
// .concat([{ name: "Other", value: null }])
// }
2 changes: 1 addition & 1 deletion src/flows/UserPrograms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function userPrograms ({ accounts, endpoints }, options) {
])

const entropy = await initializeEntropy(
{ data: selectedAccount.data },
{ keyMaterial: selectedAccount.data },
endpoint
)

Expand Down
36 changes: 18 additions & 18 deletions src/flows/balance/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import inquirer from "inquirer"
import { accountChoices, isEmpty } from "../../common/utils"
import { accountChoices, debug, isEmpty } from "../../common/utils"
import { initializeEntropy } from "../../common/initializeEntropy"

const hexToBigInt = (hexString: string) => BigInt(hexString)


export async function checkBalance ({ accounts, endpoints }, options) {
const endpoint = endpoints[options.ENDPOINT]
const accountQuestion = {
type: "list",
name: "selectedAccount",
message: "Choose account:",
choices: accountChoices(accounts)
}

const otherQuestion = {
type: "input",
name: "accountSeedOrPrivateKey",
message: "Enter the account seed or private key:",
when: (answers) => !answers.selectedAccount
}
const answers = await inquirer.prompt([
{
name: "selectedAccount",
type: "list",
message: "Choose account:",
choices: accountChoices(accounts)
},
{
name: "accountSeedOrPrivateKey",
type: "input",
message: "Enter the account seed or private key:",
when: (answers) => !answers.selectedAccount
}
])
mixmix marked this conversation as resolved.
Show resolved Hide resolved

const answers = await inquirer.prompt([accountQuestion, otherQuestion])
const selectedAccount = answers.selectedAccount
const accountSeedOrPrivateKey = answers.accountSeedOrPrivateKey
console.log('selectedAccount:', selectedAccount)
const { selectedAccount, accountSeedOrPrivateKey } = answers
debug('selectedAccount:', selectedAccount)
if (!selectedAccount && !accountSeedOrPrivateKey) {
console.log('whoops')
return
} else {
console.log('before entropy creation', endpoint)
debug('before entropy creation', endpoint)

let keyMaterial = selectedAccount?.data;
if (!keyMaterial || isEmpty(keyMaterial)) {
Expand Down
6 changes: 3 additions & 3 deletions src/flows/entropyFaucet/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inquirer from "inquirer"
import { accountChoices } from "../../common/utils"
import { debug, accountChoices } from "../../common/utils"
import { initializeEntropy } from "../../common/initializeEntropy"

export async function entropyFaucet ({ accounts, endpoints }, options) {
Expand All @@ -14,7 +14,7 @@ export async function entropyFaucet ({ accounts, endpoints }, options) {

const answers = await inquirer.prompt([accountQuestion])
const selectedAccount = answers.selectedAccount
console.log({selectedAccount})
debug('selectedAccount', selectedAccount)

const recipientAddress = selectedAccount.address
const aliceData = {
Expand All @@ -24,7 +24,7 @@ export async function entropyFaucet ({ accounts, endpoints }, options) {
},
}

const entropy = await initializeEntropy(aliceData, endpoint)
const entropy = await initializeEntropy({ keyMaterial: aliceData }, endpoint)

if (!entropy.registrationManager.signer.pair) {
throw new Error("Keys are undefined")
Expand Down
Loading