Skip to content
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@poppyseed/lastic-sdk",
"author": "Lastic <[email protected]> (https://lastic.xyz)",
"version": "0.2.17",
"version": "0.2.20",
"description": "Typesafe React Hooks abstracting functionality of polkadot.js, created for Lastic website",
"homepage": "https://lastic.xyz",
"license": "GPL-3.0",
Expand Down Expand Up @@ -113,6 +113,7 @@
"@changesets/cli": "^2.27.5",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@mimirdev/apps-inject": "^0.3.0",
"@mui/material": "^5.15.20",
"@polkadot/api": "^11.3.1",
"@polkadot/api-contract": "^11.3.1",
Expand Down
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

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

11 changes: 5 additions & 6 deletions src/hooks/broker/txButton.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Toast } from '@/types'
import { signedTx, unsignedTx } from '@/utils'
import { ApiPromise } from '@polkadot/api'
import { InjectedAccount } from '@polkadot/extension-inject/types'
import { Signer } from '@polkadot/types/types'
import { InjectedAccount, InjectedExtension } from '@polkadot/extension-inject/types'
import { useState } from 'react'

export interface TxButtonProps {
Expand All @@ -17,7 +16,7 @@ export interface TxButtonProps {
}
type: 'SIGNED-TX' | 'UNSIGNED-TX'
activeAccount: InjectedAccount | undefined
activeSigner: Signer | undefined
activeExtension: InjectedExtension | undefined
}

