Skip to content

Commit 266b450

Browse files
leocourbassierluizstacioLuizAsFight
authored
feat: solana connector (#137)
Co-authored-by: Luiz Estácio | stacio.eth <luizstacio@gmail.com> Co-authored-by: Luiz Gomes <8636507+LuizAsFight@users.noreply.github.com>
1 parent 481b5fe commit 266b450

39 files changed

+2916
-166
lines changed

.changeset/young-tigers-shop.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@fuel-connectors/walletconnect-connector": minor
3+
"@fuel-connectors/solana-connector": minor
4+
---
5+
6+
Added Solana Connector using Wallet's Connect web3modal provider.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ yarn-error.log*
1717

1818
# Misc
1919
.DS_Store
20+
.zed

examples/react-app/src/main.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import {
1414
FuelWalletConnector,
1515
FuelWalletDevelopmentConnector,
1616
FueletWalletConnector,
17+
SolanaConnector,
18+
WalletConnectConnector,
1719
} from '@fuels/connectors';
18-
import { WalletConnectConnector } from '@fuels/connectors/walletconnect';
1920
import { FuelProvider } from '@fuels/react';
2021

2122
import * as Toast from '@radix-ui/react-toast';
@@ -75,6 +76,9 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
7576
}),
7677
new FuelWalletDevelopmentConnector(),
7778
new BurnerWalletConnector(),
79+
new SolanaConnector({
80+
projectId: WC_PROJECT_ID,
81+
}),
7882
],
7983
}}
8084
>

examples/react-next/src/components/Providers.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
FuelWalletConnector,
99
FuelWalletDevelopmentConnector,
1010
FueletWalletConnector,
11+
SolanaConnector,
12+
WalletConnectConnector,
1113
} from '@fuels/connectors';
12-
import { WalletConnectConnector } from '@fuels/connectors/walletconnect';
1314
import { FuelProvider } from '@fuels/react';
1415

1516
const queryClient = new QueryClient();
@@ -26,6 +27,7 @@ export const Providers = ({ children }: { children: React.ReactNode }) => {
2627
new BakoSafeConnector(),
2728
new FueletWalletConnector(),
2829
new WalletConnectConnector(),
30+
new SolanaConnector(),
2931
],
3032
}}
3133
>

