Skip to content

Commit

Permalink
Merge pull request #8 from ebceu4/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
siemarell authored Nov 1, 2018
2 parents 671ebd9 + 567a463 commit 3674886
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
17 changes: 9 additions & 8 deletions src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function validateParams(seed: SeedTypes, params: Params) {
const { seed: s } = pullSeedAndIndex(seed)

if (s == undefined && params.senderPublicKey == undefined)
throw 'Please provide either seed or senderPublicKey'
throw new Error('Please provide either seed or senderPublicKey')
}

export interface SeedsAndIndexes {
Expand All @@ -23,7 +23,7 @@ export function addProof(tx: WithProofs, proof: string, index?: number) {
}

if (tx.proofs != undefined && tx.proofs[index] != undefined)
throw `Proof at index ${index} is already exists.`
throw new Error(`Proof at index ${index} is already exists.`)

tx.proofs = tx.proofs || []

Expand Down Expand Up @@ -58,7 +58,7 @@ export const pullSeedAndIndex = (seed: SeedTypes): { seed: string, index?: numbe
return empty

if (isSeedsAndIndexes(seed)) {
const keys = Object.keys(seed).map(k => parseInt(k))
const keys = Object.keys(seed).map(k => parseInt(k)).filter(k => !isNaN(k))

if (keys == undefined || keys.length == 0)
return empty
Expand All @@ -71,11 +71,12 @@ export const pullSeedAndIndex = (seed: SeedTypes): { seed: string, index?: numbe
return { seed: seed[keys[0]], index, nextSeed: Object.keys(newSeed).length > 0 ? newSeed : undefined }
}
if (isArrayOfSeeds(seed)) {
const [, ...newSeed] = seed
if (seed.length > 0)
return { seed: seed[0], nextSeed: newSeed.length > 0 ? newSeed : undefined }

return empty
return pullSeedAndIndex(
Object.entries(seed).filter(([k, v]) => !!v).reduce((acc, next) => {
acc[next[0]] = next[1];
return acc
}, {})
)
}

return { seed: <string>seed }
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { transfer } from './transactions/transfer'
export { alias } from './transactions/alias'
export { setScript } from './transactions/set-script'
export { order } from './transactions/order'
export { signTx } from './signTx'
39 changes: 39 additions & 0 deletions src/signTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { TransactionType, Tx } from './transactions'
import { SeedTypes } from "./generic";
import { issue } from "./transactions/issue";
import { transfer } from "./transactions/transfer";
import { reissue } from "./transactions/reissue";
import { burn } from "./transactions/burn";
import { lease } from "./transactions/lease";
import { cancelLease } from "./transactions/cancel-lease";
import { data } from "./transactions/data";
import { massTransfer } from "./transactions/mass-transfer";
import { alias } from "./transactions/alias";
import { setScript } from "./transactions/set-script";

export function signTx(seed: SeedTypes, tx: Tx): Tx {
switch (tx.type) {
case TransactionType.Issue:
return issue(seed, tx);
case TransactionType.Transfer:
return transfer(seed, tx);
case TransactionType.Reissue:
return reissue(seed, tx);
case TransactionType.Burn:
return burn(seed, tx);
case TransactionType.Lease:
return lease(seed, tx);
case TransactionType.CancelLease:
return cancelLease(seed, tx);
case TransactionType.Alias:
return alias(seed, tx);
case TransactionType.MassTransfer:
return massTransfer(seed, tx);
case TransactionType.Data:
return data(seed, tx);
case TransactionType.SetScript:
return setScript(seed, tx);
default:
throw new Error(`Unknown tx type: ${tx!.type}`)
}
}
53 changes: 50 additions & 3 deletions test/set-script.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import { BASE58_STRING, BASE64_STRING, BYTES, concat, LEN, LONG, OPTION, SHORT, veriySignature } from "waves-crypto";
import {
BASE58_STRING,
BASE64_STRING,
BYTES,
concat,
LEN,
LONG,
OPTION,
SHORT,
verifySignature,
publicKey,
} from "waves-crypto";
import { setScript } from '../src';
import { SetScriptTransaction } from "../src/transactions";

describe('setScript', () => {
const seed = 'test seed';
const seed2 = 'test seed 2'
const compiledContract = 'AQQAAAALYWxpY2VQdWJLZXkBAAAAID3+K0HJI42oXrHhtHFpHijU5PC4nn1fIFVsJp5UWrYABAAAAAlib2JQdWJLZXkBAAAAIBO1uieokBahePoeVqt4/usbhaXRq+i5EvtfsdBILNtuBAAAAAxjb29wZXJQdWJLZXkBAAAAIOfM/qkwkfi4pdngdn18n5yxNwCrBOBC3ihWaFg4gV4yBAAAAAthbGljZVNpZ25lZAMJAAH0AAAAAwgFAAAAAnR4AAAACWJvZHlCeXRlcwkAAZEAAAACCAUAAAACdHgAAAAGcHJvb2ZzAAAAAAAAAAAABQAAAAthbGljZVB1YktleQAAAAAAAAAAAQAAAAAAAAAAAAQAAAAJYm9iU2lnbmVkAwkAAfQAAAADCAUAAAACdHgAAAAJYm9keUJ5dGVzCQABkQAAAAIIBQAAAAJ0eAAAAAZwcm9vZnMAAAAAAAAAAAEFAAAACWJvYlB1YktleQAAAAAAAAAAAQAAAAAAAAAAAAQAAAAMY29vcGVyU2lnbmVkAwkAAfQAAAADCAUAAAACdHgAAAAJYm9keUJ5dGVzCQABkQAAAAIIBQAAAAJ0eAAAAAZwcm9vZnMAAAAAAAAAAAIFAAAADGNvb3BlclB1YktleQAAAAAAAAAAAQAAAAAAAAAAAAkAAGcAAAACCQAAZAAAAAIJAABkAAAAAgUAAAALYWxpY2VTaWduZWQFAAAACWJvYlNpZ25lZAUAAAAMY29vcGVyU2lnbmVkAAAAAAAAAAACVateHg=='

it('Should generate correct signed setScript transaction', () => {
Expand All @@ -15,26 +27,61 @@ describe('setScript', () => {
expect(validateSetScriptTx(signedTx)).to.be.true
});

it('Should generate correct signed setScript transaction with multiple signers via array', () => {
const txParams = { script: compiledContract };
const signedTx = setScript([null, seed, seed2], txParams);

expect(signedTx.proofs[0]).to.be.null
expect(validateSetScriptTx(signedTx, 1)).to.be.true
expect(validateSetScriptTx(signedTx, 2, publicKey(seed2))).to.be.true
});

it('Should generate correct signed setScript transaction with multiple signers via object', () => {
const txParams = { script: compiledContract };
const signedTx = setScript({ '1': seed, '2': seed2 }, txParams);

expect(signedTx.proofs[0]).to.be.null
expect(validateSetScriptTx(signedTx, 1, publicKey(seed))).to.be.true
expect(validateSetScriptTx(signedTx, 2, publicKey(seed2))).to.be.true
});

it('Should generate correct signed setScript transaction with null script', () => {
const txParams = { script: null };
const signedTx = setScript(seed, txParams);

expect(validateSetScriptTx(signedTx)).to.be.true
});

it('Should generate correct signed setScript transaction without seed', () => {
const txParams = { script: compiledContract, senderPublicKey: publicKey(seed) };
const tx = setScript(null, txParams);

expect(tx.script).to.eql('base64:' + txParams.script);
expect(tx.senderPublicKey).to.eql(publicKey(seed));
});

it('Should throw on undefined script', () => {
const txParams = {};
expect(() => setScript(seed, txParams)).to.throw('Script field cannot be undefined. Use null explicitly to remove script')
});

it('Should handle incorrect keys in seedObject', () => {
const txParams = { script: compiledContract };
const signedTx = setScript({ 'asd1': seed, '2': seed2 }as any, txParams);

expect(signedTx.proofs[0]).to.be.null
expect(signedTx.proofs[1]).to.be.null
expect(validateSetScriptTx(signedTx, 2, publicKey(seed2))).to.be.true
});
})

function validateSetScriptTx(tx: SetScriptTransaction, proofNumber = 0): boolean {
function validateSetScriptTx(tx: SetScriptTransaction, proofNumber = 0, publicKey?: string): boolean {
const bytes = concat(
BYTES([tx.type, tx.version, tx.chainId.charCodeAt(0)]),
BASE58_STRING(tx.senderPublicKey),
OPTION(LEN(SHORT)(BASE64_STRING))(tx.script ? tx.script.slice(7) : null),
LONG(tx.fee),
LONG(tx.timestamp),
)
return verifySignature(tx.senderPublicKey, bytes, tx.proofs[proofNumber])
return verifySignature(publicKey || tx.senderPublicKey, bytes, tx.proofs[proofNumber])
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"compilerOptions": {
"target": "es6",
"lib": [
"es7"
"es7",
"es2018"
],
//"sourceMap": true,
"declaration": true,
Expand Down

0 comments on commit 3674886

Please sign in to comment.