interface UseTxButtonResult {
Expand All @@ -32,7 +31,7 @@ export const useTxButton = ({
attrs,
type,
activeAccount,
activeSigner,
activeExtension,
}: TxButtonProps): UseTxButtonResult => {
const [status, setStatus] = useState<string | null>(null)
const [unsub, setUnsub] = useState<(() => void) | null>(null)
Expand All @@ -51,15 +50,15 @@ export const useTxButton = ({
setStatus('Sending...')

// Check if API, account, and signer are present
if (!api || !activeAccount || !activeSigner) {
if (!api || !activeAccount || !activeExtension) {
setStatus('Error: API, account or signer not present')
return
}

// Call the appropriate transaction function based on the type
try {
if (isSigned()) {
await signedTx(api, attrs, setStatus, addToast, setUnsub, activeAccount, activeSigner)
await signedTx(api, attrs, setStatus, addToast, setUnsub, activeAccount, activeExtension)
} else if (isUnsigned()) {
await unsignedTx(api, attrs, setStatus, addToast, setUnsub)
}
Expand Down
17 changes: 16 additions & 1 deletion src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import {
getSubstrateWallet,
isWalletInstalled
} from '@/wallets'
import { inject, isMimirReady, MIMIR_REGEXP } from '@mimirdev/apps-inject'
import { ApiPromise, HttpProvider, WsProvider } from '@polkadot/api'
import { ApiOptions } from '@polkadot/api/types'
import { InjectedAccount, InjectedExtension, Unsubcall } from '@polkadot/extension-inject/types'
import { Signer } from '@polkadot/types/types'
import {
createContext,
FC,
PropsWithChildren,
createContext,
useContext,
useEffect,
useRef,
Expand Down Expand Up @@ -136,6 +137,16 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
...apiOptions,
}))

;(async () => {
// special, apps will working in iframe with mimir wallet
// so, check if the environment in ifram
const origin = await isMimirReady();
if (origin && MIMIR_REGEXP.test(origin)) {
// inject window.injectedWeb3.mimir
inject()
}
})()

api?.disconnect()
setApi(_api)
relayApi?.disconnect()
Expand Down Expand Up @@ -236,6 +247,10 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
activeExtension.current = extension
activeSigner.current = extension?.signer as Signer

extension?.accounts.get().then(accounts => {
updateAccounts(accounts, lastActiveAccountAddress)
})

// Query & keep listening to injected accounts
unsubscribeAccounts.current?.()
const unsubscribe = extension?.accounts.subscribe((accounts) => {
Expand Down
32 changes: 25 additions & 7 deletions src/utils/broker_tx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Toast } from '@/types'
import { checkCall } from '@/utils/check'
import { ApiPromise, SubmittableResult } from '@polkadot/api'
import { InjectedAccount } from '@polkadot/extension-inject/types'
import { Signer } from '@polkadot/types/types'
import { InjectedAccount, InjectedExtension } from '@polkadot/extension-inject/types'
import { Dispatch, SetStateAction } from 'react'
import { transformParams } from './broker'
import { txErrHandler, txResHandler } from './broker_handler'
Expand All @@ -28,21 +28,39 @@ const signedTx = async (
addToast: (toast: Omit<Toast, 'id'>) => void,
setUnsub: Dispatch<SetStateAction<any>>,
activeAccount: InjectedAccount,
activeSigner: Signer,
activeExtension: InjectedExtension
) => {
const address = activeAccount?.address
const transformed = transformParams(paramFields, inputParams)

const txExecute = transformed
let txExecute = transformed
? api.tx[palletRpc][callable](...transformed)
: api.tx[palletRpc][callable]()

const signerOptions = {
signer: activeSigner,
const isMimir = activeExtension.name === 'mimir'

if (isMimir) {
const result: any = await activeExtension.signer.signPayload?.({
address,
genesisHash: api.genesisHash.toHex(),
method: txExecute.method.toHex()
} as unknown as any)

const method = api.registry.createType('Call', result.payload.method)

if (!checkCall(api, method, txExecute.method)) {
throw new Error('not safe tx')
}

txExecute = api.tx[method.section][method.method](...method.args)

txExecute.addSignature(result.signer, result.signature, result.payload)
} else {
await txExecute.signAsync(address, { signer: activeExtension.signer })
}

const unsub = await txExecute
.signAndSend(address, signerOptions, (result: SubmittableResult) =>
.send((result: SubmittableResult) =>
txResHandler(setStatus, api, addToast, result),
)
.catch((err: Error) => txErrHandler(setStatus, addToast, err))
Expand Down
36 changes: 36 additions & 0 deletions src/utils/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023-2024 dev.mimir authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { ApiPromise } from '@polkadot/api'

import type { Call } from '@polkadot/types/interfaces'
import type { IMethod } from '@polkadot/types/types'

function findFinalCall (api: ApiPromise, call: Call | IMethod): Call | IMethod {
if (api.tx.multisig?.asMulti?.is(call)) {
const data = call.args[3].toU8a(true)

return findFinalCall(api, api.registry.createType('Call', data))
} else if (api.tx.proxy?.proxy?.is(call)) {
const data = call.args[2].toU8a(true)

return findFinalCall(api, api.registry.createType('Call', data))
} else {
return call
}
}

export function checkCall (api: ApiPromise, multisigCall: Call | IMethod, expectCall: Call | IMethod): boolean {
const finalCall = findFinalCall(api, multisigCall)

if (!finalCall.hash.eq(expectCall.hash)) {
const meta = api.registry.findMetaCall(finalCall.callIndex)
const expectMeta = api.registry.findMetaCall(finalCall.callIndex)

console.warn(`the final call found in multisigCall is (${meta.section}.${meta.method}), which does not match the expected call(${expectMeta.section}.${expectMeta.method}).`)

return false
} else {
return true
}
}
17 changes: 16 additions & 1 deletion src/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,25 @@ export const nightlyConnect: SubstrateWallet = {
'https://github.com/scio-labs/use-inkathon/raw/main/assets/wallet-logos/[email protected]',
],
}

export const mimir: SubstrateWallet = {
id: 'mimir',
name: 'Mimir Wallet',
platforms: [
SubstrateWalletPlatform.Browser
],
urls: {
website: 'https://mimir.global',
},
logoUrls: [
'https://app.mimir.global/icons/[email protected]',
'https://app.mimir.global/icons/[email protected]'
],
}
/**
* Exporting all wallets separately
*/
export const allSubstrateWallets: SubstrateWallet[] = [subwallet, talisman, polkadotjs, nova]
export const allSubstrateWallets: SubstrateWallet[] = [subwallet, talisman, polkadotjs, nova, mimir]

/**
* Returns wallet (if existent) for given identifier (`id` field).
Expand Down