Skip to content

Commit

Permalink
feat : add secureAccount config and modify existing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulazeem-tk4vr committed Jan 13, 2025
1 parent 7a0545c commit 5936793
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
37 changes: 37 additions & 0 deletions genesis-secure-accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"Name": "Foundation",
"SourceFundsAddress": "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"RecipientFundsAddress": "0xf08b4a82ef3ad9c919852a3d32eb5088af9a9153",
"SecureAccountAddress": "22390e555baa11ea1edfaf80afe7c6d7896866dd000000000000000000000000",
"SourceFundsBalance": "55880000000000000000000000"
},
{
"Name": "Team",
"SourceFundsAddress": "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"RecipientFundsAddress": "0xde368dce1070dba428b8133265666261ddca9f1a",
"SecureAccountAddress": "f2129aaf113129fbbf22b8513a95ed9f16d09977000000000000000000000000",
"SourceFundsBalance": "71120000000000000000000000"
},
{
"Name": "Ecosystem",
"SourceFundsAddress": "0xcccccccccccccccccccccccccccccccccccccccc",
"RecipientFundsAddress": "0x2cf747f34a61bd23a7cff6974e56012fbc193b7e",
"SecureAccountAddress": "2d54f7ff2f268e7702ea72d91db16b3c15fb1397000000000000000000000000",
"SourceFundsBalance": "25400000000000000000000000"
},
{
"Name": "Sale",
"SourceFundsAddress": "0xdddddddddddddddddddddddddddddddddddddddd",
"RecipientFundsAddress": "0x051b7b30e828ea8c371019e94aea28cdbef47ff8",
"SecureAccountAddress": "6fd9d634db6b19bb25067f84d20cc8c00e277f4d000000000000000000000000",
"SourceFundsBalance": "91440000000000000000000000"
},
{
"Name": "ShardusLicense",
"SourceFundsAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"RecipientFundsAddress": "0x1be3e07e7c4c2e568c590f71234e0c03f2348734",
"SecureAccountAddress": "5916018611b4958ab9dc963df642d0b6bcff15a9000000000000000000000000",
"SourceFundsBalance": "5080000000000000000000000"
}
]
53 changes: 21 additions & 32 deletions src/Data/ExtractShardKey.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { config } from '../Config'
import { AccountType } from '../shardeum/calculateAccountHash'
import { InternalTXType } from '../shardeum/verifyGlobalTxReceipt'
import * as NodeList from '../NodeList'
import { getJson } from '../P2P'
import * as Utils from '../Utils'
import { Address, toBytes } from '@ethereumjs/util'
import { Transaction, TransactionFactory, TransactionType, TypedTransaction } from '@ethereumjs/tx'
import { getSenderAddress } from '@shardus/net'
import { Utils as StringUtils } from '@shardus/types'
import * as fs from 'fs'
import { resolve } from 'path'

const genesisSecureAccounts = StringUtils.safeJsonParse(
fs.readFileSync(resolve(__dirname, '../../genesis-secure-accounts.json'), 'utf8')
)

interface SecureAccountData {
Name: string
SourceFundsAddress: string
RecipientFundsAddress: string
SecureAccountAddress: string
}

type GetTxSenderAddressResult = { address: Address; isValid: boolean; gasValid: boolean }

Expand All @@ -15,38 +26,16 @@ let simpleTTL = 0
const cacheMaxSize = 20000

export async function crackKeyFromSecureAccount(tx: any): Promise<string> {
// Define the query function for robustQuery
const queryFn = async (node: NodeList.ConsensusNodeInfo): Promise<any[]> => {
const response = (await getJson(`http://${node.ip}:${node.port}/secure_accounts`)) as any

if (response.accounts) {
return response.accounts
}

throw new Error(`Secure account data not found on node ${node.ip}`)
}

// Use robustQuery to fetch secure accounts
const activeNodes = NodeList.getActiveList()
const result = await Utils.robustQuery(activeNodes, queryFn)

if (!result.value || !Array.isArray(result.value)) {
throw new Error(`Unable to retrieve secure accounts`)
}

// Find the matching secure account from the result
const matchingAccount = result.value.find(
(account: any) => account.secureAccountConfig.Name === tx.accountName
const secureAccountDataMap: Map<string, SecureAccountData> = new Map(
genesisSecureAccounts.map((account) => [account.Name, account])
)

if (!matchingAccount) {
if (!secureAccountDataMap.has(tx.accountName)) {
throw new Error(`Secure account ${tx.accountName} not found`)
}

const secureAccountConfig = matchingAccount.secureAccountConfig

// Return the SourceFundsAddress formatted as a Shardus address
return toShardusAddress(secureAccountConfig.SourceFundsAddress, AccountType.Account)
return toShardusAddress(secureAccountDataMap.get(tx.accountName).SourceFundsAddress, AccountType.Account)
}

export function toShardusAddress(addressStr: string, accountType: AccountType): string {
Expand Down Expand Up @@ -213,14 +202,14 @@ export async function extractKeyFromTx(receiptTx: any): Promise<string> {

if (isInternalTx(tx)) {
let key = await extractKeyFromInternalTx(tx)
if(config.VERBOSE) console.log('The generated executionShardkey is', key)
if (config.VERBOSE) console.log('The generated executionShardkey is', key)
return key
}

if (isDebugTx(tx)) {
const debugTx = tx
const transformedSourceKey = toShardusAddress(debugTx.from, AccountType.Debug)
if(config.VERBOSE) console.log('The generated executionShardkey is', transformedSourceKey)
if (config.VERBOSE) console.log('The generated executionShardkey is', transformedSourceKey)
return transformedSourceKey
}

Expand All @@ -229,7 +218,7 @@ export async function extractKeyFromTx(receiptTx: any): Promise<string> {
const senderAddress = getTxSenderAddress(transaction, txId).address
const txSenderEvmAddr = senderAddress.toString()
const transformedSourceKey = toShardusAddress(txSenderEvmAddr, AccountType.Account)
if(config.VERBOSE) console.log('The generated executionShardkey is', transformedSourceKey)
if (config.VERBOSE) console.log('The generated executionShardkey is', transformedSourceKey)
return transformedSourceKey // executionShardKey
}

Expand Down

0 comments on commit 5936793

Please sign in to comment.