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 @@ -34,6 +34,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
49 changes: 24 additions & 25 deletions src/common/initializeEntropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import Entropy, { wasmGlobalsReady } from "@entropyxyz/sdk"
// TODO: fix importing of types from @entropy/sdk/keys
// @ts-ignore
import Keyring from "@entropyxyz/sdk/keys"
import { decrypt } from "../flows/password"
import inquirer from "inquirer"
// have a main account to use
let defaultAccount
import { decrypt } from "../flows/password"
import { debug } from "../common/utils"

// TODO: unused
// let defaultAccount // have a main account to use
// let entropys

// have a main keyring
const keyrings = {
default: undefined
}
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,13 +24,13 @@ export function getKeyring(address) {
}


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

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

export const initializeEntropy = async (keyMaterial, endpoint: string): Promise<Entropy> => {
console.log('KEY MATERIAL', keyMaterial);
debug('key material', keyMaterial);

// if (defaultAccount && defaultAccount.seed === keyMaterial.seed) return entropys[defaultAccount.registering.address]
await wasmGlobalsReady()
Expand Down Expand Up @@ -76,29 +79,25 @@ export const initializeEntropy = async (keyMaterial, endpoint: string): Promise<
throw new Error("Data format is not recognized as either encrypted or unencrypted")
}

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

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

const entropy = new Entropy({ keyring: selected, endpoint })
await entropy.ready
console.log('data sent', accountData);

console.log('storage', keyrings);

console.log('selected', selectedAccount);

console.log('keyring', entropy.keyring);


debug('data sent', accountData);
debug('storage', keyrings);
debug('selected', selected);
debug('keyring', entropy.keyring);

if (!entropy?.keyring?.accounts?.registration?.seed) {
throw new Error("Keys are undefined")
Expand Down
12 changes: 9 additions & 3 deletions 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 Expand Up @@ -60,6 +66,6 @@ export function accountChoices (accounts) {
}

export function accountChoicesWithOther (accounts) {
return accountChoices(accounts)
.concat([{ name: "Other", value: null }])
return accountChoices(accounts)
.concat([{ name: "Other", value: null }])
}
2 changes: 0 additions & 2 deletions src/config/migrations/00.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { selectAccount } from "../../flows/manage-accounts/select-account"

export const version = '0'

export function migrate (data= {}) {
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
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