Skip to content
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

Add selection of the required key #48

Merged
merged 1 commit into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/services/api/transactions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionBuilder, ChainTypes, ops, PrivateKey } from 'bitsharesjs';
import { ChainConfig } from 'bitsharesjs-ws';
import { getUser } from './account';
import { encryptMemo } from '../../utils';
import { encryptMemo, getMemoPrivKey } from '../../utils';
import { getCachedComissions } from './parameters';


Expand Down Expand Up @@ -36,7 +36,21 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) =>
return { success: false, error: 'Destination user not found' };
}

const { active, owner } = keys;
const {
data: {
account: {
options: {
memo_key: memoKey
}
}
}
} = await getUser(fromId);

const memoPrivate = getMemoPrivKey(keys, memoKey);

if (!memoPrivate) {
return { success: false, error: 'Cant find key to encrypt memo' };
}

const transferObject = {
fee: {
Expand All @@ -53,7 +67,7 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) =>

if (memo) {
try {
transferObject.memo = encryptMemo(memo, active, toAccount.data.account.options.memo_key);
transferObject.memo = encryptMemo(memo, memoPrivate, toAccount.data.account.options.memo_key);
} catch (error) {
return { success: false, error: 'Encrypt memo failed' };
}
Expand All @@ -65,7 +79,7 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) =>
}, ChainConfig.expire_in_secs * 2000);

try {
await buildAndBroadcast('transfer', transferObject, { active, owner });
await buildAndBroadcast('transfer', transferObject, keys);
clearTimeout(broadcastTimeout);
resolve({ success: true });
} catch (error) {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ export const decryptMemo = (memo, privateKey) => {
).toString('utf-8');
};

export const getMemoPrivKey = (keys, publicKey) => {
const { active, owner } = keys;
const ownerPubkey = owner.toPublicKey().toPublicKeyString();
const activePubkey = active.toPublicKey().toPublicKeyString();
if (publicKey === ownerPubkey) {
return owner;
}
if (publicKey === activePubkey) {
return active;
}
return false;
};


/** Calculates distribution 0..1 of total amount of assets expressed
* in base asset
Expand Down