-
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 #20 from decentldotland/prep
prep: `massa` molecule & `massa-auth` atom
- Loading branch information
Showing
11 changed files
with
119 additions
and
7 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 massa_molecule_endpoint = state.massa_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()}.massa`; | ||
signatures.push(signature); | ||
|
||
return { state }; | ||
} | ||
|
||
async function _moleculeSignatureVerification(caller, message, signature) { | ||
try { | ||
const isValid = await EXM.deterministicFetch( | ||
`${massa_molecule_endpoint}/massa-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 world", | ||
"massa_molecule_endpoint": "http://massa.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,25 @@ | ||
## About | ||
The EXM contract below is an example implementation of a contract that uses [Massa.net](https://massa.net) for contract's caller authentication (`action.caller`), empowered by `massa-auth` atom of `massa` molecule. | ||
|
||
#### Contract | ||
- Live deployment: [EDzDQDgaStQEGmuDH9X1DxQGY5o7F11Jyj4AYSFL9MI](https://api.exm.dev/read/EDzDQDgaStQEGmuDH9X1DxQGY5o7F11Jyj4AYSFL9MI) | ||
- 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 world` | ||
|
||
`signature` and caller (`publicKey`) should be both passed under base58 encoding format. | ||
|
||
#### EXM CLI | ||
```console | ||
exm function:write EDzDQDgaStQEGmuDH9X1DxQGY5o7F11Jyj4AYSFL9MI --input '{"function": "register", "name": "buildooor", "signature": "$MASSA_SIGNATURE", "caller": "$MASSA_PUBLIC_KEY"}' --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
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,14 @@ | ||
import { MOLECULE_API_ENDPOINT } from "../../../utils/constants.js" | ||
import axios from "axios"; | ||
|
||
export async function isMassaSigner(message, pubkey, signature) { | ||
try { | ||
const result = await axios.get( | ||
`${MOLECULE_API_ENDPOINT}/massa/${pubkey}/${message}/${signature}` | ||
); | ||
|
||
return result?.data;; | ||
} 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
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