-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from arconnectio/development
ArConnect BETA 1.18.4
- Loading branch information
Showing
14 changed files
with
733 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/api/modules/batch_sign_data_item/batch_sign_data_item.background.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { ModuleFunction } from "~api/module"; | ||
import { isNotCancelError, isRawDataItem } from "~utils/assertions"; | ||
import authenticate from "../connect/auth"; | ||
import browser from "webextension-polyfill"; | ||
import { getActiveKeyfile } from "~wallets"; | ||
import { freeDecryptedWallet } from "~wallets/encryption"; | ||
import { ArweaveSigner, createData, DataItem } from "arbundles"; | ||
import type { RawDataItem } from "../sign_data_item/types"; | ||
|
||
const background: ModuleFunction<number[][]> = async ( | ||
appData, | ||
dataItems: unknown[] | ||
) => { | ||
// validate | ||
if (!Array.isArray(dataItems)) { | ||
throw new Error("Input must be an array of data items"); | ||
} | ||
|
||
for (const dataItem of dataItems) { | ||
isRawDataItem(dataItem); | ||
} | ||
|
||
const results: number[][] = []; | ||
|
||
await authenticate({ | ||
type: "batchSignDataItem", | ||
data: dataItems, | ||
appData | ||
}); | ||
|
||
// grab the user's keyfile | ||
const decryptedWallet = await getActiveKeyfile().catch((e) => { | ||
isNotCancelError(e); | ||
|
||
// if there are no wallets added, open the welcome page | ||
browser.tabs.create({ url: browser.runtime.getURL("tabs/welcome.html") }); | ||
|
||
throw new Error("No wallets added"); | ||
}); | ||
|
||
try { | ||
if (decryptedWallet.type !== "local") { | ||
throw new Error( | ||
"Only local wallets are currently supported for batch signing" | ||
); | ||
} | ||
|
||
const dataSigner = new ArweaveSigner(decryptedWallet.keyfile); | ||
|
||
for (const dataItem of dataItems as RawDataItem[]) { | ||
const { data, ...options } = dataItem; | ||
const binaryData = new Uint8Array(data); | ||
|
||
const dataEntry = createData(binaryData, dataSigner, options); | ||
|
||
await dataEntry.sign(dataSigner); | ||
|
||
results.push(Array.from<number>(dataEntry.getRaw())); | ||
} | ||
} finally { | ||
// @ts-expect-error | ||
freeDecryptedWallet(decryptedWallet.keyfile); | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
export default background; |
54 changes: 54 additions & 0 deletions
54
src/api/modules/batch_sign_data_item/batch_sign_data_item.foreground.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { TransformFinalizer } from "~api/foreground"; | ||
import type { ModuleFunction } from "~api/module"; | ||
import type { RawDataItem, SignDataItemParams } from "../sign_data_item/types"; | ||
import { isArrayBuffer } from "~utils/assertions"; | ||
|
||
const MAX_TOTAL_SIZE = 200 * 1024; | ||
|
||
const foreground: ModuleFunction<Record<any, any>[]> = async ( | ||
dataItems: SignDataItemParams[] | ||
) => { | ||
if (!Array.isArray(dataItems)) { | ||
throw new Error("Input must be an array of data items"); | ||
} | ||
|
||
const totalSize = dataItems.reduce((acc, dataItem) => { | ||
const dataSize = | ||
typeof dataItem.data === "string" | ||
? new TextEncoder().encode(dataItem.data).length | ||
: dataItem.data.length; | ||
return acc + dataSize; | ||
}, 0); | ||
|
||
if (totalSize > MAX_TOTAL_SIZE) { | ||
throw new Error("Total size of data items exceeds 200 KB"); | ||
} | ||
|
||
const rawDataItems: RawDataItem[] = dataItems.map((dataItem) => { | ||
let rawDataItem: RawDataItem; | ||
|
||
if (typeof dataItem.data !== "string") { | ||
isArrayBuffer(dataItem.data); | ||
|
||
rawDataItem = { | ||
...dataItem, | ||
data: Array.from(dataItem.data) | ||
}; | ||
} else { | ||
rawDataItem = { | ||
...dataItem, | ||
data: Array.from(new TextEncoder().encode(dataItem.data)) | ||
}; | ||
} | ||
|
||
return rawDataItem; | ||
}); | ||
|
||
return [rawDataItems]; | ||
}; | ||
|
||
export const finalizer: TransformFinalizer<number[][]> = (result) => { | ||
return result.map((item) => new Uint8Array(item).buffer); | ||
}; | ||
|
||
export default foreground; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { PermissionType } from "~applications/permissions"; | ||
import type { ModuleProperties } from "~api/module"; | ||
|
||
const permissions: PermissionType[] = ["SIGN_TRANSACTION"]; | ||
|
||
const batchSignDataItem: ModuleProperties = { | ||
functionName: "batchSignDataItem", | ||
permissions | ||
}; | ||
|
||
export default batchSignDataItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.