Skip to content

Commit

Permalink
enhancement: better error handling when unsupported chain definition …
Browse files Browse the repository at this point in the history
…is used
  • Loading branch information
dafuga committed Jul 27, 2024
1 parent 92ab2b2 commit 310e691
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [14, 16, 18]
node-version: [18, 20]
name: Node.js v${{ matrix.node-version }}
steps:
- name: Setup Node.js
Expand Down
29 changes: 19 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {
AbstractAccountCreationPlugin,
AccountCreationPlugin,
AccountCreationPluginConfig,
CreateAccountContext,
Chains,
Checksum256Type,
CreateAccountContext,
PublicKey,
Chains,
} from '@wharfkit/session'
import {AccountCreationPluginMetadata} from '@wharfkit/session'
import {MetaMaskInpageProvider, RequestArguments} from '@metamask/providers'
Expand All @@ -28,6 +28,7 @@ export class AccountCreationPluginMetamask

readonly config: AccountCreationPluginConfig = {
requiresChainSelect: true,
supportedChains: [Chains.EOS, Chains.Jungle4],
}

readonly metadata: AccountCreationPluginMetadata = AccountCreationPluginMetadata.from({
Expand All @@ -44,32 +45,40 @@ export class AccountCreationPluginMetamask
}

async create(context: CreateAccountContext) {
if (context.chain?.id !== Chains.EOS.id) {
throw new Error('Only EOS is currently supported by this plugin.')
if (!context.chain) {
throw new Error('Chain not provided')
}
const currentChain = this.config.supportedChains?.find(
(chain) => chain.name === context.chain?.name
)
if (!currentChain) {
throw new Error(
`Chain not supported. This plugin only supports ${this.config.supportedChains
?.map((chain) => chain.name)
.join(', ')}`
)
}
const qs = new URLSearchParams()
qs.set('supported_chains', String(context.chain))
qs.set('supported_chains', String(currentChain))
if (context.appName) {
qs.set('scope', String(context.appName))
}

const publicKey = await this.retrievePublicKey(context.chain.id)
const publicKey = await this.retrievePublicKey(currentChain.id)

qs.set('owner_key', String(publicKey))
qs.set('active_key', String(publicKey))
const accountCreator = new AccountCreator({
supportedChains: [String(context.chain?.id)],
supportedChains: [String(currentChain.id)],
fullCreationServiceUrl: `${ACCOUNT_CREATION_SERVICE_URL}?${qs.toString()}`,
scope: context.appName || 'Antelope App',
})
const accountCreationResponse = await accountCreator.createAccount()

console.log({accountCreationResponse})

if ('sa' in accountCreationResponse && 'sp' in accountCreationResponse) {
return {
accountName: accountCreationResponse.sa,
chain: Chains.EOS,
chain: context.chain,
}
} else {
throw new Error(accountCreationResponse.error)
Expand Down

0 comments on commit 310e691

Please sign in to comment.