-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: the ability to connect qr based hardware wallet
- Loading branch information
1 parent
49da939
commit ce7c67e
Showing
20 changed files
with
1,443 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,101 @@ | ||
import { decode } from '@aeternity/aepp-sdk/es/tx/builder/helpers'; | ||
import { UR, URDecoder, UREncoder } from '@ngraveio/bc-ur'; | ||
import bs58check from 'bs58check'; | ||
import { IACMessageType, SerializerV3 } from '@airgap/serializer'; | ||
import type { | ||
AccountShareResponse, | ||
IACMessageDefinitionObjectV3, | ||
} from '@airgap/serializer'; | ||
import { AeternityAddress } from '@airgap/aeternity'; | ||
import type { IAccount, IDefaultComposableOptions } from '../types'; | ||
import { ACCOUNT_AIR_GAP_WALLET } from '../popup/utils'; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
export function useAirGap({ store }: IDefaultComposableOptions) { | ||
/** | ||
* Decodes a serialized data string that conforms to the "Uniform Resource" format (UR). | ||
* The function first decodes the UR string using the URDecoder class, then decodes the | ||
* resulting CBOR data, and finally deserializes the decoded data using a SerializerV3 instance. | ||
* | ||
* @param serializedData A string containing the serialized UR data to decode. | ||
* | ||
* @returns The deserialized data, | ||
* or null if the input data is not in the expected format or decoding fails. | ||
*/ | ||
async function decodeURSerializedData( | ||
serializedData: string, | ||
): Promise<IACMessageDefinitionObjectV3[]> { | ||
if (!serializedData.toUpperCase().startsWith('UR:')) { | ||
return []; | ||
} | ||
|
||
const decoder = new URDecoder(); | ||
decoder.receivePart(serializedData); | ||
|
||
if (!decoder.isComplete() || !decoder.isSuccess()) { | ||
return []; | ||
} | ||
|
||
const decoded = decoder.resultUR(); | ||
const combinedData = decoded.decodeCBOR(); | ||
const resultUr = bs58check.encode(combinedData); | ||
|
||
const serializer = SerializerV3.getInstance(); | ||
const parsedData: IACMessageDefinitionObjectV3[] = await serializer.deserialize(resultUr); | ||
|
||
return parsedData; | ||
} | ||
|
||
/** | ||
* Encodes an array of IAC message definition objects into a Uniform Resource (UR) format. | ||
*/ | ||
async function encodeIACMessageDefinitionObjects(data: IACMessageDefinitionObjectV3[]) { | ||
const serializer = SerializerV3.getInstance(); | ||
const serializedData = await serializer.serialize(data); | ||
|
||
const buffer = bs58check.decode(serializedData); | ||
const ur = UR.fromBuffer(buffer); | ||
const encoder = new UREncoder(ur); | ||
|
||
return encoder; | ||
} | ||
|
||
/** | ||
* Extracts shared addresses groups from encoded UR Data. | ||
* @param serializedData | ||
* @returns IAccount[] | ||
*/ | ||
async function extractAccountShareResponseData( | ||
serializedData: string, | ||
): Promise<IAccount[]> { | ||
const decodedData = await decodeURSerializedData(serializedData); | ||
|
||
if (!decodedData) { | ||
return []; | ||
} | ||
|
||
return decodedData | ||
.filter((item) => item.type === IACMessageType.AccountShareResponse) | ||
.map((item) => { | ||
const address = AeternityAddress.from( | ||
(item.payload as AccountShareResponse).publicKey, | ||
).asString(); | ||
const publicKey = Buffer.from(decode(address, 'ak')); | ||
|
||
return { | ||
address, | ||
publicKey, | ||
name: '', | ||
showed: true, | ||
type: ACCOUNT_AIR_GAP_WALLET, | ||
isOffline: true, | ||
} as IAccount; | ||
}); | ||
} | ||
|
||
return { | ||
decodeURSerializedData, | ||
encodeIACMessageDefinitionObjects, | ||
extractAccountShareResponseData, | ||
}; | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,75 @@ | ||
<template> | ||
<div class="account-import-row"> | ||
<AccountInfo | ||
:address="account.address" | ||
:name="account.name" | ||
:idx="account.idx" | ||
:is-air-gap="isAirGapAccount" | ||
/> | ||
|
||
<TokenAmount :amount="+numericBalance" /> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import BigNumber from 'bignumber.js'; | ||
import { | ||
computed, | ||
defineComponent, | ||
onMounted, | ||
ref, | ||
PropType, | ||
} from '@vue/composition-api'; | ||
import AccountInfo from './AccountInfo.vue'; | ||
import TokenAmount from './TokenAmount.vue'; | ||
import type { IAccount } from '../../types'; | ||
import { ROUTE_ACCOUNT_DETAILS } from '../router/routeNames'; | ||
import { useSdk } from '../../composables'; | ||
import { ACCOUNT_AIR_GAP_WALLET, aettosToAe } from '../utils'; | ||
export default defineComponent({ | ||
components: { | ||
AccountInfo, | ||
TokenAmount, | ||
}, | ||
props: { | ||
account: { type: Object as PropType<IAccount>, required: true }, | ||
}, | ||
setup(props, { root }) { | ||
const { getSdk } = useSdk({ store: root.$store }); | ||
const balance = ref(new BigNumber(0)); | ||
const numericBalance = computed<number>(() => balance.value.toNumber()); | ||
const isAirGapAccount = computed((): boolean => props.account.type === ACCOUNT_AIR_GAP_WALLET); | ||
onMounted(async () => { | ||
const sdk = await getSdk(); | ||
const fetchedBalance = await (sdk as any).balance(props.account.address); | ||
balance.value = new BigNumber(aettosToAe(fetchedBalance)); | ||
}); | ||
return { | ||
numericBalance, | ||
isAirGapAccount, | ||
ROUTE_ACCOUNT_DETAILS, | ||
}; | ||
}, | ||
}); | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
@use '../../styles/mixins'; | ||
.account-import-row { | ||
@include mixins.flex(space-between, center, row); | ||
::v-deep .token-amount { | ||
.amount, | ||
.fiat { | ||
display: block; | ||
text-align: right; | ||
} | ||
} | ||
} | ||
</style> |
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
Oops, something went wrong.