@@ -20,8 +20,10 @@ import {
2020 InjectiveExecutor ,
2121} from "@pythnetwork/cosmwasm-deploy-tools" ;
2222import { Network } from "@injectivelabs/networks" ;
23+ import { IotaClient } from "@iota/iota-sdk/client" ;
2324import { SuiClient } from "@mysten/sui/client" ;
24- import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519" ;
25+ import { Ed25519Keypair as IotaEd25519Keypair } from "@iota/iota-sdk/keypairs/ed25519" ;
26+ import { Ed25519Keypair as SuiEd25519Keypair } from "@mysten/sui/keypairs/ed25519" ;
2527import { TokenId } from "./token" ;
2628import { BN , Provider , Wallet , WalletUnlocked } from "fuels" ;
2729import { FUEL_ETH_ASSET_ID } from "@pythnetwork/pyth-fuel-js" ;
@@ -38,6 +40,8 @@ import { keyPairFromSeed } from "@ton/crypto";
3840import { PythContract } from "@pythnetwork/pyth-ton-js" ;
3941import * as nearAPI from "near-api-js" ;
4042import * as bs58 from "bs58" ;
43+ import { MIST_PER_SUI } from "@mysten/sui/utils" ;
44+ import { NANOS_PER_IOTA } from "@iota/iota-sdk/utils" ;
4145
4246/**
4347 * Returns the chain rpc url with any environment variables replaced or throws an error if any are missing
@@ -333,8 +337,8 @@ export class SuiChain extends Chain {
333337 }
334338
335339 async getAccountAddress ( privateKey : PrivateKey ) : Promise < string > {
336- const keypair = Ed25519Keypair . fromSecretKey (
337- Buffer . from ( privateKey , "hex" )
340+ const keypair = SuiEd25519Keypair . fromSecretKey (
341+ new Uint8Array ( Buffer . from ( privateKey , "hex" ) )
338342 ) ;
339343 return keypair . toSuiAddress ( ) ;
340344 }
@@ -344,7 +348,73 @@ export class SuiChain extends Chain {
344348 const balance = await provider . getBalance ( {
345349 owner : await this . getAccountAddress ( privateKey ) ,
346350 } ) ;
347- return Number ( balance . totalBalance ) / 10 ** 9 ;
351+ return Number ( balance . totalBalance ) / Number ( MIST_PER_SUI ) ;
352+ }
353+ }
354+
355+ export class IotaChain extends Chain {
356+ static type = "IotaChain" ;
357+
358+ constructor (
359+ id : string ,
360+ mainnet : boolean ,
361+ wormholeChainName : string ,
362+ nativeToken : TokenId | undefined ,
363+ public rpcUrl : string
364+ ) {
365+ super ( id , mainnet , wormholeChainName , nativeToken ) ;
366+ }
367+
368+ static fromJson ( parsed : ChainConfig ) : IotaChain {
369+ if ( parsed . type !== IotaChain . type ) throw new Error ( "Invalid type" ) ;
370+ return new IotaChain (
371+ parsed . id ,
372+ parsed . mainnet ,
373+ parsed . wormholeChainName ,
374+ parsed . nativeToken ,
375+ parsed . rpcUrl
376+ ) ;
377+ }
378+
379+ toJson ( ) : KeyValueConfig {
380+ return {
381+ id : this . id ,
382+ wormholeChainName : this . wormholeChainName ,
383+ mainnet : this . mainnet ,
384+ rpcUrl : this . rpcUrl ,
385+ type : IotaChain . type ,
386+ } ;
387+ }
388+
389+ getType ( ) : string {
390+ return IotaChain . type ;
391+ }
392+
393+ /**
394+ * Returns the payload for a governance contract upgrade instruction for contracts deployed on this chain
395+ * @param digest hex string of the 32 byte digest for the new package without the 0x prefix
396+ */
397+ generateGovernanceUpgradePayload ( digest : string ) : Buffer {
398+ return new UpgradeContract256Bit ( this . wormholeChainName , digest ) . encode ( ) ;
399+ }
400+
401+ getProvider ( ) : IotaClient {
402+ return new IotaClient ( { url : this . rpcUrl } ) ;
403+ }
404+
405+ async getAccountAddress ( privateKey : PrivateKey ) : Promise < string > {
406+ const keypair = IotaEd25519Keypair . fromSecretKey (
407+ new Uint8Array ( Buffer . from ( privateKey , "hex" ) )
408+ ) ;
409+ return keypair . toIotaAddress ( ) ;
410+ }
411+
412+ async getAccountBalance ( privateKey : PrivateKey ) : Promise < number > {
413+ const provider = this . getProvider ( ) ;
414+ const balance = await provider . getBalance ( {
415+ owner : await this . getAccountAddress ( privateKey ) ,
416+ } ) ;
417+ return Number ( balance . totalBalance ) / Number ( NANOS_PER_IOTA ) ;
348418 }
349419}
350420
@@ -912,7 +982,9 @@ export class NearChain extends Chain {
912982
913983 async getAccountAddress ( privateKey : PrivateKey ) : Promise < string > {
914984 return Buffer . from (
915- Ed25519Keypair . fromSecretKey ( Buffer . from ( privateKey , "hex" ) )
985+ SuiEd25519Keypair . fromSecretKey (
986+ new Uint8Array ( Buffer . from ( privateKey , "hex" ) )
987+ )
916988 . getPublicKey ( )
917989 . toRawBytes ( )
918990 ) . toString ( "hex" ) ;
@@ -931,7 +1003,9 @@ export class NearChain extends Chain {
9311003 ) : Promise < nearAPI . Account > {
9321004 const keyStore = new nearAPI . keyStores . InMemoryKeyStore ( ) ;
9331005 if ( typeof senderPrivateKey !== "undefined" ) {
934- const key = bs58 . encode ( Buffer . from ( senderPrivateKey , "hex" ) ) ;
1006+ const key = bs58 . encode (
1007+ new Uint8Array ( Buffer . from ( senderPrivateKey , "hex" ) )
1008+ ) ;
9351009 const keyPair = nearAPI . KeyPair . fromString ( key ) ;
9361010 const address = await this . getAccountAddress ( senderPrivateKey ) ;
9371011 await keyStore . setKey ( this . networkId , address , keyPair ) ;
0 commit comments