-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
277 additions
and
156 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -11,9 +11,6 @@ | |
}, | ||
"author": "thirdweb eng <[email protected]>", | ||
"type": "module", | ||
"bin": { | ||
"thirdweb": "node_modules/@thirdweb-dev/cli/dist/cli/index.js" | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js", | ||
"types": "./dist/types/index.d.ts", | ||
|
@@ -54,6 +51,11 @@ | |
"import": "./dist/esm/wallets/index.js", | ||
"default": "./dist/cjs/wallets/index.js" | ||
}, | ||
"./utils": { | ||
"types": "./dist/types/utils/index.d.ts", | ||
"import": "./dist/esm/utils/index.js", | ||
"default": "./dist/cjs/utils/index.js" | ||
}, | ||
"./wallets/*": { | ||
"types": "./dist/types/wallets/*.d.ts", | ||
"import": "./dist/esm/wallets/*.js", | ||
|
@@ -91,6 +93,9 @@ | |
"wallets": [ | ||
"./dist/types/wallets/index.d.ts" | ||
], | ||
"utils": [ | ||
"./dist/types/utils/index.d.ts" | ||
], | ||
"wallets/*": [ | ||
"./dist/types/wallets/*.d.ts" | ||
], | ||
|
@@ -127,30 +132,31 @@ | |
"@radix-ui/react-popover": "^1.0.7", | ||
"@radix-ui/react-tabs": "^1.0.4", | ||
"@radix-ui/react-tooltip": "^1.0.7", | ||
"@tanstack/react-query": "^5.18.0", | ||
"@walletconnect/ethereum-provider": "^2.11.0", | ||
"@tanstack/react-query": "^5.18.1", | ||
"@walletconnect/ethereum-provider": "^2.11.1", | ||
"abitype": "^1.0.0", | ||
"bs58": "^5.0.0", | ||
"cbor-x": "^1.5.8", | ||
"fuse.js": "^7.0.0", | ||
"mipd": "^0.0.5", | ||
"qrcode": "^1.5.3", | ||
"viem": "^2.7.1" | ||
"viem": "^2.7.6" | ||
}, | ||
"devDependencies": { | ||
"@tanstack/eslint-plugin-query": "^5.18.0", | ||
"@tanstack/eslint-plugin-query": "^5.18.1", | ||
"@thirdweb-dev/sdk": "workspace:*", | ||
"@types/bun": "^1.0.4", | ||
"@types/bun": "^1.0.5", | ||
"@types/qrcode": "^1.5.5", | ||
"@types/react": "18.2.17", | ||
"@viem/anvil": "^0.0.7", | ||
"@vitest/coverage-v8": "^1.2.2", | ||
"eslint-config-thirdweb": "workspace:*", | ||
"eslint-plugin-better-tree-shaking": "0.0.4", | ||
"eslint-plugin-jsdoc": "^48.0.4", | ||
"eslint-plugin-jsdoc": "^48.0.6", | ||
"ethers5": "npm:ethers@^5.0.0", | ||
"ethers6": "npm:ethers@^6.0.0", | ||
"react": "^18.2.0", | ||
"rimraf": "^5.0.5", | ||
"tinybench": "^2.6.0", | ||
"typedoc-gen": "workspace:*", | ||
"typescript": "^5.3.3", | ||
"vitest": "^1.2.2" | ||
|
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
File renamed without changes.
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,38 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { getByteCode } from "../../contract/index.js"; | ||
import { | ||
DOODLES_CONTRACT, | ||
USDC_CONTRACT, | ||
} from "../../../test/src/test-contracts.js"; | ||
import { extractIPFSUri } from "./extractIPFS.js"; | ||
|
||
describe("extractIPFSUri", () => { | ||
it("works if ipfs is there", async () => { | ||
// get some bytecode | ||
const bytecode = await getByteCode(DOODLES_CONTRACT); | ||
// extract IPFS hash | ||
const ipfsHash = extractIPFSUri(bytecode); | ||
|
||
// DOODLES bytecode contains an IPFS hash | ||
expect(ipfsHash).toMatchInlineSnapshot( | ||
`"ipfs://QmRKWTE2aFc8VZdsHAZ8B7vMStPBFwn4AQr7WGUhjkuJWb"`, | ||
); | ||
}); | ||
|
||
it("does not work if ipfs is not there", async () => { | ||
// get some bytecode | ||
const bytecode = await getByteCode(USDC_CONTRACT); | ||
// extract IPFS hash | ||
const ipfsHash = extractIPFSUri(bytecode); | ||
|
||
// USDC bytecode does not contain an IPFS hash | ||
expect(ipfsHash).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
|
||
it("works with the weird mumbai contract", async () => { | ||
const ipfsHash = extractIPFSUri( | ||
"0x363d3d373d3d3d363d30545af43d82803e903d91601857fd5bf3", | ||
); | ||
expect(ipfsHash).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
}); |
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,40 @@ | ||
import { hexToBytes } from "@noble/hashes/utils"; | ||
import { decode } from "cbor-x"; | ||
import { encode } from "bs58"; | ||
|
||
/** | ||
* Extracts the IPFS URI from the given bytecode. | ||
* @param bytecode - The bytecode to extract the IPFS URI from. | ||
* @returns The IPFS URI if found, otherwise undefined. | ||
* @example | ||
* ```ts | ||
* import { extractIPFSUri } from "thirdweb/utils/bytecode/extractIPFS"; | ||
* const bytecode = "0x363d3d373d3d3d363d30545af43d82803e903d91601857fd5bf3"; | ||
* const ipfsHash = extractIPFSUri(bytecode); | ||
* console.log(ipfsHash); | ||
* ``` | ||
*/ | ||
export function extractIPFSUri(bytecode: string): string | undefined { | ||
const numericBytecode = hexToBytes( | ||
bytecode.startsWith("0x") ? bytecode.slice(2) : bytecode, | ||
); | ||
|
||
const cborLength = | ||
// @ts-expect-error - TS doesn't like this, but it's fine | ||
numericBytecode[numericBytecode.length - 2] * 0x100 + | ||
// @ts-expect-error - TS doesn't like this, but it's fine | ||
numericBytecode[numericBytecode.length - 1]; | ||
const cborStart = numericBytecode.length - 2 - cborLength; | ||
// if the cborStart is invalid, return undefined | ||
if (cborStart < 0 || cborStart > numericBytecode.length) { | ||
return undefined; | ||
} | ||
const bytecodeBuffer = numericBytecode.slice(cborStart, -2); | ||
|
||
const cborData = decode(bytecodeBuffer); | ||
if ("ipfs" in cborData) { | ||
return `ipfs://${encode(cborData["ipfs"])}`; | ||
} | ||
|
||
return undefined; | ||
} |
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,13 @@ | ||
// bytecode | ||
export { extractIPFSUri } from "./bytecode/extractIPFS.js"; | ||
export { detectMethod } from "./bytecode/detectExtension.js"; | ||
|
||
// units | ||
export { | ||
formatEther, | ||
formatGwei, | ||
formatUnits, | ||
parseEther, | ||
parseGwei, | ||
parseUnits, | ||
} from "./units.js"; |
Oops, something went wrong.