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

Fix dep versions, missing fields, and switch chain #183

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/account/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aleph-sdk/account",
"version": "1.0.3",
"version": "1.1.0",
"description": "Common interfaces for accounts compatible with aleph.im.",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
Expand All @@ -13,7 +13,7 @@
"access": "public"
},
"peerDependencies": {
"@aleph-sdk/core": "^1.x.x"
"@aleph-sdk/core": "../core"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
4 changes: 2 additions & 2 deletions packages/avalanche/__tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ describe('Avalanche accounts', () => {

it('should throw Error to get a Keypair', async () => {
const fakePrivateKey = 'a'
const fct = async () => await avalanche.importAccountFromPrivateKey(fakePrivateKey)
const fct = async () => await avalanche.importAccountFromPrivateKey(fakePrivateKey, avalanche.ChainType.X_CHAIN)

await expect(fct).rejects.toThrow('Invalid private key')
})

it('should import an avalanche accounts using a provider', async () => {
it('should import an avalanche account using a provider', async () => {
const { address, privateKey } = ephemeralAccount
if (!privateKey) throw Error('Can not retrieve privateKey inside ephemeralAccount.json')

Expand Down
6 changes: 3 additions & 3 deletions packages/avalanche/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"sha.js": "^2.4.11"
},
"peerDependencies": {
"@aleph-sdk/account": "^1.x.x",
"@aleph-sdk/core": "^1.x.x",
"@aleph-sdk/evm": "^1.x.x",
"@aleph-sdk/account": "../account",
"@aleph-sdk/core": "../core",
"@aleph-sdk/evm": "../evm",
"avalanche": "^3.15.3",
"ethers": "^5.x.x"
},
Expand Down
29 changes: 19 additions & 10 deletions packages/avalanche/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import { digestMessage, verifyAvalanche } from './verify'
* It is used to represent an Avalanche account when publishing a message on the Aleph network.
*/
export class AvalancheAccount extends EVMAccount {
public declare readonly wallet?: BaseProviderWallet
public readonly keyPair?: KeyPair | EVMKeyPair

constructor(
signerOrWallet: KeyPair | EVMKeyPair | BaseProviderWallet | ethers.providers.JsonRpcProvider,
signerOrWallet: KeyPair | EVMKeyPair | ethers.Wallet | BaseProviderWallet | ethers.providers.JsonRpcProvider,
address: string,
publicKey?: string,
) {
Expand Down Expand Up @@ -45,6 +44,11 @@ export class AvalancheAccount extends EVMAccount {
override async askPubKey(): Promise<void> {
if (this.publicKey) return
if (!this.wallet) throw Error('PublicKey Error: No providers are setup')

if (this.wallet instanceof ethers.Wallet) {
this.publicKey = this.wallet.publicKey
return
}
this.publicKey = await this.wallet.getPublicKey()
return
}
Expand Down Expand Up @@ -91,11 +95,11 @@ export enum ChainType {
* @param chain Avalanche chain type: c-chain | x-chain
* @returns key chains
*/
async function getKeyChain(chain = ChainType.X_CHAIN) {
async function getKeyChain(chain = ChainType.C_CHAIN) {
return new KeyChain(new Avalanche().getHRP(), chain)
}

export async function getKeyPair(privateKey?: string, chain = ChainType.X_CHAIN): Promise<KeyPair> {
export async function getKeyPair(privateKey?: string, chain = ChainType.C_CHAIN): Promise<KeyPair> {
const keyChain = await getKeyChain(chain)
const keyPair = keyChain.makeKey()

Expand Down Expand Up @@ -123,10 +127,15 @@ export async function getKeyPair(privateKey?: string, chain = ChainType.X_CHAIN)
*/
export async function importAccountFromPrivateKey(
privateKey: string,
chain = ChainType.X_CHAIN,
chain = ChainType.C_CHAIN,
): Promise<AvalancheAccount> {
const keyPair = await getKeyPair(privateKey, chain)
return new AvalancheAccount(keyPair, keyPair.getAddressString(), keyPair.getPublicKey().toString('hex'))
if (chain === ChainType.X_CHAIN) {
const keyPair = await getKeyPair(privateKey, chain)
return new AvalancheAccount(keyPair, keyPair.getAddressString(), keyPair.getPublicKey().toString('hex'))
} else {
const wallet = new ethers.Wallet(privateKey)
return new AvalancheAccount(wallet, wallet.address, wallet.publicKey)
}
}

/**
Expand All @@ -141,7 +150,7 @@ export async function importAccountFromPrivateKey(
export async function importAccountFromMnemonic(
mnemonic: string,
derivationPath = "m/44'/60'/0'/0/0",
chain = ChainType.X_CHAIN,
chain = ChainType.C_CHAIN,
): Promise<AvalancheAccount> {
const wallet = ethers.Wallet.fromMnemonic(mnemonic, derivationPath)

Expand Down Expand Up @@ -186,14 +195,14 @@ function getEVMAddress(keypair: EVMKeyPair): string {
const pkHex = keypair.getPrivateKey().toString('hex')
const pkBuffNative = Buffer.from(pkHex, 'hex')
const ethAddress = privateToAddress(pkBuffNative).toString('hex')
return `0x${ethAddress}`
return ethers.utils.getAddress(`0x${ethAddress}`)
}

/**
* Creates a new Avalanche account using a randomly generated privateKey
*/
export async function newAccount(
chain = ChainType.X_CHAIN,
chain = ChainType.C_CHAIN,
): Promise<{ account: AvalancheAccount; privateKey: string }> {
const keypair = await getKeyPair(undefined, chain)
const privateKey = keypair.getPrivateKey().toString('hex')
Expand Down
8 changes: 4 additions & 4 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"sha.js": "^2.4.11"
},
"peerDependencies": {
"@aleph-sdk/account": "^1.x.x",
"@aleph-sdk/core": "^1.0.4",
"@aleph-sdk/ethereum": "^1.x.x",
"@aleph-sdk/evm": "^1.0.4",
"@aleph-sdk/account": "../account",
"@aleph-sdk/core": "../core",
"@aleph-sdk/ethereum": "../ethereum",
"@aleph-sdk/evm": "../evm",
"avalanche": "^3.15.3",
"ethers": "^5.x.x"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"@types/sha.js": "^2.4.4"
},
"peerDependencies": {
"@aleph-sdk/account": "^1.x.x",
"@aleph-sdk/core": "^1.x.x",
"@aleph-sdk/message": "^1.x.x"
"@aleph-sdk/account": "../account",
"@aleph-sdk/core": "../core",
"@aleph-sdk/message": "../message"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"elliptic": "^6.5.4"
},
"peerDependencies": {
"@aleph-sdk/account": "^1.x.x",
"@aleph-sdk/core": "^1.x.x",
"@aleph-sdk/account": "../account",
"@aleph-sdk/core": "../core",
"@cosmjs/amino": "^0.32.2"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/ethereum-ledger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"semver": "^7.3.8"
},
"peerDependencies": {
"@aleph-sdk/account": "^1.x.x",
"@aleph-sdk/core": "^1.x.x",
"@aleph-sdk/account": "../account",
"@aleph-sdk/core": "../core",
"@ledgerhq/hw-app-eth": "^6.35.4",
"@ledgerhq/hw-transport": "^6.30.3",
"@ledgerhq/hw-transport-node-hid": "^6.28.3",
Expand Down
6 changes: 3 additions & 3 deletions packages/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"eciesjs": "^0.4.6"
},
"peerDependencies": {
"@aleph-sdk/account": "^1.x.x",
"@aleph-sdk/core": "^1.x.x",
"@aleph-sdk/evm": "^1.x.x",
"@aleph-sdk/account": "../account",
"@aleph-sdk/core": "../core",
"@aleph-sdk/evm": "../evm",
"ethers": "^5.x.x"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"ethers": "^5.x.x"
},
"peerDependencies": {
"@aleph-sdk/account": "^1.x.x",
"@aleph-sdk/core": "^1.x.x",
"@aleph-sdk/account": "../account",
"@aleph-sdk/core": "../core",
"ethers": "^5.x.x"
},
"scripts": {
Expand Down
4 changes: 1 addition & 3 deletions packages/evm/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export abstract class EVMAccount extends ECIESAccount {
public async changeNetwork(chainOrRpc: RpcType | RpcId = RpcId.ETH): Promise<void> {
if (this.wallet instanceof JsonRPCWallet) {
await this.wallet.changeNetwork(chainOrRpc)
}
if (this.wallet instanceof ethers.Wallet) {
//await this.wallet.provider.send("wallet_switchEthereumChain", [{ chainId: chainId.toString(16) }]);
} else if (this.wallet instanceof ethers.Wallet) {
throw new Error('Not implemented for ethers.Wallet')
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/evm/src/provider/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const MetamaskErrorCodes: Record<string, number> = {
REJECTED: 4001,
UNRECOGNIZED: 4902,
}
Loading
Loading