-
Notifications
You must be signed in to change notification settings - Fork 9
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 #22 from decentldotland/prep
prep: integrate Fuel Network
- Loading branch information
Showing
8 changed files
with
121 additions
and
3 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
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"); | ||
} | ||
} | ||
} | ||
|
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,7 @@ | ||
{ | ||
"names": {}, | ||
"signatures": [], | ||
"verification_message": "hello", | ||
"fuel_molecule_endpoint": "http://fuel.molecule.sh" | ||
} | ||
|
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,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 | ||
``` | ||
|
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
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 }; | ||
} | ||
} |
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