Skip to content

Commit

Permalink
Merge pull request #119 from Juneo-io/chain-vm-hdpath
Browse files Browse the repository at this point in the history
Chain vm hdpath
  • Loading branch information
alekswaslet authored Jun 2, 2024
2 parents 8ef1694 + 690a7b6 commit 99bf696
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 95 deletions.
19 changes: 5 additions & 14 deletions src/chain/chain.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'

export enum VMAccountType {
Utxo = 'Utxo',
Nonce = 'Nonce',
}
import { type ChainVM, type MCNProvider } from '../juneo'

export interface Blockchain {
name: string
id: string
vmId: string
accountType: VMAccountType
vm: ChainVM
asset: TokenAsset
assetId: string
aliases: string[]
Expand All @@ -27,8 +21,7 @@ export interface Blockchain {
export abstract class AbstractBlockchain implements Blockchain {
name: string
id: string
vmId: string
accountType: VMAccountType
vm: ChainVM
asset: TokenAsset
assetId: string
aliases: string[]
Expand All @@ -37,16 +30,14 @@ export abstract class AbstractBlockchain implements Blockchain {
constructor (
name: string,
id: string,
vmId: string,
accountType: VMAccountType,
vm: ChainVM,
asset: TokenAsset,
aliases: string[] = [],
registeredAssets: TokenAsset[] = []
) {
this.name = name
this.id = id
this.vmId = vmId
this.accountType = accountType
this.vm = vm
this.asset = asset
this.assetId = asset.assetId
this.aliases = aliases
Expand Down
1 change: 1 addition & 0 deletions src/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './jevm'
export * from './jvm'
export * from './platform'
export * from './solidity'
export * from './vm'
15 changes: 9 additions & 6 deletions src/chain/jevm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { ethers } from 'ethers'
import { ChainError, fetchJNT, isContractAddress } from '../utils'
import { AssetId } from '../transaction'
import { type JEVMAPI } from '../api'
import { ERC20TokenHandler, ContractManager, type SolidityTokenHandler } from './solidity'
import { type TokenAsset, type JRC20Asset, type JEVMGasToken, TokenType, type WrappedAsset } from '../asset'
import { AbstractBlockchain, VMAccountType } from './chain'
import { type JEVMGasToken, type JRC20Asset, type TokenAsset, TokenType, type WrappedAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { AssetId } from '../transaction'
import { ChainError, fetchJNT, isContractAddress } from '../utils'
import { AbstractBlockchain } from './chain'
import { ContractManager, ERC20TokenHandler, type SolidityTokenHandler } from './solidity'
import { ChainVM, VMWalletType } from './vm'

export const JEVM_ID: string = 'orkbbNQVf27TiBe6GqN5dm8d8Lo3rutEov8DUWZaKNUjckwSk'
export const EVM_ID: string = 'mgj786NP7uDwBCcq6YwThhaN8FLyybkCa4zBWTQbNgmK6k9A6'

const HD_PATH = 60

export const NativeAssetBalanceContract: string = '0x0100000000000000000000000000000000000001'
export const NativeAssetCallContract: string = '0x0100000000000000000000000000000000000002'

Expand Down Expand Up @@ -37,7 +40,7 @@ export class JEVMBlockchain extends AbstractBlockchain {
jrc20Assets: JRC20Asset[] = [],
wrappedAsset?: WrappedAsset | undefined
) {
super(name, id, JEVM_ID, VMAccountType.Nonce, asset, aliases, registeredAssets)
super(name, id, new ChainVM(JEVM_ID, VMWalletType.Nonce, HD_PATH), asset, aliases, registeredAssets)
this.asset = asset
this.chainId = chainId
this.baseFee = baseFee
Expand Down
11 changes: 7 additions & 4 deletions src/chain/jvm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { fetchJNT, validateBech32 } from '../utils'
import { type TokenAsset, type JNTAsset } from '../asset'
import { AbstractBlockchain, VMAccountType } from './chain'
import { type JNTAsset, type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { fetchJNT, validateBech32 } from '../utils'
import { AbstractBlockchain } from './chain'
import { ChainVM, VMWalletType } from './vm'

export const JVM_ID: string = 'otSmSxFRBqdRX7kestRW732n3WS2MrLAoWwHZxHnmMGMuLYX8'

const HD_PATH = 9000

export class JVMBlockchain extends AbstractBlockchain {
constructor (name: string, id: string, asset: JNTAsset, aliases?: string[], registeredAssets: TokenAsset[] = []) {
super(name, id, JVM_ID, VMAccountType.Utxo, asset, aliases, registeredAssets)
super(name, id, new ChainVM(JVM_ID, VMWalletType.Utxo, HD_PATH), asset, aliases, registeredAssets)
}

protected async fetchAsset (provider: MCNProvider, assetId: string): Promise<TokenAsset> {
Expand Down
7 changes: 5 additions & 2 deletions src/chain/platform.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { type JNTAsset, type TokenAsset } from '../asset'
import { type MCNProvider } from '../juneo'
import { fetchJNT, RewardCalculator, TimeUtils, validateBech32 } from '../utils'
import { AbstractBlockchain, VMAccountType } from './chain'
import { AbstractBlockchain } from './chain'
import { ChainVM, VMWalletType } from './vm'

export const PLATFORMVM_ID: string = '11111111111111111111111111111111LpoYY'

const HD_PATH = 9000

const BaseShare: number = 100_0000 // 100%

export class PlatformBlockchain extends AbstractBlockchain {
Expand All @@ -21,7 +24,7 @@ export class PlatformBlockchain extends AbstractBlockchain {
aliases?: string[],
registeredAssets: TokenAsset[] = []
) {
super(name, id, PLATFORMVM_ID, VMAccountType.Utxo, asset, aliases, registeredAssets)
super(name, id, new ChainVM(PLATFORMVM_ID, VMWalletType.Utxo, HD_PATH), asset, aliases, registeredAssets)
this.stakeConfig = stakeConfig
this.rewardConfig = rewardConfig
this.rewardCalculator = new RewardCalculator(rewardConfig)
Expand Down
16 changes: 16 additions & 0 deletions src/chain/vm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum VMWalletType {
Utxo = 'Utxo',
Nonce = 'Nonce',
}

export class ChainVM {
id: string
walletType: VMWalletType
hdPath: number

constructor (id: string, walletType: VMWalletType, hdPath: number) {
this.id = id
this.walletType = walletType
this.hdPath = hdPath
}
}
2 changes: 1 addition & 1 deletion src/juneo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class MCNProvider {
this.juneApi = new JEVMAPI(client, this.juneChain)
for (const supernet of this.mcn.supernets) {
for (const chain of supernet.chains) {
if (chain.vmId === JEVM_ID) {
if (chain.vm.id === JEVM_ID) {
this.jevmApi[chain.id] = new JEVMAPI(client, chain as JEVMBlockchain)
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/transaction/platform/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { TransferableOutput } from '../output'
import { type Signable } from '../signature'
import { AbstractBaseTransaction, AbstractExportTransaction, AbstractImportTransaction } from '../transaction'
import {
BlockchainIdSize,
type Address,
type AssetId,
AssetIdSize,
BlockchainId,
type SupernetId,
SupernetIdSize,
BlockchainIdSize,
type DynamicId,
DynamicIdSize,
type AssetId,
type Address,
AssetIdSize,
type NodeId,
NodeIdSize
NodeIdSize,
type SupernetId,
SupernetIdSize
} from '../types'
import { SupernetAuth, type BLSSigner } from './supernet'
import { Validator, Secp256k1OutputOwners } from './validation'
import { type BLSSigner, SupernetAuth } from './supernet'
import { Secp256k1OutputOwners, Validator } from './validation'

const AddValidatorTransactionTypeId: number = 0x0000000c
const AddSupernetValidatorTransactionType: number = 0x0000000d
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function calculateBalances (utxoSet: Utxo[], balances: Map<string, Balanc
}

export function getUtxoAPI (provider: MCNProvider, chain: Blockchain): AbstractUtxoAPI {
const vmId: string = chain.vmId
const vmId: string = chain.vm.id
if (vmId === JVM_ID) {
return provider.jvmApi
} else if (vmId === PLATFORMVM_ID) {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type Blockchain, JVM_ID, PLATFORMVM_ID, JEVM_ID } from '../chain'
import { type Blockchain, JEVM_ID, JVM_ID, PLATFORMVM_ID } from '../chain'
import { UserInput } from '../transaction'
import { type Spending, BaseSpending, type ExecutableOperation, type TransactionType, type MCNAccount } from '../wallet'
import { sha256, rmd160 } from './crypto'
import { isHex, hasHexPrefix, decodeCB58, isBase58, encodeBech32 } from './encoding'
import { BaseSpending, type ExecutableOperation, type MCNAccount, type Spending, type TransactionType } from '../wallet'
import { rmd160, sha256 } from './crypto'
import { decodeCB58, encodeBech32, hasHexPrefix, isBase58, isHex } from './encoding'

export const JVMPrivateKeyPrefix = 'PrivateKey-'
const PrivateKeyLength: number = 64
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function trackJuneoTransaction (
transactionType: TransactionType
): Promise<boolean> {
let success: boolean = false
const vmId: string = chain.vmId
const vmId: string = chain.vm.id
if (vmId === JVM_ID) {
success = await executable.trackJVMTransaction(transactionId, transactionType)
} else if (vmId === PLATFORMVM_ID) {
Expand Down
46 changes: 23 additions & 23 deletions src/wallet/transaction/cross/cross.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ export class CrossManager {
utxosCount?: number,
assetsCount?: number
): Promise<BaseFeeData> {
if (destination.vmId === JVM_ID) {
if (destination.vm.id === JVM_ID) {
return await estimateJVMImportTransaction(provider)
} else if (destination.vmId === PLATFORMVM_ID) {
} else if (destination.vm.id === PLATFORMVM_ID) {
return await estimatePlatformImportTransaction(provider)
} else if (destination.vmId === JEVM_ID) {
} else if (destination.vm.id === JEVM_ID) {
const api: JEVMAPI = provider.jevmApi[destination.id]
const exportedAssetsCount: number = assetId === api.chain.assetId ? 1 : 2
const inputsCount: number = typeof utxosCount === 'number' ? utxosCount : exportedAssetsCount
// default outputsCount should use 1 instead of exportedAssetsCount but currently needed for importing june for jrc20 deposits
const outputsCount: number = typeof assetsCount === 'number' ? assetsCount : exportedAssetsCount // 1
return await estimateEVMImportTransaction(api, inputsCount, outputsCount)
}
throw new CrossError(`destination vm id does not support cross: ${destination.vmId}`)
throw new CrossError(`destination vm id does not support cross: ${destination.vm.id}`)
}

async estimateExport (
Expand All @@ -92,19 +92,19 @@ export class CrossManager {
destination: Blockchain,
assetId: string
): Promise<BaseFeeData> {
if (source.vmId === JVM_ID) {
if (source.vm.id === JVM_ID) {
return await estimateJVMExportTransaction(provider)
} else if (source.vmId === PLATFORMVM_ID) {
} else if (source.vm.id === PLATFORMVM_ID) {
return await estimatePlatformExportTransaction(provider)
} else if (source.vmId === JEVM_ID) {
} else if (source.vm.id === JEVM_ID) {
const api: JEVMAPI = provider.jevmApi[source.id]
return await estimateEVMExportTransaction(api, assetId, destination)
}
throw new CrossError(`source vm id does not support cross: ${source.vmId}`)
throw new CrossError(`source vm id does not support cross: ${source.vm.id}`)
}

canPayImportFee (destination: Blockchain, importFee: bigint, importFeeAssetDestinationBalance: bigint): boolean {
return destination.vmId !== JEVM_ID ? importFeeAssetDestinationBalance >= importFee : false
return destination.vm.id !== JEVM_ID ? importFeeAssetDestinationBalance >= importFee : false
}

shouldSendImportFee (
Expand All @@ -131,8 +131,8 @@ export class CrossManager {
// initial cross transaction can be finalized by doing a regular JVM to JUNE chain cross transaction with the
// initial exported funds towards it.
return (
cross.source.vmId === JEVM_ID &&
cross.destination.vmId === JEVM_ID &&
cross.source.vm.id === JEVM_ID &&
cross.destination.vm.id === JEVM_ID &&
cross.source.id !== this.provider.juneChain.id
)
}
Expand All @@ -153,7 +153,7 @@ export class CrossManager {
if (source.id === destination.id) {
throw new CrossError('source and destination chain cannot be the same')
}
if (source.vmId === JVM_ID) {
if (source.vm.id === JVM_ID) {
return await executeJVMExportTransaction(
provider,
this.wallet,
Expand All @@ -167,7 +167,7 @@ export class CrossManager {
utxoSet,
extraFeeAmount
)
} else if (source.vmId === PLATFORMVM_ID) {
} else if (source.vm.id === PLATFORMVM_ID) {
return await executePlatformExportTransaction(
provider,
this.wallet,
Expand All @@ -180,7 +180,7 @@ export class CrossManager {
exportFee,
utxoSet
)
} else if (source.vmId === JEVM_ID) {
} else if (source.vm.id === JEVM_ID) {
const api: JEVMAPI = provider.jevmApi[source.id]
return await executeEVMExportTransaction(
provider,
Expand All @@ -195,7 +195,7 @@ export class CrossManager {
exportFee
)
}
throw new CrossError(`source vm id does not support cross: ${source.vmId}`)
throw new CrossError(`source vm id does not support cross: ${source.vm.id}`)
}

async import (
Expand All @@ -209,18 +209,18 @@ export class CrossManager {
if (source.id === destination.id) {
throw new CrossError('source and destination chain cannot be the same')
}
if (destination.vmId === JVM_ID) {
if (destination.vm.id === JVM_ID) {
return await executeJVMImportTransaction(provider, this.wallet, source, importFee, utxoSet)
} else if (destination.vmId === PLATFORMVM_ID) {
} else if (destination.vm.id === PLATFORMVM_ID) {
return await executePlatformImportTransaction(provider, this.wallet, source, importFee, utxoSet)
} else if (destination.vmId === JEVM_ID) {
} else if (destination.vm.id === JEVM_ID) {
if (payImportFee) {
throw new CrossError(`vm id ${destination.vmId} cannot pay import fee`)
throw new CrossError(`vm id ${destination.vm.id} cannot pay import fee`)
}
const api: JEVMAPI = provider.jevmApi[destination.id]
return await executeEVMImportTransaction(provider, api, this.wallet, source, importFee, utxoSet)
}
throw new CrossError(`destination vm id does not support cross: ${destination.vmId}`)
throw new CrossError(`destination vm id does not support cross: ${destination.vm.id}`)
}

async estimateCrossOperation (cross: CrossOperation, account: MCNAccount): Promise<CrossOperationSummary> {
Expand Down Expand Up @@ -353,7 +353,7 @@ export class CrossManager {
cross.destination = jvmChain
await this.executeCrossOperationStep(summary, account, cross, summary.fees[0], summary.fees[1])
// jevm cross transactions amount must be changed because of atomic denominator
if (cross.source.vmId === JEVM_ID && cross.assetId === cross.source.assetId) {
if (cross.source.vm.id === JEVM_ID && cross.assetId === cross.source.assetId) {
cross.amount /= AtomicDenomination
}
cross.source = jvmChain
Expand Down Expand Up @@ -408,7 +408,7 @@ export class CrossManager {
const balancesSync: Array<Promise<void>> = []
let sourceUtxos: Utxo[] = []
const sourceAccount: ChainAccount = account.getAccount(cross.source.id)
if (cross.source.vmId === JVM_ID || cross.source.vmId === PLATFORMVM_ID) {
if (cross.source.vm.id === JVM_ID || cross.source.vm.id === PLATFORMVM_ID) {
sourceUtxos = (sourceAccount as UtxoAccount).utxoSet
}
const destinationAccount: ChainAccount = account.getAccount(cross.destination.id)
Expand Down Expand Up @@ -451,7 +451,7 @@ export class CrossManager {
cross.source.id,
exportTransactionId
)
const destinationVmId: string = cross.destination.vmId
const destinationVmId: string = cross.destination.vm.id
if (!cross.sendImportFee && (destinationVmId === JVM_ID || destinationVmId === PLATFORMVM_ID)) {
destinationUtxos.push(...(destinationAccount as UtxoAccount).utxoSet)
}
Expand Down
22 changes: 11 additions & 11 deletions src/wallet/transaction/platform.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { type PlatformBlockchain } from '../../chain'
import { type MCNProvider } from '../../juneo'
import {
Validator,
type UnsignedTransaction,
NodeId,
buildAddPermissionlessValidatorTransaction,
ProofOfPossession,
PrimarySigner,
Address,
buildAddPermissionlessDelegatorTransaction,
buildCreateSupernetTransaction,
buildAddPermissionlessValidatorTransaction,
buildAddSupernetValidatorTransaction,
CreateSupernetTransaction,
Address,
buildRemoveSupernetValidatorTransaction,
buildCreateChainTransaction,
type DynamicId
buildCreateSupernetTransaction,
buildRemoveSupernetValidatorTransaction,
CreateSupernetTransaction,
type DynamicId,
NodeId,
PrimarySigner,
ProofOfPossession,
type UnsignedTransaction,
Validator
} from '../../transaction'
import { type PlatformAccount } from '../account'
import {
Expand Down
Loading

0 comments on commit 99bf696

Please sign in to comment.