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 614e966
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 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
28 changes: 20 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
AbstractAccountCreationPlugin,
AccountCreationPlugin,
AccountCreationPluginConfig,
CreateAccountContext,
Checksum256Type,
CreateAccountContext,
PublicKey,
Chains,
} from '@wharfkit/session'
Expand All @@ -16,7 +16,7 @@ export type GetSnapsResponse = Record<string, Snap>

const DEFAULT_SNAP_ORIGIN = 'local:http://localhost:8080'
const ACCOUNT_CREATION_SERVICE_URL =
'https://adding-login-through-apple.account-creation-portal.pages.dev/buy'
'https://unlimited-accounts.account-creation-portal.pages.dev/buy'

export class AccountCreationPluginMetamask
extends AbstractAccountCreationPlugin
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,21 +45,32 @@ 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
)
console.log({currentChain})
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',
})
Expand All @@ -69,7 +81,7 @@ export class AccountCreationPluginMetamask
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 614e966

Please sign in to comment.