Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: create common library for predicated connectors #148

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
6feb86b
feat: create new common pkg with helper functions
leonardocbsr Jul 2, 2024
d9f2614
feature: create common packages with reusable predicated connectors
leonardocbsr Jul 9, 2024
87ff6d3
Merge remote-tracking branch 'upstream/main' into refactor/create-com…
leonardocbsr Jul 12, 2024
fab8a27
fix: update pnpm-lock
leonardocbsr Jul 12, 2024
5501e5a
Merge remote-tracking branch 'upstream/main' into refactor/create-com…
leonardocbsr Jul 12, 2024
6ca3f26
refactor: use common package in the solana connector
leonardocbsr Jul 13, 2024
00b4f5b
test: add tests for predicatedconnector
leonardocbsr Jul 13, 2024
d642b0e
chore: update turbo.json
leonardocbsr Jul 13, 2024
7fcdbaa
chore: update turbo.json
leonardocbsr Jul 13, 2024
06d491f
test: remove failing tests
leonardocbsr Jul 13, 2024
1dbe94f
fix: remove unneeded vite config
leonardocbsr Jul 13, 2024
fd38955
fix: update vite.config.ts
leonardocbsr Jul 13, 2024
2b6c6a3
tests: fix EVM failing tests
leonardocbsr Jul 13, 2024
2644928
fix: remove /common from test file
leonardocbsr Jul 13, 2024
e454c07
test: change test environment
leonardocbsr Jul 13, 2024
2c01ed2
doc: add workspaces to package.json
leonardocbsr Jul 13, 2024
db5657d
test: add test.deps to vite.config
leonardocbsr Jul 13, 2024
2a54d33
chore: tweak tsconfig,viteconfig
leonardocbsr Jul 13, 2024
0088eb3
test: improve setupPredicate test
leonardocbsr Jul 13, 2024
a34889f
chore: bump vitest
leonardocbsr Jul 13, 2024
6d0d55f
chore: update package.json
leonardocbsr Jul 13, 2024
e7817cf
fix: make ci's test depend on build
leonardocbsr Jul 17, 2024
dd845f0
Merge remote-tracking branch 'upstream/main' into refactor/create-com…
leonardocbsr Jul 17, 2024
09e20cc
fix: update pnpm-lock
leonardocbsr Jul 17, 2024
e16d2e0
chore: update with main branch changes
leonardocbsr Jul 17, 2024
d82fbfc
chore: bump vitest
leonardocbsr Jul 17, 2024
0763859
Merge remote-tracking branch 'upstream/main' into refactor/create-com…
leonardocbsr Jul 31, 2024
83b4259
feat: bump fuels version
leonardocbsr Jul 31, 2024
12240a6
chore: remove redundancy from wagmiConfig
leonardocbsr Aug 1, 2024
c76f52f
chore: remove version from predicate
leonardocbsr Aug 1, 2024
2d6b665
fix: broken tests
leonardocbsr Aug 1, 2024
ffd191b
chore: fix predicateConnector broken test
leonardocbsr Aug 1, 2024
b680a38
test: fix evm test
leonardocbsr Aug 1, 2024
681205d
chore: fix solana tests
leonardocbsr Aug 1, 2024
68e9fdb
chore: fix evm test
leonardocbsr Aug 1, 2024
5b9c363
test: wait for setupPredicate
leonardocbsr Aug 1, 2024
1aabc43
Merge remote-tracking branch 'upstream/main' into refactor/create-com…
leonardocbsr Aug 1, 2024
3e70045
feat: predicate version using fuels:typegen
leonardocbsr Aug 2, 2024
1227fcf
feat: use OutputChange to newest predicate
leonardocbsr Aug 2, 2024
c62e9ca
fix: remove packageManager
leonardocbsr Aug 2, 2024
c35961a
test: ignore stopfn
leonardocbsr Aug 2, 2024
3476bb1
chore: bump turbo
leonardocbsr Aug 2, 2024
1bb9e92
chore: add packageManager matching actions version
leonardocbsr Aug 2, 2024
c7aff34
chore: update tsup from common
leocourbassier Aug 2, 2024
6fce9cf
chore: change output address when set
leonardocbsr Aug 5, 2024
4e3afac
chore: do not overwrite existing predicate in scripts
leocourbassier Aug 7, 2024
cd1363f
chore: review code readability improvements
leonardocbsr Aug 7, 2024
861b57d
Merge remote-tracking branch 'upstream/main' into refactor/create-com…
leonardocbsr Aug 7, 2024
19c4810
chore: update pnpm
leonardocbsr Aug 7, 2024
65a6b83
chore: update turbo to make ts:check depend on build
leonardocbsr Aug 7, 2024
96a5cd6
chore: remove confusing typegen
leonardocbsr Aug 7, 2024
4bd928d
chore: invert logic from prepareTransaction
leonardocbsr Aug 7, 2024
13d5a21
chore: change addr to b256
leonardocbsr Aug 8, 2024
9d1159a
chore: convert address to b256
leonardocbsr Aug 8, 2024
069728a
chore: recover prev predicates
leonardocbsr Aug 8, 2024
44157e0
Merge remote-tracking branch 'upstream/main' into refactor/create-com…
leonardocbsr Aug 8, 2024
a6b2e42
chore: fix wrong address comparing
leonardocbsr Aug 8, 2024
612bcca
chore: change automatically after successful tx
leonardocbsr Aug 8, 2024
4b88e2e
Merge branch 'main' into refactor/create-common-library
LuizAsFight Aug 8, 2024
2c6ac38
chore: start from the oldest predicate first
leonardocbsr Aug 9, 2024
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
8 changes: 8 additions & 0 deletions .changeset/mean-yaks-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@fuel-connectors/walletconnect-connector": minor
"@fuel-connectors/solana-connector": minor
"@fuel-connectors/evm-connector": minor
"@fuel-connectors/common": minor
---

