Skip to content

Commit

Permalink
failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
jawndiego committed Mar 15, 2024
1 parent a006c2f commit ac16988
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 23 deletions.
71 changes: 53 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { Adapter } from './signing/adapters/types'
import { isValidPair } from './keys'
import { Signer, Address } from './types'
import ProgramManager from './programs'
import { runDkgProtocol, ValidatorInfo } from '@entropyxyz/entropy-protocol-nodejs'
import { fromHex } from '@entropyxyz/entropy-protocol-nodejs'
import { runDkgProtocol, ValidatorInfo, fromHex } from '@entropyxyz/entropy-protocol-nodejs'
export interface EntropyAccount {
sigRequestKey?: Signer
programModKey?: Signer | string
Expand Down Expand Up @@ -41,7 +40,7 @@ export interface EntropyOpts {
* programModKey: signer,
* }
*
* const entropy = new Entropy({ account: entropyAccount });
* const entropy = new Entropy({ account: entropyAccount })
* await entropy.ready
*
* await entropy.register({
Expand Down Expand Up @@ -235,7 +234,9 @@ export default class Entropy {
}

async participateInDkgForPrivateVisibility (address: string): Promise<void> {
console.log("PRIVATE REGISTER")
const blockNumber = await this.substrate.rpc.chain.getHeader().then((header) => header.number.toNumber())
console.log("block", blockNumber)
const validatorsInfo = await this.getDKGCommittee(blockNumber + 1)
if (validatorsInfo.length === 0) {
throw new Error('No validators info available for DKG.')
Expand All @@ -253,56 +254,70 @@ export default class Entropy {
}

async getDKGCommittee (blockNumber: number): Promise<ValidatorInfo[]> {
console.log("GET DKG COMMITTEE")
const validatorsInfo: ValidatorInfo[] = []
const SIGNING_PARTY_SIZE = 2

for (let i = 0; i < SIGNING_PARTY_SIZE; i++) {
try {
const accountId = await this.selectValidatorFromSubgroup(i, blockNumber)
console.log({accountId})
const serverInfoOption = await this.substrate.query.stakingExtension.thresholdServers(accountId)

if (serverInfoOption.isNone) {

Check failure on line 267 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Property 'isNone' does not exist on type 'Codec'.
throw new Error("Stash Fetch Error")
}

console.log("pre server info")

const serverInfo = serverInfoOption.unwrap()

Check failure on line 273 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Property 'unwrap' does not exist on type 'Codec'.
validatorsInfo.push(new ValidatorInfo(fromHex(serverInfo.x25519PublicKey.toString()), serverInfo.endpoint.toString(), fromHex(serverInfo.tssAccount.toString())))
console.log("PubKey", serverInfo.x25519PublicKey.toString())
console.log("endpoint", serverInfo.endpoint.toString())
console.log("tss", serverInfo.tssAccount.toString())
validatorsInfo.push(new ValidatorInfo(fromHex(serverInfo.x25519PublicKey.toString()), serverInfo.endpoint.toString(), serverInfo.tssAccount))
} catch (error) {
console.error(`Error fetching validator info: ${error}`)
throw error
}
}
console.log("ABOUT TO CONSOLE VALIDATOR INFO")

return validatorsInfo
}
async selectValidatorFromSubgroup (signingGroup: number, blockNumber: number): Promise<string> {
try {
const subgroupInfo = await this.substrate.query.stakingExtension.signingGroups(signingGroup)
if (subgroupInfo.isNone || subgroupInfo.unwrap().isEmpty) {

Check failure on line 290 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Property 'isNone' does not exist on type 'Codec'.

Check failure on line 290 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Property 'unwrap' does not exist on type 'Codec'.
throw new Error("Subgroup Fetch Error")
throw new Error("Subgroup information not available or empty.")
}

const subgroupAddresses = subgroupInfo.unwrap()

for (let attempt = 0; attempt < subgroupAddresses.length; attempt++) {
const subgroupAddresses = subgroupInfo.unwrap().toArray()

Check failure on line 293 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Property 'unwrap' does not exist on type 'Codec'.

while (subgroupAddresses.length > 0) {
const selectionIndex = blockNumber % subgroupAddresses.length
const address = subgroupAddresses[selectionIndex]

const isSynced = await this.substrate.query.stakingExtension.isValidatorSynced(address)
if (isSynced) {
return address.toString()
} else {

try {
const isSynced = await this.substrate.query.stakingExtension.isValidatorSynced(address)

if (isSynced) {
console.log(`Address ${address.toString()} is synced.`)
return address.toString()
} else {
subgroupAddresses.splice(selectionIndex, 1)
blockNumber++
console.log(`Address ${address.toString()} is not synced. Trying next validator.`)
}
} catch (error) {
console.error(`Error checking sync status for address ${address.toString()}:`, error)
subgroupAddresses.splice(selectionIndex, 1)
}
}

throw new Error("No synced validators found in the subgroup.")
} catch (error) {
console.error(`Error selecting validator from subgroup: ${error}`)
console.error("Failed to select a validator:", error)
throw error
}
}

/**
* Retrieves the verifying key associated with the given address's registration record.
*
Expand All @@ -318,6 +333,13 @@ export default class Entropy {
return registeredInfo.toHuman().verifyingKey
}

async getKeyVisibility ( address: Address): Promise<string> {
const registeredInfo = await this.substrate.query.relayer.registered(address)

// @ts-ignore: next line

Check warning on line 339 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Do not use "@ts-ignore" because it alters compilation errors
return registeredInfo.toHuman().keyVisibility
}

/**
* Signs a given transaction based on the provided parameters.
*
Expand All @@ -343,9 +365,20 @@ export default class Entropy {
throw new Error(
'Initialized in read only state: can not use write functions'
)
return this.signingManager.signTransaction(params)

const keyVisibilityInfo = await this.getKeyVisibility(this.account.sigRequestKey.wallet.address)

if (keyVisibilityInfo === 'Private') {
return this.signingManager.privateSign(params)
} else {
return this.signingManager.signTransaction(params)
}
}





/**
* Signs a signature request hash. This method involves various steps including validator
* selection, transaction request formatting, and submission of these requests to validators
Expand All @@ -365,6 +398,8 @@ export default class Entropy {
throw new Error(
'Initialized in read only state: can not use write functions'
)


return this.signingManager.sign(params)
}
}
4 changes: 3 additions & 1 deletion src/registration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class RegistrationManager extends ExtrinsicBaseClass {
async register ({
freeTx = false,
initialPrograms = [],
keyVisibility = 'Permissioned',
keyVisibility,
programModAccount,
}: RegistrationParams): Promise<RegisteredInfo> {
const programModificationAccount = programModAccount
Expand Down Expand Up @@ -134,6 +134,8 @@ export default class RegistrationManager extends ExtrinsicBaseClass {
return registered
}



/**
* Verifies the registration status of an address.
*
Expand Down
39 changes: 37 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { decodeAddress, encodeAddress } from '@polkadot/keyring'
import { hexToU8a, isHex } from '@polkadot/util'
import { Address } from '../types'
import { ValidatorInfo } from '../types'
import { ApiPromise } from '@polkadot/api'

export interface AnyObject {
[key: string]: number | string | string[] | AnyObject
Expand All @@ -17,7 +19,6 @@ export function typeofthing (thing) {
}
}


export function stripHexPrefix (str: string): string {
if (str.startsWith('0x')) return str.slice(2)
return str
Expand Down Expand Up @@ -88,4 +89,38 @@ export function hex2buf (hex: string): ArrayBuffer {
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
}
return bytes.buffer
}
}

export async function pickValidatorsBySignature (
substrate: ApiPromise,
sigRequest: string
): Promise<ValidatorInfo[]> {
const entries = await substrate.query.stakingExtension.signingGroups.entries()
const stashKeys = entries.map((group) => {
const keyGroup = group[1]
const index = parseInt(sigRequest, 16) % keyGroup.unwrap().length

Check failure on line 101 in src/utils/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Property 'unwrap' does not exist on type 'Codec'.
return keyGroup.unwrap()[index]

Check failure on line 102 in src/utils/index.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

Property 'unwrap' does not exist on type 'Codec'.
})

const rawValidatorInfo = await Promise.all(
stashKeys.map((stashKey) =>
substrate.query.stakingExtension.thresholdServers(stashKey)
)
)

const validatorsInfo: ValidatorInfo[] = rawValidatorInfo.map((validator) => {
const validatorHuman = validator.toHuman() as {
x25519PublicKey: string
endpoint: string
tssAccount: string
}

return {
x25519_public_key: validatorHuman.x25519PublicKey,
ip_address: validatorHuman.endpoint,
tss_account: validatorHuman.tssAccount,
}
})

return validatorsInfo
}
6 changes: 4 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const programConfig = util.u8aToHex(new Uint8Array(byteArray))


await entropy.register({
keyVisibility: 'Permissioned',
keyVisibility: 'Private',
freeTx: false,
// initialPrograms: [{ pointer: programData.pointer, config: programData.config }],
initialPrograms: [programData],
Expand Down Expand Up @@ -238,7 +238,9 @@ const programConfig = util.u8aToHex(new Uint8Array(byteArray))
data: '0x' + Buffer.from('Created On Entropy').toString('hex'),
}

const signature = await entropy.signTransaction({txParams: basicTx, type: 'eth' }) as string
// const signature = await entropy.signTransaction({txParams: basicTx, type: 'eth' }) as string
const signature = await entropy.signingManager.privateSign({txParams: basicTx, type: 'eth' }) as string



// encoding signature
Expand Down

0 comments on commit ac16988

Please sign in to comment.