Skip to content

Commit

Permalink
Merge pull request #22 from decentldotland/prep
Browse files Browse the repository at this point in the history
prep: integrate Fuel Network
  • Loading branch information
charmful0x authored Jan 8, 2023
2 parents ecc97a7 + c42ab56 commit b26b6e1
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 3 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ molecules/
└── ton/
├── └── ton-auth/
└── massa/
└── massa-auth
├── └── massa-auth
└── fuel/
└── fuel-auth
└── near/
└── └── n-view-state
└── ark/
Expand All @@ -76,6 +78,7 @@ molecules/
| Internet Protocol (`ICP`) | `icp.molecule.sh` | `icp-auth` | 🟩 |
| TON (`ton`) | `ton.molecule.sh` | `ton-auth` | 🟩 |
| Massa (`massa`) | `massa.molecule.sh` | `massa-auth` | 🟩 |
| Fuel Network (`fuel`) | `fuel.molecule.sh` | `fuel-auth` | 🟩 |
| NEAR (`near`) | `near.molecule.sh` | `n-view-state` | 🟩 |
| Ark Protocol (`ark`) | `ark.molecule.sh` | `state` `resolve` `soark/domain` | 🟩/🟨
| Randomization (`rand`) | `rand.molecule.sh` | `generate` | 🟩/🟨 |
Expand Down Expand Up @@ -114,9 +117,11 @@ The following EXM contracts integrate [molecule.sh](http://molecule.sh) atoms to

- Massa authentication: Simple name registry contract. [example](./examples/massa-signing/wtf.md)

- Fuel authentication: Simple name registry contract. [example](./examples/fuel-signing/wtf.md)

- Getting [Ark Protocol](https://ark.decent.land) identity object. [example](./examples/ark-resolving/wtf.md)

- GPT3 integration in a smart contract. [example](./examples/gpt3/wtf.md)
- GPT3 integration in a smart contract. [example](./examples/gpt3/wtf.md)

## License
This project is licensed under the [MIT License](./LICENSE)
Expand Down
42 changes: 42 additions & 0 deletions examples/fuel-signing/contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export async function handle(state, action) {
const input = action.input;

const names = state.names;
const signatures = state.signatures;
const verification_message = state.verification_message;
const fuel_molecule_endpoint = state.fuel_molecule_endpoint;

if (input.function === "register") {
const name = input.name;
const caller = input.caller;
const signature = input.signature;

ContractAssert(name.trim().length, "error invalid name");
ContractAssert(!(name in names), "name already registered");
ContractAssert(caller && signature, "missing required arguments");
ContractAssert(
!signatures.includes(signature),
"error signed message used"
);

const message = btoa(verification_message);
const res = await _moleculeSignatureVerification(caller, message, signature);
state.names[res.address] = `${name.trim()}.fuel`;
signatures.push(signature);

return { state };
}

async function _moleculeSignatureVerification(caller, message, signature) {
try {
const isValid = await EXM.deterministicFetch(
`${fuel_molecule_endpoint}/fuel-auth/${caller}/${message}/${signature}`
);
ContractAssert(isValid.asJSON()?.result, "unauthorized caller");
return isValid.asJSON();
} catch (error) {
throw new ContractError("molecule res error");
}
}
}

7 changes: 7 additions & 0 deletions examples/fuel-signing/contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"names": {},
"signatures": [],
"verification_message": "hello",
"fuel_molecule_endpoint": "http://fuel.molecule.sh"
}

24 changes: 24 additions & 0 deletions examples/fuel-signing/wtf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## About
The EXM contract below is an example implementation of a contract that uses [fuel.network](https://fuel.network) for contract's caller authentication (`action.caller`), empowered by `fuel-auth` atom of `fuel` molecule.

#### Contract
- Live deployment: [amrBq6BJaApgVy1Znz-T_v3egfF-ntwMQLD3U2B80Ik](https://api.exm.dev/read/amrBq6BJaApgVy1Znz-T_v3egfF-ntwMQLD3U2B80Ik)
- source code: [./contract.js](./contract.js)

## Prerequisites

- EXM SDK
```console
npm i -g @execution-machine/sdk
```

- EXM API token ID: visit [exm.dev](https://exm.dev)

## Iteracting with the contract
To register a `name` in the contract example, you have first to sign with your wallet the message used for verification in the contract's example which is `hello`

#### EXM CLI
```console
exm function:write amrBq6BJaApgVy1Znz-T_v3egfF-ntwMQLD3U2B80Ik --input '{"function": "register", "name": "buildooor", "signature": "$FUEL_SIGNATURE", "caller": "$FUEL_CALLER_ADDR"}' --token EXM_TOKEN_ID
```

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "molecule",
"version": "0.1.6",
"version": "0.1.7",
"type": "module",
"description": "reusable EXM components and helper functions for a faster development",
"main": "./src/api.js",
Expand Down
18 changes: 18 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isTrxSigner } from "./molecules/trx/atoms/verifySigner.js";
import { isIcpSigner } from "./molecules/icp/atoms/verifySigner.js";
import { isTonSigner } from "./molecules/ton/atoms/verifySigner.js";
import { isMassaSigner } from "./molecules/massa/atoms/verifySigner.js";
import { isFuelSigner } from "./molecules/fuel/atoms/verifySigner.js";
import { readNearOracleState } from "./molecules/near/atoms/read-contract.js";
import base64url from "base64url";

Expand Down Expand Up @@ -197,6 +198,23 @@ app.get("/massa-auth/:pubkey/:message/:signature", async (req, res) => {
}
});

app.get("/fuel-auth/:address/:message/:signature", async (req, res) => {
try {
res.setHeader("Content-Type", "application/json");

assert.equal(checkSubdomain(req, "fuel"), true);
const { address, message, signature } = req.params;
const response = await isFuelSigner(message, address, signature);
res.send(response);
return;
} catch (error) {
console.log(error)
res.send({ result: false, address: null });
return;
}
});


app.get("/n-view-state/:network/:address", async (req, res) => {
try {
res.setHeader("Content-Type", "application/json");
Expand Down
20 changes: 20 additions & 0 deletions src/molecules/fuel/atoms/verifySigner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FUEL_MOLECULE_ENDPOINT } from "../../../utils/constants.js";
import assert from "node:assert";
import axios from "axios";

export async function isFuelSigner(message, address, signature) {
try {
const result = (
await axios.get(
`${FUEL_MOLECULE_ENDPOINT}/verify/${signature}/${message}`
)
)?.data;
assert(
result?.address === address && result?.message === atob(message),
true
);
return { result: true, address: address };
} catch (error) {
return { result: false, address: null };
}
}
2 changes: 2 additions & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const MOLECULES = [
"ton",
"near",
"massa",
"fuel",
"substrate",
"rand",
];

export const MOLECULE_API_ENDPOINT = `https://molecule-apis-wrapper.herokuapp.com`;
export const FUEL_MOLECULE_ENDPOINT = `https://fuels-api.herokuapp.com`;

0 comments on commit b26b6e1

Please sign in to comment.