Added a new package with helper classes/functions for predicated connectors since they share a fair ammount of core logic.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ yarn-error.log*
# Misc
.DS_Store
.zed

# Sway
**/out/release
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
save-exact = true
enable-pre-post-scripts = true
1 change: 1 addition & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"**/.turbo",
"**/build",
"**/contracts",
"**/examples/react-next/.next",
"pnpm-lock.yaml",
"**/examples/react-app/src/types",
"**/examples/react-next/.next",
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"@changesets/cli": "2.27.1",
"@fuels/ts-config": "0.20.0",
"@fuels/tsup-config": "0.20.0",
"@vitest/coverage-v8": "1.6.0",
"@vitest/coverage-v8": "2.0.2",
"compare-versions": "6.1.0",
"husky": "9.0.11",
"lint-staged": "15.2.2",
"tsx": "^4.16.2",
"turbo": "1.13.3",
"vitest": "1.6.0"
"turbo": "2.0.11",
"vitest": "2.0.2"
},
"engines": {
"node": ">=18",
Expand All @@ -45,5 +45,6 @@
"ws@>=6.0.0 <6.2.3": ">=6.2.3",
"fast-xml-parser@<4.4.1": ">=4.4.1"
}
}
},
"packageManager": "[email protected]"
}
2 changes: 1 addition & 1 deletion packages/burner-wallet-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"typescript": "5.4.5",
"vite": "5.2.11",
"vite-plugin-dts": "3.9.1",
"vitest": "1.6.0"
"vitest": "2.0.2"
}
}
44 changes: 44 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@fuel-connectors/common",
"version": "0.0.1",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./scripts": {
"require": "./dist/scripts/index.js",
"import": "./dist/scripts/index.mjs",
"types": "./dist/scripts/index.d.ts"
}
},
"files": ["dist"],
"scripts": {
"build": "tsup --dts",
"ts:check": "tsc --noEmit",
"test": "vitest"
},
"peerDependencies": {
"fuels": ">=0.93.0"
},
"dependencies": {
"@ethersproject/bytes": "5.7.0",
"@solana/web3.js": "1.93.2",
"bs58": "6.0.0",
"memoizee": "0.4.15",
"tweetnacl": "1.0.3",
"viem": "2.19.1"
},
"devDependencies": {
"@types/memoizee": "0.4.11",
"fuels": "0.93.0",
"terser": "5.31.0",
"tsup": "8.0.2",
"typescript": "5.4.5",
"vitest": "2.0.2"
}
}
1 change: 1 addition & 0 deletions packages/common/scripts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './versions';
105 changes: 105 additions & 0 deletions packages/common/scripts/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import fs from 'node:fs';
import { getPredicateRoot } from 'fuels';

// When running pnpm fuels:build, the build script creates files
// differently than when running pnpm fuels:typegen
export const checkForcBuild = async (__dirname: string) => {
const predicatePath = `${__dirname}/factories`;
if (!fs.existsSync(predicatePath)) {
console.info('No new predicate found. Ignoring...');
return;
}

console.info('New predicate found. Moving to new folder...');
const rightPath = `${__dirname}/new`;
if (fs.existsSync(rightPath)) await addPredicate(__dirname);

fs.mkdirSync(rightPath);
fs.renameSync(predicatePath, `${rightPath}/factories`);
fs.writeFileSync(
`${rightPath}/index.ts`,
`export { VerificationPredicateAbi__factory } from './factories/VerificationPredicateAbi__factory';`,
);
console.info('Predicate moved to new folder.');
};

