-
Notifications
You must be signed in to change notification settings - Fork 42
Feat/superhero ID #3627
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
Draft
alex-k8
wants to merge
14
commits into
develop
Choose a base branch
from
feat/superhero-id
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Feat/superhero ID #3627
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9963d8a
feat: superhero id initial commit
akalogerakisunicorn a7a636f
feat: sync superhero id only once
akalogerakisunicorn ccceb57
feat: sign superhero id transaction
akalogerakisunicorn 4e17079
feat: add sign transaction to ceate superhero id
akalogerakisunicorn 3180b18
feat: enforce superhero id on caller
akalogerakisunicorn 973ccf0
feat: use first ae account as superhero id
akalogerakisunicorn 0dffa4c
fix: call onConnect correctly
akalogerakisunicorn 74aebb5
feat: superhero id composable
akalogerakisunicorn be69336
feat: superhero id error handling
akalogerakisunicorn a0ae47c
feat: superhero id translations and refactoring
akalogerakisunicorn 8be7f67
feat: cleanup superhero id service code
akalogerakisunicorn ac505f7
feat: more generic contract to support multiple keys
akalogerakisunicorn 6a420e2
feat: superhero id sync currency
akalogerakisunicorn 50f2aaa
fix: failing tests
akalogerakisunicorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,31 @@ | ||
| contract SuperheroID = | ||
| record state = | ||
| { data : map(address, map(string, string)) } | ||
|
|
||
| entrypoint init() = | ||
| { data = {} } | ||
|
|
||
| stateful entrypoint set_data(key : string, value : string) = | ||
| let user_data = Map.lookup_default(Call.caller, state.data, {}) | ||
| let new_user_data = user_data{ [key] = value } | ||
| put(state{ data[Call.caller] = new_user_data }) | ||
|
|
||
| entrypoint get_data(key : string) : option(string) = | ||
| switch(Map.lookup(Call.caller, state.data)) | ||
| None => None | ||
| Some(user_data) => Map.lookup(key, user_data) | ||
|
|
||
| entrypoint has_data() : bool = | ||
| Map.member(Call.caller, state.data) | ||
|
|
||
| entrypoint has_key(key : string) : bool = | ||
| switch(Map.lookup(Call.caller, state.data)) | ||
| None => false | ||
| Some(user_data) => Map.member(key, user_data) | ||
|
|
||
| stateful entrypoint delete_key(key : string) = | ||
| switch(Map.lookup(Call.caller, state.data)) | ||
| None => () | ||
| Some(user_data) => | ||
| let new_user_data = Map.delete(key, user_data) | ||
| put(state{ data[Call.caller] = new_user_data }) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,151 @@ | ||
| import { | ||
| Ref, | ||
| ref, | ||
| watch, | ||
| computed, | ||
| } from 'vue'; | ||
| import { SuperheroIDService } from '@/protocols/aeternity/libs/SuperheroIDService'; | ||
| import type { Encoded } from '@aeternity/aepp-sdk'; | ||
| import { useAccounts } from '@/composables/accounts'; | ||
| import { useAeSdk } from '@/composables/aeSdk'; | ||
| import { useModals } from '@/composables/modals'; | ||
| import { MODAL_CONFIRM_TRANSACTION_SIGN, PROTOCOLS } from '@/constants'; | ||
| import { unpackTx } from '@aeternity/aepp-sdk'; | ||
| import { useNetworks } from '@/composables/networks'; | ||
| import { useAddressBook } from '@/composables/addressBook'; | ||
| import { useCurrencies } from '@/composables/currencies'; | ||
| import { CurrencyCode, ITx } from '@/types'; | ||
|
|
||
| const superheroSvcRef: Ref<SuperheroIDService | null> = ref(null); | ||
| const hasSuperheroIdRef = ref(false); | ||
|
|
||
| export function useSuperheroId(): { | ||
| hasSuperheroId: Ref<boolean>; | ||
| syncAddressBook: (json: string) => Promise<void>; | ||
| syncSettings: (json: string) => Promise<void>; | ||
| loadAddressBook: () => Promise<void>; | ||
| loadSettings: () => Promise<void>; | ||
| deployContract: () => Promise<string>; | ||
| } { // eslint-disable-line | ||
| const { aeAccounts } = useAccounts(); | ||
| const { getAeSdk } = useAeSdk(); | ||
| const { openModal } = useModals(); | ||
| const { addAddressBookEntriesFromJson } = useAddressBook(); | ||
|
|
||
| const firstAeAddress = computed(() => ( | ||
| aeAccounts.value?.[0]?.address as Encoded.AccountAddress | undefined)); | ||
|
|
||
| function getService(): SuperheroIDService { | ||
| if (!(superheroSvcRef.value instanceof SuperheroIDService)) { | ||
| superheroSvcRef.value = new SuperheroIDService(); | ||
| } | ||
| return superheroSvcRef.value; | ||
| } | ||
| // Settings (currency) integration (manual sync only) | ||
| const { currentCurrencyCode, setCurrentCurrency } = useCurrencies(); | ||
|
|
||
| async function loadSettings() { | ||
| const addr = firstAeAddress.value; | ||
| if (!addr) return; | ||
| const svc = getService(); | ||
| const val = await svc.getData('settings'); | ||
| if (!val) return; | ||
| try { | ||
| const parsed = JSON.parse(val) as { currency?: CurrencyCode }; | ||
| if (parsed?.currency && parsed.currency !== currentCurrencyCode.value) { | ||
| setCurrentCurrency(parsed.currency as CurrencyCode); | ||
| } | ||
| } catch { /* NOOP */ } | ||
| } | ||
|
|
||
| async function refreshHasSuperheroId(): Promise<void> { | ||
| const addr = firstAeAddress.value; | ||
| if (!addr) { | ||
| hasSuperheroIdRef.value = false; | ||
| return; | ||
| } | ||
| const svc = getService(); | ||
| try { | ||
| hasSuperheroIdRef.value = await svc.hasAnyData(); | ||
| } catch { | ||
| hasSuperheroIdRef.value = false; | ||
| } | ||
| } | ||
|
|
||
| async function syncData(domain: string, json: string): Promise<void> { | ||
| const addr = firstAeAddress.value; | ||
| if (!addr) throw new Error('No æternity account'); | ||
| const svc = getService(); | ||
| const txBase64 = await svc.buildSetDataTx(domain, json) as Encoded.Transaction; | ||
| const tx = unpackTx(txBase64) as unknown as ITx; | ||
| await openModal(MODAL_CONFIRM_TRANSACTION_SIGN, { | ||
| txBase64, | ||
| tx, | ||
| protocol: PROTOCOLS.aeternity, | ||
| app: { host: window.location.origin, href: window.location.origin }, | ||
| }); | ||
| const aeSdk = await getAeSdk(); | ||
| const signed = await aeSdk | ||
| .signTransaction(txBase64, { fromAccount: addr } as any) as Encoded.Transaction; | ||
| const { txHash } = await aeSdk.api.postTransaction({ tx: signed as Encoded.Transaction }); | ||
| await aeSdk.poll(txHash); | ||
| hasSuperheroIdRef.value = true; | ||
| } | ||
|
|
||
| async function syncAddressBook(json: string): Promise<void> { | ||
| return syncData('address_book', json); | ||
| } | ||
|
|
||
| async function syncSettings(json: string): Promise<void> { | ||
| return syncData('settings', json); | ||
| } | ||
|
|
||
| async function loadAddressBook() { | ||
| const addr = firstAeAddress.value; | ||
| if (!addr) throw new Error('No æternity account'); | ||
| const svc = getService(); | ||
| const val = await svc.getData('address_book'); | ||
| hasSuperheroIdRef.value = !!val; | ||
| if (val) { | ||
| addAddressBookEntriesFromJson(val, false); | ||
| } | ||
| } | ||
|
|
||
| async function deployContract(): Promise<string> { | ||
| const svc = getService(); | ||
| const res = await fetch('/contracts/SuperheroIds.aes'); | ||
| const source = await res.text(); | ||
| return svc.deployFromSource(source); | ||
| } | ||
|
|
||
| // Re-init on network change if an AE account exists | ||
| const { onNetworkChange } = useNetworks(); | ||
| onNetworkChange(async () => { | ||
| await refreshHasSuperheroId(); | ||
| await loadSettings(); | ||
| }); | ||
|
|
||
| // Auto-initialize service when first AE account is available; clear on removal | ||
| watch( | ||
| () => firstAeAddress.value, | ||
| async (addr) => { | ||
| if (addr) { | ||
| await refreshHasSuperheroId(); | ||
| await loadSettings(); | ||
| } else { | ||
| superheroSvcRef.value = null; | ||
| hasSuperheroIdRef.value = false; | ||
| } | ||
| }, | ||
| { immediate: true }, | ||
| ); | ||
|
|
||
| return { | ||
| hasSuperheroId: hasSuperheroIdRef, | ||
| syncAddressBook, | ||
| syncSettings, | ||
| loadAddressBook, | ||
| loadSettings, | ||
| deployContract, | ||
| }; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, the http compiler is not for production use, it's better to precompile the contract on build using CompilerCli
I would use https://github.com/aeternity/contract-builder, but it is not maintained currently