Skip to content

Commit

Permalink
Merge pull request #48 from TrustyFund/GetNeededMemoKey
Browse files Browse the repository at this point in the history
Add selection of the required key
  • Loading branch information
roma219 authored Mar 30, 2018
2 parents 0fc0bea + 6697ada commit 60648c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
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

0 comments on commit 60648c3

Please sign in to comment.