Skip to content
Permalink
Browse files

fix transfer

  • Loading branch information
SebastienGllmt committed Dec 3, 2019
1 parent 85ba937 commit 5386d55d6fb25a7d4d16a7da3de3ae31e89e3d22
@@ -2,6 +2,7 @@

import type { CoreAddressT } from '../database/primitives/enums';
import { CoreAddressTypes } from '../database/primitives/enums';
import { Bech32Prefix } from '../../../../../config/stringConfig';
import { RustModule } from '../../cardanoCrypto/rustLoader';

export function addressToKind(
@@ -69,3 +70,23 @@ export function verifyAddress(
}
}
}

export function addressToDisplayString(
address: string
): string {
try {
// Need to try parsing as a legacy address first
// Since parsing as bech32 directly may give a wrong result if the address contains a 1
RustModule.WalletV2.Address.from_base58(address);
return address;
} catch (_e1) {
try {
const wasmAddr = RustModule.WalletV3.Address.from_bytes(
Buffer.from(address, 'hex')
);
return wasmAddr.to_string(Bech32Prefix.ADDRESS);
} catch (_e2) {
throw new Error('addressToKind failed to parse address type ' + address);
}
}
}
@@ -9,6 +9,7 @@ import {
stringifyError,
} from '../../../../utils/logging';
import { LOVELACES_PER_ADA } from '../../../../config/numbersConfig';
import { Bech32Prefix } from '../../../../config/stringConfig';
import {
GenerateTransferTxError
} from '../../errors';
@@ -70,8 +71,11 @@ export async function buildDaedalusTransferTx(payload: {|
fee: fee.dividedBy(LOVELACES_PER_ADA),
id: Buffer.from(fragment.id().as_bytes()).toString('hex'),
encodedTx: fragment.as_bytes(),
// recall: Daedalus addresses all have to be legacy so we don't turn them to bech32
senders: Object.keys(addressKeys),
receiver: outputAddr,
receiver: RustModule.WalletV3.Address.from_bytes(
Buffer.from(outputAddr, 'hex')
).to_string(Bech32Prefix.ADDRESS)
};
} catch (error) {
Logger.error(`daedalusTransfer::buildTransferTx ${stringifyError(error)}`);
@@ -7,6 +7,8 @@ import {
stringifyError,
} from '../../../../utils/logging';
import { LOVELACES_PER_ADA } from '../../../../config/numbersConfig';
import { Bech32Prefix } from '../../../../config/stringConfig';
import { addressToDisplayString } from '../../lib/storage/bridge/utils';
import {
GenerateTransferTxError
} from '../../errors';
@@ -54,15 +56,19 @@ export async function buildYoroiTransferTx(payload: {|
true,
);

const uniqueSenders = Array.from(new Set(senderUtxos.map(utxo => utxo.receiver)));

// return summary of transaction
return {
recoveredBalance: totalBalance.dividedBy(LOVELACES_PER_ADA),
fee: fee.dividedBy(LOVELACES_PER_ADA),
id: Buffer.from(fragment.id().as_bytes()).toString('hex'),
encodedTx: fragment.as_bytes(),
// only display unique addresses
senders: Array.from(new Set(senderUtxos.map(utxo => utxo.receiver))),
receiver: outputAddr,
// recall: some addresses may be legacy, some may be Shelley
senders: uniqueSenders.map(addr => addressToDisplayString(addr)),
receiver: RustModule.WalletV3.Address.from_bytes(
Buffer.from(outputAddr, 'hex')
).to_string(Bech32Prefix.ADDRESS)
};
} catch (error) {
Logger.error(`transfer::buildTransferTx ${stringifyError(error)}`);
@@ -226,7 +226,7 @@ describe('Shelley era tx format tests', () => {
expect(transferInfo.fee.toString()).toBe('0.155383');
expect(transferInfo.recoveredBalance.toString()).toBe('1');
expect(transferInfo.senders).toEqual([address]);
expect(transferInfo.receiver).toBe(outAddress);
expect(transferInfo.receiver).toBe(bech32Addr);

// check tx itself
const fragment = RustModule.WalletV3.Fragment.from_bytes(transferInfo.encodedTx);
@@ -323,7 +323,7 @@ describe('Shelley era tx format tests', () => {
expect(transferInfo.fee.toString()).toBe('0.155482');
expect(transferInfo.recoveredBalance.toString()).toBe('100.0001');
expect(transferInfo.senders).toEqual([address]);
expect(transferInfo.receiver).toBe(outAddress);
expect(transferInfo.receiver).toBe(bech32Addr);

// check tx itself
const fragment = RustModule.WalletV3.Fragment.from_bytes(transferInfo.encodedTx);
@@ -17,6 +17,7 @@ import type {
import {
ChainDerivations,
} from '../../../../config/numbersConfig';
import { Bech32Prefix } from '../../../../config/stringConfig';

import {
loadLovefieldDB,
@@ -48,7 +49,7 @@ function getShelleyAddress(
RustModule.WalletV3.AddressDiscrimination.Production
);
return {
address: addr.to_string('addr'),
address: Buffer.from(addr.as_bytes()).toString('hex'),
addressing: {
path: [ChainDerivations.EXTERNAL, derivationId],
startLevel: Bip44DerivationLevels.CHAIN.level,
@@ -178,8 +179,12 @@ describe('Shelley era tx format tests', () => {

expect(transferInfo.fee.toString()).toBe('0.155383');
expect(transferInfo.recoveredBalance.toString()).toBe('1');
expect(transferInfo.senders).toEqual([addr1.address]);
expect(transferInfo.receiver).toBe(outAddress);
expect(transferInfo.senders).toEqual([
RustModule.WalletV3.Address.from_bytes(
Buffer.from(addr1.address, 'hex')
).to_string(Bech32Prefix.ADDRESS)
]);
expect(transferInfo.receiver).toBe(bech32Addr);

// check tx itself
const fragment = RustModule.WalletV3.Fragment.from_bytes(transferInfo.encodedTx);

0 comments on commit 5386d55

Please sign in to comment.
You can’t perform that action at this time.