Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

feat: verify that expected and actual sender of transactions are a match #651

Merged
merged 3 commits into from Aug 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/platform-sdk-ark/src/services/transaction.test.ts
Expand Up @@ -28,6 +28,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.transfer({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -45,6 +46,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.secondSignature({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -61,6 +63,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.delegateRegistration({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -77,6 +80,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.vote({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -93,6 +97,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.multiSignature({
nonce: "1",
from: "DEMvpU4Qq6KvSzF3sRNjGCkm6Kj7cFfVaz",
data: {
publicKeys: [
"039180ea4a8a803ee11ecb462bb8f9613fcdb5fe917e292dbcc73409f0e98f8f22",
Expand Down Expand Up @@ -120,6 +125,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.ipfs({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -134,6 +140,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.multiPayment({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -154,6 +161,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.delegateResignation({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -167,6 +175,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.htlcLock({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -189,6 +198,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.htlcClaim({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -206,6 +216,7 @@ describe("Core", () => {
it("should verify", async () => {
const result = await subject.htlcRefund({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -224,6 +235,7 @@ describe("Magistrate", () => {
it("should verify", async () => {
const result = await subject.entityRegistration({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -242,6 +254,7 @@ describe("Magistrate", () => {
it("should verify", async () => {
const result = await subject.entityResignation({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand All @@ -259,6 +272,7 @@ describe("Magistrate", () => {
it("should verify", async () => {
const result = await subject.entityUpdate({
nonce: "1",
from: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand Down
38 changes: 22 additions & 16 deletions packages/platform-sdk-ark/src/services/transaction.ts
Expand Up @@ -226,6 +226,28 @@ export class TransactionService implements Contracts.TransactionService {
options?: Contracts.TransactionOptions,
callback?: Function,
): Promise<DTO.SignedTransactionData> {
let address: string | undefined;

if (input.sign.mnemonic) {
address = await this.#identity.address().fromMnemonic(BIP39.normalize(input.sign.mnemonic));
}

if (input.sign.wif) {
address = await this.#identity.address().fromWIF(input.sign.wif);
}

if (!address) {
throw new Error(
`Failed to retrieve the nonce for the signatory wallet. Please provide one through the [input] parameter.`,
);
}

if (input.from !== address) {
throw new Error(
`Signatory should be [${input.from}] but is [${address}]. Please ensure that the expected and actual signatory match.`,
);
}

let transaction;

if (this.#magistrateBuilders[type]) {
Expand All @@ -237,22 +259,6 @@ export class TransactionService implements Contracts.TransactionService {
if (input.nonce) {
transaction.nonce(input.nonce);
} else {
let address: string | undefined;

if (input.sign.mnemonic) {
address = await this.#identity.address().fromMnemonic(BIP39.normalize(input.sign.mnemonic));
}

if (input.sign.wif) {
address = await this.#identity.address().fromWIF(input.sign.wif);
}

if (!address) {
throw new Error(
`Failed to retrieve the nonce for the signatory wallet. Please provide one through the [input] parameter.`,
);
}

const body: any = (await this.#http.get(`${this.#peer}/wallets/${address}`)).json();

transaction.nonce(BigNumber.make(body.data.nonce).plus(1).toFixed());
Expand Down
Expand Up @@ -19,6 +19,7 @@ describe("TransactionService", () => {
.reply(200, require(`${__dirname}/../../test/fixtures/client/wallet.json`));

const result: any = await subject.transfer({
from: "cosmos1fvxjdyfdvat5g0ee7jmyemwl2n95ad7negf7ap",
sign: {
mnemonic: "this is a top secret passphrase",
},
Expand Down
4 changes: 2 additions & 2 deletions packages/platform-sdk-eos/src/services/transaction.ts
Expand Up @@ -47,12 +47,12 @@ export class TransactionService implements Contracts.TransactionService {
name: "transfer",
authorization: [
{
actor: input.data.from,
actor: input.from,
permission: "active",
},
],
data: {
from: input.data.from,
from: input.from,
to: input.data.to,
quantity: "0.0001 TNT", // todo: use network specific token
memo: input.data.memo,
Expand Down
5 changes: 5 additions & 0 deletions packages/platform-sdk-lsk/src/services/transaction.test.ts
Expand Up @@ -14,6 +14,7 @@ describe("TransactionService", () => {
const service = await TransactionService.construct(createConfig({ network }));

const result: any = await service.transfer({
from: "15957226662510576840L",
sign: {
mnemonic: identity.mnemonic,
},
Expand All @@ -30,6 +31,7 @@ describe("TransactionService", () => {
describe("#secondSignature", () => {
it("should verify", async () => {
const result: any = await subject.secondSignature({
from: "15957226662510576840L",
sign: {
mnemonic: identity.mnemonic,
secondMnemonic: identity.mnemonic,
Expand All @@ -46,6 +48,7 @@ describe("TransactionService", () => {
describe("#delegateRegistration", () => {
it("should verify", async () => {
const result: any = await subject.delegateRegistration({
from: "15957226662510576840L",
sign: {
mnemonic: identity.mnemonic,
},
Expand All @@ -61,6 +64,7 @@ describe("TransactionService", () => {
describe("#vote", () => {
it("should verify", async () => {
const result: any = await subject.vote({
from: "15957226662510576840L",
sign: {
mnemonic: identity.mnemonic,
},
Expand All @@ -76,6 +80,7 @@ describe("TransactionService", () => {
describe("#multiSignature", () => {
it("should verify", async () => {
const result: any = await subject.multiSignature({
from: "15957226662510576840L",
sign: { mnemonic: identity.mnemonic },
data: {
publicKeys: [
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-sdk-trx/src/services/transaction.test.ts
Expand Up @@ -20,11 +20,11 @@ describe("TransactionService", function () {
.reply(200, require(`${__dirname}/../../test/fixtures/crypto/transfer.json`));

const result = await subject.transfer({
from: testWallet.address,
sign: {
mnemonic: testWallet.privateKey,
},
data: {
from: testWallet.address,
to: "TY689z7Q2NpZYBxGfXbYR4PmS2WXyTNrir",
amount: "1",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-sdk-trx/src/services/transaction.ts
Expand Up @@ -31,7 +31,7 @@ export class TransactionService implements Contracts.TransactionService {
const transaction = await this.#connection.transactionBuilder.sendTrx(
input.data.to,
input.data.amount,
input.data.from,
input.from,
1,
);

Expand Down
2 changes: 2 additions & 0 deletions packages/platform-sdk-xlm/src/services/client.test.ts
Expand Up @@ -104,6 +104,7 @@ describe("ClientService", function () {

const result = await subject.broadcast([
await transactionService.transfer({
from: identity.address,
sign: {
mnemonic: identity.mnemonic,
},
Expand Down Expand Up @@ -136,6 +137,7 @@ describe("ClientService", function () {

const result = await subject.broadcast([
await transactionService.transfer({
from: identity.address,
sign: {
mnemonic: identity.mnemonic,
},
Expand Down
1 change: 1 addition & 0 deletions packages/platform-sdk-xlm/src/services/transaction.test.ts
Expand Up @@ -23,6 +23,7 @@ describe("TransactionService", () => {
.reply(200, require(`${__dirname}/../../test/fixtures/client/wallet.json`));

const result: any = await subject.transfer({
from: identity.address,
sign: {
mnemonic: identity.mnemonic,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/platform-sdk/src/contracts/coins/transaction.ts
Expand Up @@ -30,6 +30,7 @@ export interface TransactionInput {
fee?: string;
feeLimit?: string;
nonce?: string;
from: string;
sign: {
mnemonic: string;
mnemonics?: string[];
Expand All @@ -44,14 +45,13 @@ export interface TransactionInput {
}

export interface TransactionOptions {
unsignedJson: boolean;
unsignedBytes: boolean;
unsignedJson: boolean;
}

export interface TransferInput extends TransactionInput {
data: {
amount: string;
from?: string;
to: string;
memo?: string;
};
Expand Down