packages/connectors/package.json

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@
99
"require": "./dist/index.js",
1010
"import": "./dist/index.mjs",
1111
"types": "./dist/index.d.ts"
12-
},
13-
"./walletconnect": {
14-
"require": "./dist/walletconnect-connector/index.js",
15-
"import": "./dist/walletconnect-connector/index.mjs",
16-
"types": "./dist/walletconnect-connector/src/index.d.ts"
17-
},
18-
"./evm": {
19-
"require": "./dist/evm-connector/index.js",
20-
"import": "./dist/evm-connector/index.mjs",
21-
"types": "./dist/evm-connector/src/index.d.ts"
2212
}
2313
},
2414
"files": ["dist"],
@@ -34,9 +24,14 @@
3424
"@ethereumjs/util": "9.0.3",
3525
"@ethersproject/bytes": "5.7.0",
3626
"@wagmi/core": "2.9.1",
37-
"@web3modal/wagmi": "4.1.11",
27+
"@web3modal/wagmi": "5.0.0",
3828
"viem": "2.10.2",
39-
"socket.io-client": "4.7.2"
29+
"socket.io-client": "4.7.2",
30+
"@web3modal/solana": "5.0.0",
31+
"@web3modal/scaffold": "5.0.0",
32+
"@web3modal/core": "5.0.0",
33+
"@solana/web3.js": "1.93.1",
34+
"rpc-websockets": "7.11.0"
4035
},
4136
"devDependencies": {
4237
"@fuel-connectors/burner-wallet-connector": "workspace:*",
@@ -46,6 +41,7 @@
4641
"@fuel-connectors/fuelet-wallet": "workspace:*",
4742
"@fuel-connectors/walletconnect-connector": "workspace:*",
4843
"@fuel-connectors/bako-safe": "workspace:*",
44+
"@fuel-connectors/solana-connector": "workspace:*",
4945
"fuels": "0.88.1",
5046
"terser": "5.31.0",
5147
"tsup": "8.0.2",

packages/connectors/src/defaultConnectors.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@ import { WalletConnectConnector } from '@fuel-connectors/walletconnect-connector
77
import type { FuelConnector } from 'fuels';
88
import type { BurnerWalletConfig } from '../../burner-wallet-connector/src/types';
99

10+
const DEFAULT_WC_PROJECT_ID = '00000000000000000000000000000000';
11+
1012
type DefaultConnectors = {
1113
devMode?: boolean;
14+
wcProjectId?: string;
1215
burnerWalletConfig?: BurnerWalletConfig;
1316
};
1417

1518
export function defaultConnectors({
1619
devMode,
20+
wcProjectId = DEFAULT_WC_PROJECT_ID,
1721
burnerWalletConfig,
1822
}: DefaultConnectors = {}): Array<FuelConnector> {
1923
const connectors = [
2024
new FuelWalletConnector(),
2125
new BakoSafeConnector(),
2226
new FueletWalletConnector(),
23-
new WalletConnectConnector(),
27+
new WalletConnectConnector({
28+
projectId: wcProjectId,
29+
}),
2430
new BurnerWalletConnector(burnerWalletConfig),
2531
];
2632

packages/connectors/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from '@fuel-connectors/fuelet-wallet';
55
export * from '@fuel-connectors/bako-safe';
66
export * from '@fuel-connectors/burner-wallet-connector';
77
export * from '@fuel-connectors/walletconnect-connector';
8+
export * from '@fuel-connectors/solana-connector';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@fuel-connectors/solana-connector';

packages/connectors/tsup.config.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { defineConfig } from 'tsup';
33

44
export default defineConfig((options) => ({
55
...baseConfig(options, { withReact: false }),
6-
entry: [
7-
'src/index.ts',
8-
'src/walletconnect-connector/index.ts',
9-
'src/evm-connector/index.ts',
10-
],
6+
entry: ['src/index.ts'],
117
external: ['fuels'],
128
noExternal: [
139
'@fuel-connectors/fuel-development-wallet',
@@ -17,6 +13,7 @@ export default defineConfig((options) => ({
1713
'@fuel-connectors/evm-connector',
1814
'@fuel-connectors/walletconnect-connector',
1915
'@fuel-connectors/bako-safe',
16+
'@fuel-connectors/solana-connector',
2017
],
2118
minify: 'terser',
2219
dts: {
@@ -28,6 +25,7 @@ export default defineConfig((options) => ({
2825
'@fuel-connectors/evm-connector',
2926
'@fuel-connectors/walletconnect-connector',
3027
'@fuel-connectors/bako-safe',
28+
'@fuel-connectors/solana-connector',
3129
],
3230
},
3331
splitting: true,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "@fuel-connectors/solana-connector",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"files": ["dist"],
7+
"main": "./dist/wallet-connector-solana.umd.cjs",
8+
"module": "./dist/wallet-connector-solana.js",
9+
"exports": {
10+
".": {
11+
"import": "./dist/wallet-connector-solana.js",
12+
"require": "./dist/wallet-connector-solana.umd.cjs"
13+
}
14+
},
15+
"types": "./dist/index.d.ts",
16+
"scripts": {
17+
"build:forc": "pnpm fuels-forc build --release --path ./predicate",
18+
"build:resources": "pnpm run build:forc && tsx ./scripts/generatePredicateResources.ts",
19+
"build": "tsup --dts-only",
20+
"build:watch": "pnpm build --watch",
21+
"ts:check": "tsc --noEmit",
22+
"test": "vitest"
23+
},
24+
"peerDependencies": {
25+
"@web3modal/scaffold": ">=5.0.0",
26+
"@web3modal/core": ">=5.0.0",
27+
"fuels": ">=0.88.1"
28+
},
29+
"dependencies": {
30+
"@ethereumjs/util": "9.0.3",
31+
"@ethersproject/bytes": "5.7.0",
32+
"@solana/web3.js": "1.93.1",
33+
"@web3modal/solana": "5.0.0",
34+
"bs58": "6.0.0",
35+
"memoizee": "0.4.15",
36+
"tweetnacl": "1.0.3"
37+
},
38+
"devDependencies": {
39+
"@fuel-ts/account": "0.88.1",
40+
"@fuel-ts/forc": "0.88.1",
41+
"@fuel-ts/fuel-core": "0.88.1",
42+
"@types/memoizee": "0.4.11",
43+
"fuels": "0.88.1",
44+
"jsdom": "24.0.0",
45+
"ts-loader": "9.5.1",
46+
"ts-node": "10.9.2",
47+
"tsup": "8.0.2",
48+
"tsx": "4.9.3",
49+
"typescript": "5.4.5",
50+
"vite": "5.2.11",
51+
"vite-plugin-dts": "3.9.1",
52+
"vitest": "1.6.0",
53+
"@web3modal/core": "5.0.0"
54+
}
55+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[package]]
2+
name = "core"
3+
source = "path+from-root-148AAAB4460F1A9D"
4+
5+
[[package]]
6+
name = "std"
7+
source = "git+https://github.com/fuellabs/sway?tag=v0.59.0#d9985d8111f94235edba9a08fc71a9513ec2a95c"
8+
dependencies = ["core"]
9+
10+
[[package]]
11+
name = "verification-predicate"
12+
source = "member"
13+
dependencies = ["std"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[project]
2+
authors = ["Fuel Labs <contact@fuel.sh>"]
3+
entry = "main.sw"
4+
license = "Apache-2.0"
5+
name = "verification-predicate"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[toolchain]
2+
channel = "testnet"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"encoding": "1",
3+
"types": [
4+
{
5+
"typeId": 0,
6+
"type": "b256",
7+
"components": null,
8+
"typeParameters": null
9+
},
10+
{
11+
"typeId": 1,
12+
"type": "bool",
13+
"components": null,
14+
"typeParameters": null
15+
},
16+
{
17+
"typeId": 2,
18+
"type": "u64",
19+
"components": null,
20+
"typeParameters": null
21+
}
22+
],
23+
"functions": [
24+
{
25+
"inputs": [
26+
{
27+
"name": "witness_index",
28+
"type": 2,
29+
"typeArguments": null
30+
}
31+
],
32+
"name": "main",
33+
"output": {
34+
"name": "",
35+
"type": 1,
36+
"typeArguments": null
37+
},
38+
"attributes": null
39+
}
40+
],
41+
"loggedTypes": [],
42+
"messagesTypes": [],
43+
"configurables": [
44+
{
45+
"name": "SIGNER",
46+
"configurableType": {
47+
"name": "",
48+
"type": 0,
49+
"typeArguments": null
50+
},
51+
"offset": 2568
52+
}
53+
]
54+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x8f562b4f23ce90b0547d11e4e02462d3cd660f4f3fe5f719ae725eab4d7a5d42
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
predicate;
2+
3+
use std::{
4+
b512::B512,
5+
constants::ZERO_B256,
6+
hash::{
7+
Hash,
8+
sha256,
9+
},
10+
ecr::{
11+
EcRecoverError,
12+
ed_verify,
13+
},
14+
tx::{
15+
tx_id,
16+
tx_witness_data,
17+
},
18+
bytes::Bytes,
19+
};
20+
21+
const ASCII_MAP: [u8; 16] = [
22+
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102
23+
];
24+
25+
pub fn create_txid_small(tx_id: b256) -> b256 {
26+
let mut ascii_bytes = Bytes::with_capacity(32);
27+
let bytes_ascii = b256_to_ascii_bytes(tx_id);
28+
let (begin, rest) = bytes_ascii.split_at(16);
29+
let (_rest, end) = rest.split_at(32);
30+
31+
ascii_bytes.append(begin);
32+
ascii_bytes.append(end);
33+
34+
let small_txId: b256 = ascii_bytes.into();
35+
small_txId
36+
}
37+
38+
pub fn b256_to_ascii_bytes(val: b256) -> Bytes {
39+
let bytes = Bytes::from(val);
40+
let mut ascii_bytes = Bytes::with_capacity(64);
41+
let mut idx = 0;
42+
43+
while idx < 32 {
44+
let b = bytes.get(idx).unwrap();
45+
ascii_bytes.push(ASCII_MAP[(b >> 4).as_u64()]);
46+
ascii_bytes.push(ASCII_MAP[(b & 15).as_u64()]);
47+
idx = idx + 1;
48+
}
49+
50+
ascii_bytes
51+
}
52+
53+
configurable {
54+
SIGNER: b256 = ZERO_B256,
55+
}
56+
57+
fn main(witness_index: u64) -> bool {
58+
let signature: B512 = tx_witness_data(witness_index);
59+
let encoded = create_txid_small(tx_id());
60+
let result = ed_verify(SIGNER, signature, encoded);
61+
62+
if result.is_ok() {
63+
return true;
64+
}
65+
66+
// Otherwise, an invalid signature has been passed and we invalidate the Tx.
67+
false
68+
}

0 commit comments

Comments
 (0)