Skip to content

Commit

Permalink
Merge pull request #181 from alephium/add-signature
Browse files Browse the repository at this point in the history
Add signatures in wallet functions
  • Loading branch information
h0ngcha0 authored Feb 16, 2024
2 parents 5b29a69 + 85175a5 commit d33f3e9
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 299 deletions.
35 changes: 23 additions & 12 deletions packages/extension/src/background/actionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export const handleActionApproval = async (
switch (action.type) {
case "CONNECT_DAPP": {
const { host, networkId, group, keyType } = action.payload
const selectedAccount = await wallet.getAlephiumSelectedAddress(networkId, group, keyType)
const selectedAccount = await wallet.getAlephiumSelectedAddress(
networkId,
group,
keyType,
)

if (!selectedAccount) {
openUi()
Expand All @@ -45,11 +49,12 @@ export const handleActionApproval = async (
}

case "TRANSACTION": {
const {signature, ...transaction} = additionalData as (ReviewTransactionResult & { signature?: string })
const { signature: signatureOpt, ...transaction } =
additionalData as ReviewTransactionResult & { signature?: string }
try {
await executeTransactionAction(
const { signature } = await executeTransactionAction(
transaction,
signature,
signatureOpt,
background,
transaction.params.networkId,
)
Expand All @@ -58,7 +63,7 @@ export const handleActionApproval = async (

return {
type: "ALPH_TRANSACTION_SUBMITTED",
data: { result: transaction.result, actionHash },
data: { result: { ...transaction.result, signature } , actionHash },
}
} catch (error: unknown) {
return {
Expand All @@ -69,7 +74,10 @@ export const handleActionApproval = async (
}

case "SIGN_MESSAGE": {
const account = await wallet.getAccount({ address: action.payload.signerAddress, networkId: action.payload.networkId })
const account = await wallet.getAccount({
address: action.payload.signerAddress,
networkId: action.payload.networkId,
})
if (!account) {
throw Error("No selected account")
}
Expand All @@ -85,23 +93,26 @@ export const handleActionApproval = async (
}
}

case 'SIGN_UNSIGNED_TX': {
case "SIGN_UNSIGNED_TX": {
try {
const account = await wallet.getAccount({ address: action.payload.signerAddress, networkId: action.payload.networkId })
const account = await wallet.getAccount({
address: action.payload.signerAddress,
networkId: action.payload.networkId,
})
if (!account) {
throw Error("No selected account")
}

const result = await wallet.signUnsignedTx(account, action.payload)

return {
type: 'ALPH_SIGN_UNSIGNED_TX_SUCCESS',
data: { actionHash, result }
type: "ALPH_SIGN_UNSIGNED_TX_SUCCESS",
data: { actionHash, result },
}
} catch (error) {
return {
type: 'ALPH_SIGN_UNSIGNED_TX_FAILURE',
data: { actionHash, error: `${error}` }
type: "ALPH_SIGN_UNSIGNED_TX_FAILURE",
data: { actionHash, error: `${error}` },
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
import {
ReviewTransactionResult,
} from "../../shared/actionQueue/types"
import { BackgroundService } from "../background"
import { ReviewTransactionResult } from "../../shared/actionQueue/types"
import { addTransaction } from "../../shared/transactions/store"
import { BackgroundService } from "../background"

export const executeTransactionAction = async (
transaction: ReviewTransactionResult,
signature: string | undefined,
signatureOpt: string | undefined,
{ wallet }: BackgroundService,
networkId: string,
) => {
): Promise<{ signature: string }> => {
const account = await wallet.getAccount({
address: transaction.params.signerAddress,
networkId: networkId,
})

if (signature === undefined) {
await wallet.signAndSubmitUnsignedTx(account, {
signerAddress: account.address,
unsignedTx: transaction.result.unsignedTx,
})
let finalSignature = signatureOpt ?? "Getting signature"
if (signatureOpt === undefined) {
finalSignature = (
await wallet.signAndSubmitUnsignedTx(account, {
signerAddress: account.address,
unsignedTx: transaction.result.unsignedTx,
})
).signature
} else {
await wallet.submitSignedTx(account, transaction.result.unsignedTx, signature)
await wallet.submitSignedTx(
account,
transaction.result.unsignedTx,
signatureOpt,
)
}

if (account !== undefined) {
addTransaction({
account: account,
hash: transaction.result.txId,
meta: {
request: transaction
}
request: transaction,
},
})
}

return { signature: finalSignature }
}
Loading

0 comments on commit d33f3e9

Please sign in to comment.