Skip to content

Commit

Permalink
Merge branch 'develop' into ruslan/asset-deposit-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Sep 23, 2021
2 parents 1f2b2e8 + c37f4fb commit 35ec75c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
Expand Up @@ -209,9 +209,11 @@ export function decodeErgoTokenInfo(
desc: string | null,
numDecimals: number | null,
|} {
const name = parseEIP0004Data(registers.R4) || null
const desc = parseEIP0004Data(registers.R5) || null;
const numDecimals = parseInt(parseEIP0004Data(registers.R6) ?? '', 10);
// eslint-disable-next-line no-console
const log = console.log;
const name = parseEIP0004Data(registers.R4, log) || null
const desc = parseEIP0004Data(registers.R5, log) || null;
const numDecimals = parseInt(parseEIP0004Data(registers.R6, log) ?? '', 10);

return {
name,
Expand Down
Expand Up @@ -37,6 +37,7 @@ import { calculateAndFormatValue } from '../../../utils/unit-of-account';
import classnames from 'classnames';
import { mintedTokenInfo } from '../../../../chrome/extension/ergo-connector/utils';
import type { Tx } from '../../../../chrome/extension/ergo-connector/types';
import { Logger } from '../../../utils/logging';

type Props = {|
+tx: Tx,
Expand Down Expand Up @@ -128,7 +129,7 @@ class SignTxPage extends Component<Props> {
// Tokens can be minted inside the transaction so we have to look it up there first
_resolveTokenInfo: TokenEntry => $ReadOnly<TokenRow> | null = tokenEntry => {
const { tx } = this.props;
const mintedTokens = mintedTokenInfo(tx);
const mintedTokens = mintedTokenInfo(tx, Logger.info);
const mintedToken = mintedTokens.find(t => tokenEntry.identifier === t.Identifier);
if (mintedToken != null) {
return mintedToken;
Expand Down
Expand Up @@ -42,6 +42,7 @@ import { ErgoExternalTxSignRequest } from '../../api/ergo/lib/transactions/ErgoE
import { RustModule } from '../../api/ada/lib/cardanoCrypto/rustLoader';
import { toRemoteUtxo } from '../../api/ergo/lib/transactions/utils';
import { mintedTokenInfo } from '../../../chrome/extension/ergo-connector/utils';
import { Logger } from '../../utils/logging';

// Need to run only once - Connecting wallets
let initedConnecting = false;
Expand Down Expand Up @@ -285,7 +286,7 @@ export default class ConnectorStore extends Store<StoresMap, ActionsMap> {
if (!signingMessage.sign.tx) return;
const { tx } = signingMessage.sign;
// it's possible we minted assets in this tx, so looking them up will fail
const mintedTokenIds = mintedTokenInfo(tx).map(t => t.Identifier);
const mintedTokenIds = mintedTokenInfo(tx, Logger.info).map(t => t.Identifier);
const tokenIdentifiers = Array.from(new Set([
...tx.inputs
.flatMap(output => output.assets)
Expand Down
23 changes: 15 additions & 8 deletions packages/yoroi-extension/chrome/extension/ergo-connector/utils.js
Expand Up @@ -2,9 +2,11 @@

import type { Tx } from './types';
import type { TokenRow } from '../../../app/api/ada/lib/storage/database/primitives/tables';
import { Logger } from '../../../app/utils/logging';

export function parseEIP0004Data(input: any): ?string {
export function parseEIP0004Data(
input: any,
log: string => void,
): ?string {
// https://github.com/ergoplatform/eips/blob/master/eip-0004.md
// format is: 0e + vlq(len(body as bytes)) + body (as bytes formatted in hex)
// where body is a utf8 string
Expand All @@ -25,7 +27,7 @@ export function parseEIP0004Data(input: any): ?string {
len = (128 * len) + (lenChunk & 0x7F);
} while (readNext);
if (2 * len > body.length) {
Logger.info(`vlq decode trailing data: ${body.slice(2 * len)}`);
log(`vlq decode trailing data: ${body.slice(2 * len)}`);
}
if (2 * len < body.length) {
return null;
Expand All @@ -34,12 +36,17 @@ export function parseEIP0004Data(input: any): ?string {
}

// you should not be able to mint more than 1
export function mintedTokenInfo(tx: Tx): $ReadOnly<TokenRow>[] {
export function mintedTokenInfo(
tx: Tx,
log: string => void,
): $ReadOnly<TokenRow>[] {
const tokens = []
for (const output of tx.outputs) {
const name = parseEIP0004Data(output.additionalRegisters.R4);
const description = parseEIP0004Data(output.additionalRegisters.R5);
const decimals = parseInt(parseEIP0004Data(output.additionalRegisters.R6) ?? '', 10);
const name = parseEIP0004Data(output.additionalRegisters.R4, log);
const description = parseEIP0004Data(output.additionalRegisters.R5, log);
const decimals = parseInt(
parseEIP0004Data(output.additionalRegisters.R6, log) ?? '', 10
);
if (name != null && description != null && decimals != null) {
tokens.push({
TokenId: 0,
Expand All @@ -60,7 +67,7 @@ export function mintedTokenInfo(tx: Tx): $ReadOnly<TokenRow>[] {
}
}
if (tokens.length > 1) {
Logger.info(`tx ${JSON.stringify(tx)} had multiple EIP-0004-looking outputs`);
log(`tx ${JSON.stringify(tx)} had multiple EIP-0004-looking outputs`);
}
return tokens;
}

0 comments on commit 35ec75c

Please sign in to comment.