export const addPredicate = async (__dirname: string) => {
const predicatePath = `${__dirname}/new`;
if (!fs.existsSync(predicatePath)) {
console.info('No new predicate found. Ignoring...');
return;
}

const { VerificationPredicateAbi__factory: predicate } = await import(
predicatePath
);
const version = getPredicateRoot(predicate.bin);
const newPath = `${__dirname}/${version}`;
if (fs.existsSync(newPath)) {
console.warn(`Predicate already exists: ${version}`);
console.warn('Removing new folder..');
fs.rmSync(predicatePath, { recursive: true, force: true });
return;
}

fs.mkdirSync(newPath);

const date = new Date();
const code = [
'/* Autogenerated file. Do not edit manually. */',
'',
`export const generationDate = ${date.getTime().toString()};`,
'// biome-ignore lint: Autogenerated file',
`export const abi = ${JSON.stringify(predicate.abi)};`,
`export const bin = '${predicate.bin}';`,
];

fs.appendFileSync(`${newPath}/index.ts`, code.join('\n'));
fs.rmSync(predicatePath, { recursive: true, force: true });
console.info(`Predicate added: ${version}`);
};

export const syncPredicate = async (__dirname: string) => {
const dictPath = `${__dirname}/index.ts`;
const header =
"/* Autogenerated file. Do not edit manually. */\n\nimport type { PredicateVersion } from '@fuel-connectors/common';\n";
fs.writeFileSync(dictPath, header);

const versions = await Promise.all(
fs
.readdirSync(__dirname)
.filter((f) => !f.includes('.ts'))
.map(async (f) => {
const { generationDate, abi, bin } = await import(
`${__dirname}/${f}/index.ts`
);
return { abi, bin, date: generationDate, version: f };
}),
);
if (versions.length === 0) {
console.warn('No predicates found.');
return;
}

const headers = versions.map(
(v) =>
`import { abi as abi${v.date}, bin as bin${v.date}, generationDate as generationDate${v.date}} from './${v.version}';`,
);

const code = `\n\nexport const PREDICATE_VERSIONS = {\n${versions
.map(
(v) =>
`\t'${v.version}':{ predicate: {abi: abi${v.date}, bin: bin${v.date}}, generatedAt: generationDate${v.date} }`,
)
.join(',\n')}
} as Record<string, PredicateVersion>;\n`;
fs.appendFileSync(dictPath, `${headers.join('\n')}${code}`);
console.info('Dictionary updated.');
};

export const generateVersions = async (__dirname: string) => {
await checkForcBuild(__dirname);
await addPredicate(__dirname);
await syncPredicate(__dirname);
console.info('Done.');
};
68 changes: 68 additions & 0 deletions packages/common/src/FakeAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Keypair } from '@solana/web3.js';
import nacl from 'tweetnacl';
import { type PrivateKeyAccount, privateKeyToAccount } from 'viem/accounts';
import type { Hash, Option } from './types';

export interface FakeAccount {
generate: () => void;
getAddress: () => Option<string, Hash>;
getRawAddress: () => Uint8Array;
signMessage: (
message: Option<string, Uint8Array, Hash>,
) => Promise<Option<string, Uint8Array, Hash>>;
}

export class EthereumFakeAccount implements FakeAccount {
private account!: PrivateKeyAccount;
constructor() {
this.generate();
}

generate = (): void => {
this.account = privateKeyToAccount(
'0x0000000000000000000000000000000000000000000000000000000000000001',
);
};

getAddress = (): Option<string, Hash> => this.account.address;
getRawAddress = (): Uint8Array =>
Buffer.from(this.account.address.slice(2), 'hex');

signMessage = (
message: Option<string, Uint8Array, Hash>,
): Promise<Option<string, Uint8Array, Hash>> => {
return this.account.signMessage({
message: message as string,
});
};
}

export class SolanaFakeAccount implements FakeAccount {
private keypair!: Keypair;
constructor() {
this.generate();
}

generate = (): void => {
this.keypair = Keypair.generate();
};

getAddress = (): Option<string, Hash> => this.keypair.publicKey.toString();
getRawAddress = (): Uint8Array => this.keypair.publicKey.toBytes();

private getSmallTxId = (
message: Option<string, Uint8Array, Hash>,
): Uint8Array => {
const txIdNo0x = message.slice(2);
const idBytes = `${txIdNo0x.slice(0, 16)}${txIdNo0x.slice(-16)}`;
return new TextEncoder().encode(idBytes);
};

signMessage = (
message: Option<string, Uint8Array, Hash>,
): Promise<Option<string, Uint8Array, Hash>> => {
return Promise.resolve(
nacl.sign.detached(this.getSmallTxId(message), this.keypair.secretKey),
);
};
}
Loading
Loading