Socket
Socket
Sign inDemoInstall

@iota/bundle

Package Overview
Dependencies
Maintainers
5
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iota/bundle - npm Package Compare versions

Comparing version 1.0.0-beta.8 to 1.0.0-beta.9

out/errors.js

56

out/bundle/src/index.js

@@ -19,3 +19,5 @@ "use strict";

var signing_1 = require("@iota/signing");
var errors_1 = require("../../errors");
require("../../typed-array");
var HASH_TRITS_SIZE = 243;
var NULL_HASH_TRYTES = '9'.repeat(81);

@@ -36,7 +38,7 @@ var NULL_TAG_TRYTES = '9'.repeat(27);

/**
* Creates a bunlde with given transaction entries.
* Creates a bundle with given transaction entries.
*
* @method createBundle
*
* @param {BundleEntry[]} entries - Entries of signle or multiple transactions with the same address
* @param {BundleEntry[]} entries - Entries of single or multiple transactions with the same address
*

@@ -50,3 +52,3 @@ * @return {Transaction[]} List of transactions in the bundle

/**
* Creates a bunlde with given transaction entries
* Adds given transaction entry to a bundle.
*

@@ -57,11 +59,11 @@ * @method addEntry

*
* @param {object} entry - Entry of single or multiple transactions with the same address
* @param {number} [entry.length=1] - Entry length, which indicates how many transactions in the bundle will occupy
* @param {string} [entry.address] - Address, defaults to all-9s
* @param {number} [entry.value = 0] - Value to transfer in _IOTAs_
* @param {string[]} [entry.signatureMessageFragments] - Array of signature message fragments trytes, defaults to all-9s
* @param {object} entry - Entry of a single or multiple transactions with the same address
* @param {number} [entry.length = 1] - Entry length, which indicates how many transactions in the bundle it will occupy
* @param {Hash} [entry.address] - Address, defaults to all-9s
* @param {number} [entry.value = 0] - Value to transfer in iotas
* @param {Trytes[]} [entry.signatureMessageFragments] - List of signature message fragments, defaults to all-9s
* @param {number} [entry.timestamp] - Transaction timestamp, defaults to `Math.floor(Date.now() / 1000)`
* @param {string} [entry.tag] - Optional Tag, defaults to null tag (all-9s)
*
* @return {Transaction[]} Bundle
* @return {Transaction[]} List of transactions in the updated bundle
*/

@@ -74,2 +76,5 @@ exports.addEntry = function (transactions, entry) {

var obsoleteTag = tag;
if (value !== 0 && converter_1.trits(address)[HASH_TRITS_SIZE - 1] !== 0) {
throw new Error(errors_1.INVALID_ADDRESS_LAST_TRIT);
}
return transactions.map(function (transaction) { return (__assign({}, transaction, { lastIndex: lastIndex })); }).concat(Array(length)

@@ -97,13 +102,13 @@ .fill(null)

/**
* Adds a list of trytes in the bundle starting at offset
* Adds signature message fragments to transactions in a bundle starting at offset.
*
* @method addTrytes
*
* @param {Transaction[]} transactions - Transactions in the bundle
* @param {Transaction[]} transactions - List of transactions in the bundle
*
* @param {Trytes[]} fragments - Message signature fragments to add
* @param {Trytes[]} fragments - List of signature message fragments to add
*
* @param {number} [offset=0] - Optional offset to start appending signature message fragments
* @param {number} [offset = 0] - Optional offset to start appending signature message fragments
*
* @return {Transaction[]} Transactions of finalized bundle
* @return {Transaction[]} List of transactions in the updated bundle
*/

@@ -118,11 +123,12 @@ exports.addTrytes = function (transactions, fragments, offset) {

/**
* Finalizes the bundle by calculating the bundle hash
* Finalizes a bundle by calculating the bundle hash.
*
* @method finalizeBundle
*
* @param {Transaction[]} transactions - Transactions in the bundle
* @param {Transaction[]} transactions - List of transactions in the bundle
*
* @return {Transaction[]} Transactions of finalized bundle
* @return {Transaction[]} List of transactions in the finalized bundle
*/
exports.finalizeBundle = function (transactions) {
var validBundle = false;
var valueTrits = transactions.map(function (tx) { return converter_1.trits(tx.value); }).map(pad_1.padTrits(81));

@@ -133,7 +139,5 @@ var timestampTrits = transactions.map(function (tx) { return converter_1.trits(tx.timestamp); }).map(pad_1.padTrits(27));

var obsoleteTagTrits = transactions.map(function (tx) { return converter_1.trits(tx.obsoleteTag); }).map(pad_1.padTrits(81));
var bundleHash;
var validBundle = false;
var bundleHashTrits = new Int8Array(kerl_1["default"].HASH_LENGTH);
while (!validBundle) {
var kerl = new kerl_1["default"]();
kerl.initialize();
var sponge = new kerl_1["default"]();
for (var i = 0; i < transactions.length; i++) {

@@ -146,8 +150,6 @@ var essence = converter_1.trits(transactions[i].address +

converter_1.trytes(lastIndexTrits));
kerl.absorb(essence, 0, essence.length);
sponge.absorb(essence, 0, essence.length);
}
var bundleHashTrits = new Int8Array(kerl_1["default"].HASH_LENGTH);
kerl.squeeze(bundleHashTrits, 0, kerl_1["default"].HASH_LENGTH);
bundleHash = converter_1.trytes(bundleHashTrits);
if (signing_1.normalizedBundleHash(bundleHash).indexOf(13) !== -1) {
sponge.squeeze(bundleHashTrits, 0, kerl_1["default"].HASH_LENGTH);
if (signing_1.normalizedBundle(bundleHashTrits).indexOf(13) !== -1) {
// Insecure bundle, increment obsoleteTag and recompute bundle hash

@@ -162,4 +164,4 @@ obsoleteTagTrits[0] = signing_1.add(obsoleteTagTrits[0], new Int8Array(1).fill(1));

// overwrite obsoleteTag in first entry
obsoleteTag: i === 0 ? converter_1.trytes(obsoleteTagTrits[0]) : transaction.obsoleteTag, bundle: bundleHash })); });
obsoleteTag: i === 0 ? converter_1.trytes(obsoleteTagTrits[0]) : transaction.obsoleteTag, bundle: converter_1.trytes(bundleHashTrits) })); });
};
//# sourceMappingURL=index.js.map

@@ -14,4 +14,7 @@ "use strict";

exports.__esModule = true;
var converter_1 = require("@iota/converter");
var ava_1 = require("ava");
var errors_1 = require("../../errors");
var src_1 = require("../src");
var HASH_TRITS_SIZE = 243;
var NULL_HASH = '9'.repeat(81);

@@ -104,2 +107,16 @@ var NULL_NONCE = '9'.repeat(27);

});
ava_1["default"]('addEntry() throws error for entry with value and address with last trit !== 0.', function (t) {
var invalidAddressTrits = converter_1.trytesToTrits(addresses[1]);
invalidAddressTrits[HASH_TRITS_SIZE - 1] = 1;
var invalidAddressTrytes = converter_1.tritsToTrytes(invalidAddressTrits);
var entry = {
length: 1,
address: invalidAddressTrytes,
value: 1,
tag: 'TAG',
timestamp: 1522219
};
t.is(t.throws(function () { return src_1.addEntry([], entry); }).message, errors_1.INVALID_ADDRESS_LAST_TRIT, 'addEntry() should throw error for entry with value and address with last trit !== 0.');
t.is(t.throws(function () { return src_1.addEntry([], __assign({}, entry, { value: -1 })); }).message, errors_1.INVALID_ADDRESS_LAST_TRIT, 'addEntry() should throw error for entry with value and address with last trit !== 0.');
});
ava_1["default"]('addTrytes() adds trytes and returns correct transactions.', function (t) {

@@ -106,0 +123,0 @@ t.deepEqual(src_1.addTrytes(bundle, ['TRYTES', 'TRYTES', 'TRYTES']), bundle.map(function (transaction) { return (__assign({}, transaction, { signatureMessageFragment: 'TRYTES' + '9'.repeat(81 * 27 - 6) })); }), 'addEntry should add trytes and return correct transactions.');

{
"name": "@iota/bundle",
"version": "1.0.0-beta.8",
"version": "1.0.0-beta.9",
"description": "Utilities for generating and signing bundles",

@@ -67,7 +67,7 @@ "main": "./out/bundle/src/index.js",

"dependencies": {
"@iota/converter": "^1.0.0-beta.8",
"@iota/kerl": "^1.0.0-beta.8",
"@iota/pad": "^1.0.0-beta.8",
"@iota/signing": "^1.0.0-beta.8"
"@iota/converter": "^1.0.0-beta.9",
"@iota/kerl": "^1.0.0-beta.9",
"@iota/pad": "^1.0.0-beta.9",
"@iota/signing": "^1.0.0-beta.9"
}
}

@@ -39,5 +39,5 @@ # @iota/bundle

| --- | --- | --- |
| entries | <code>Array.&lt;BundleEntry&gt;</code> | Entries of signle or multiple transactions with the same address |
| entries | <code>Array.&lt;BundleEntry&gt;</code> | Entries of single or multiple transactions with the same address |
Creates a bunlde with given transaction entries.
Creates a bundle with given transaction entries.

@@ -52,13 +52,13 @@ **Returns**: <code>Array.&lt;Transaction&gt;</code> - List of transactions in the bundle

| transactions | <code>Array.&lt;Transaction&gt;</code> | | List of transactions currently in the bundle |
| entry | <code>object</code> | | Entry of single or multiple transactions with the same address |
| [entry.length] | <code>number</code> | <code>1</code> | Entry length, which indicates how many transactions in the bundle will occupy |
| [entry.address] | <code>string</code> | | Address, defaults to all-9s |
| [entry.value] | <code>number</code> | <code>0</code> | Value to transfer in _IOTAs_ |
| [entry.signatureMessageFragments] | <code>Array.&lt;string&gt;</code> | | Array of signature message fragments trytes, defaults to all-9s |
| entry | <code>object</code> | | Entry of a single or multiple transactions with the same address |
| [entry.length] | <code>number</code> | <code>1</code> | Entry length, which indicates how many transactions in the bundle it will occupy |
| [entry.address] | <code>Hash</code> | | Address, defaults to all-9s |
| [entry.value] | <code>number</code> | <code>0</code> | Value to transfer in iotas |
| [entry.signatureMessageFragments] | <code>Array.&lt;Trytes&gt;</code> | | List of signature message fragments, defaults to all-9s |
| [entry.timestamp] | <code>number</code> | | Transaction timestamp, defaults to `Math.floor(Date.now() / 1000)` |
| [entry.tag] | <code>string</code> | | Optional Tag, defaults to null tag (all-9s) |
Creates a bunlde with given transaction entries
Adds given transaction entry to a bundle.
**Returns**: <code>Array.&lt;Transaction&gt;</code> - Bundle
**Returns**: <code>Array.&lt;Transaction&gt;</code> - List of transactions in the updated bundle
<a name="module_bundle..addTrytes"></a>

@@ -70,9 +70,9 @@

| --- | --- | --- | --- |
| transactions | <code>Array.&lt;Transaction&gt;</code> | | Transactions in the bundle |
| fragments | <code>Array.&lt;Trytes&gt;</code> | | Message signature fragments to add |
| transactions | <code>Array.&lt;Transaction&gt;</code> | | List of transactions in the bundle |
| fragments | <code>Array.&lt;Trytes&gt;</code> | | List of signature message fragments to add |
| [offset] | <code>number</code> | <code>0</code> | Optional offset to start appending signature message fragments |
Adds a list of trytes in the bundle starting at offset
Adds signature message fragments to transactions in a bundle starting at offset.
**Returns**: <code>Array.&lt;Transaction&gt;</code> - Transactions of finalized bundle
**Returns**: <code>Array.&lt;Transaction&gt;</code> - List of transactions in the updated bundle
<a name="module_bundle..finalizeBundle"></a>

@@ -84,6 +84,6 @@

| --- | --- | --- |
| transactions | <code>Array.&lt;Transaction&gt;</code> | Transactions in the bundle |
| transactions | <code>Array.&lt;Transaction&gt;</code> | List of transactions in the bundle |
Finalizes the bundle by calculating the bundle hash
Finalizes a bundle by calculating the bundle hash.
**Returns**: <code>Array.&lt;Transaction&gt;</code> - Transactions of finalized bundle
**Returns**: <code>Array.&lt;Transaction&gt;</code> - List of transactions in the finalized bundle

@@ -6,3 +6,4 @@ /** @module bundle */

import { padTag, padTrits, padTrytes } from '@iota/pad'
import { add, normalizedBundleHash } from '@iota/signing'
import { add, normalizedBundle } from '@iota/signing'
import { INVALID_ADDRESS_LAST_TRIT } from '../../errors'
import '../../typed-array'

@@ -16,2 +17,3 @@ import {

const HASH_TRITS_SIZE = 243
const NULL_HASH_TRYTES = '9'.repeat(81)

@@ -45,7 +47,7 @@ const NULL_TAG_TRYTES = '9'.repeat(27)

/**
* Creates a bunlde with given transaction entries.
* Creates a bundle with given transaction entries.
*
* @method createBundle
*
* @param {BundleEntry[]} entries - Entries of signle or multiple transactions with the same address
* @param {BundleEntry[]} entries - Entries of single or multiple transactions with the same address
*

@@ -58,3 +60,3 @@ * @return {Transaction[]} List of transactions in the bundle

/**
* Creates a bunlde with given transaction entries
* Adds given transaction entry to a bundle.
*

@@ -65,11 +67,11 @@ * @method addEntry

*
* @param {object} entry - Entry of single or multiple transactions with the same address
* @param {number} [entry.length=1] - Entry length, which indicates how many transactions in the bundle will occupy
* @param {string} [entry.address] - Address, defaults to all-9s
* @param {number} [entry.value = 0] - Value to transfer in _IOTAs_
* @param {string[]} [entry.signatureMessageFragments] - Array of signature message fragments trytes, defaults to all-9s
* @param {object} entry - Entry of a single or multiple transactions with the same address
* @param {number} [entry.length = 1] - Entry length, which indicates how many transactions in the bundle it will occupy
* @param {Hash} [entry.address] - Address, defaults to all-9s
* @param {number} [entry.value = 0] - Value to transfer in iotas
* @param {Trytes[]} [entry.signatureMessageFragments] - List of signature message fragments, defaults to all-9s
* @param {number} [entry.timestamp] - Transaction timestamp, defaults to `Math.floor(Date.now() / 1000)`
* @param {string} [entry.tag] - Optional Tag, defaults to null tag (all-9s)
*
* @return {Transaction[]} Bundle
* @return {Transaction[]} List of transactions in the updated bundle
*/

@@ -83,2 +85,6 @@ export const addEntry = (transactions: Bundle, entry: Partial<BundleEntry>): Bundle => {

if (value !== 0 && trits(address)[HASH_TRITS_SIZE - 1] !== 0) {
throw new Error(INVALID_ADDRESS_LAST_TRIT)
}
return transactions.map(transaction => ({ ...transaction, lastIndex })).concat(

@@ -109,13 +115,13 @@ Array(length)

/**
* Adds a list of trytes in the bundle starting at offset
* Adds signature message fragments to transactions in a bundle starting at offset.
*
* @method addTrytes
*
* @param {Transaction[]} transactions - Transactions in the bundle
* @param {Transaction[]} transactions - List of transactions in the bundle
*
* @param {Trytes[]} fragments - Message signature fragments to add
* @param {Trytes[]} fragments - List of signature message fragments to add
*
* @param {number} [offset=0] - Optional offset to start appending signature message fragments
* @param {number} [offset = 0] - Optional offset to start appending signature message fragments
*
* @return {Transaction[]} Transactions of finalized bundle
* @return {Transaction[]} List of transactions in the updated bundle
*/

@@ -134,27 +140,22 @@ export const addTrytes = (transactions: Bundle, fragments: ReadonlyArray<Trytes>, offset = 0): Bundle =>

/**
* Finalizes the bundle by calculating the bundle hash
* Finalizes a bundle by calculating the bundle hash.
*
* @method finalizeBundle
*
* @param {Transaction[]} transactions - Transactions in the bundle
* @param {Transaction[]} transactions - List of transactions in the bundle
*
* @return {Transaction[]} Transactions of finalized bundle
* @return {Transaction[]} List of transactions in the finalized bundle
*/
export const finalizeBundle = (transactions: Bundle): Bundle => {
let validBundle: boolean = false
const valueTrits = transactions.map(tx => trits(tx.value)).map(padTrits(81))
const timestampTrits = transactions.map(tx => trits(tx.timestamp)).map(padTrits(27))
const currentIndexTrits = transactions.map(tx => trits(tx.currentIndex)).map(padTrits(27))
const lastIndexTrits = padTrits(27)(trits(transactions[0].lastIndex))
const obsoleteTagTrits = transactions.map(tx => trits(tx.obsoleteTag)).map(padTrits(81))
const bundleHashTrits = new Int8Array(Kerl.HASH_LENGTH)
let bundleHash: Hash
let validBundle: boolean = false
while (!validBundle) {
const kerl = new Kerl()
kerl.initialize()
const sponge = new Kerl()

@@ -170,10 +171,8 @@ for (let i = 0; i < transactions.length; i++) {

)
kerl.absorb(essence, 0, essence.length)
sponge.absorb(essence, 0, essence.length)
}
const bundleHashTrits = new Int8Array(Kerl.HASH_LENGTH)
kerl.squeeze(bundleHashTrits, 0, Kerl.HASH_LENGTH)
bundleHash = trytes(bundleHashTrits)
sponge.squeeze(bundleHashTrits, 0, Kerl.HASH_LENGTH)
if (normalizedBundleHash(bundleHash).indexOf(13) !== -1) {
if (normalizedBundle(bundleHashTrits).indexOf(13) !== -1) {
// Insecure bundle, increment obsoleteTag and recompute bundle hash

@@ -190,4 +189,4 @@ obsoleteTagTrits[0] = add(obsoleteTagTrits[0], new Int8Array(1).fill(1))

obsoleteTag: i === 0 ? trytes(obsoleteTagTrits[0]) : transaction.obsoleteTag,
bundle: bundleHash,
bundle: trytes(bundleHashTrits),
}))
}

@@ -0,4 +1,7 @@

import { tritsToTrytes, trytesToTrits } from '@iota/converter'
import test from 'ava'
import { INVALID_ADDRESS_LAST_TRIT } from '../../errors'
import { addEntry, addTrytes, createBundle, finalizeBundle } from '../src'
const HASH_TRITS_SIZE = 243
const NULL_HASH = '9'.repeat(81)

@@ -103,2 +106,28 @@ const NULL_NONCE = '9'.repeat(27)

test('addEntry() throws error for entry with value and address with last trit !== 0.', t => {
const invalidAddressTrits = trytesToTrits(addresses[1])
invalidAddressTrits[HASH_TRITS_SIZE - 1] = 1
const invalidAddressTrytes = tritsToTrytes(invalidAddressTrits)
const entry = {
length: 1,
address: invalidAddressTrytes,
value: 1,
tag: 'TAG',
timestamp: 1522219,
}
t.is(
t.throws(() => addEntry([], entry)).message,
INVALID_ADDRESS_LAST_TRIT,
'addEntry() should throw error for entry with value and address with last trit !== 0.'
)
t.is(
t.throws(() => addEntry([], { ...entry, value: -1 })).message,
INVALID_ADDRESS_LAST_TRIT,
'addEntry() should throw error for entry with value and address with last trit !== 0.'
)
})
test('addTrytes() adds trytes and returns correct transactions.', t => {

@@ -105,0 +134,0 @@ t.deepEqual(

@@ -15,7 +15,7 @@ /** @module bundle */

/**
* Creates a bunlde with given transaction entries.
* Creates a bundle with given transaction entries.
*
* @method createBundle
*
* @param {BundleEntry[]} entries - Entries of signle or multiple transactions with the same address
* @param {BundleEntry[]} entries - Entries of single or multiple transactions with the same address
*

@@ -26,3 +26,3 @@ * @return {Transaction[]} List of transactions in the bundle

/**
* Creates a bunlde with given transaction entries
* Adds given transaction entry to a bundle.
*

@@ -33,36 +33,36 @@ * @method addEntry

*
* @param {object} entry - Entry of single or multiple transactions with the same address
* @param {number} [entry.length=1] - Entry length, which indicates how many transactions in the bundle will occupy
* @param {string} [entry.address] - Address, defaults to all-9s
* @param {number} [entry.value = 0] - Value to transfer in _IOTAs_
* @param {string[]} [entry.signatureMessageFragments] - Array of signature message fragments trytes, defaults to all-9s
* @param {object} entry - Entry of a single or multiple transactions with the same address
* @param {number} [entry.length = 1] - Entry length, which indicates how many transactions in the bundle it will occupy
* @param {Hash} [entry.address] - Address, defaults to all-9s
* @param {number} [entry.value = 0] - Value to transfer in iotas
* @param {Trytes[]} [entry.signatureMessageFragments] - List of signature message fragments, defaults to all-9s
* @param {number} [entry.timestamp] - Transaction timestamp, defaults to `Math.floor(Date.now() / 1000)`
* @param {string} [entry.tag] - Optional Tag, defaults to null tag (all-9s)
*
* @return {Transaction[]} Bundle
* @return {Transaction[]} List of transactions in the updated bundle
*/
export declare const addEntry: (transactions: ReadonlyArray<Transaction>, entry: Partial<BundleEntry>) => ReadonlyArray<Transaction>;
/**
* Adds a list of trytes in the bundle starting at offset
* Adds signature message fragments to transactions in a bundle starting at offset.
*
* @method addTrytes
*
* @param {Transaction[]} transactions - Transactions in the bundle
* @param {Transaction[]} transactions - List of transactions in the bundle
*
* @param {Trytes[]} fragments - Message signature fragments to add
* @param {Trytes[]} fragments - List of signature message fragments to add
*
* @param {number} [offset=0] - Optional offset to start appending signature message fragments
* @param {number} [offset = 0] - Optional offset to start appending signature message fragments
*
* @return {Transaction[]} Transactions of finalized bundle
* @return {Transaction[]} List of transactions in the updated bundle
*/
export declare const addTrytes: (transactions: ReadonlyArray<Transaction>, fragments: ReadonlyArray<string>, offset?: number) => ReadonlyArray<Transaction>;
/**
* Finalizes the bundle by calculating the bundle hash
* Finalizes a bundle by calculating the bundle hash.
*
* @method finalizeBundle
*
* @param {Transaction[]} transactions - Transactions in the bundle
* @param {Transaction[]} transactions - List of transactions in the bundle
*
* @return {Transaction[]} Transactions of finalized bundle
* @return {Transaction[]} List of transactions in the finalized bundle
*/
export declare const finalizeBundle: (transactions: ReadonlyArray<Transaction>) => ReadonlyArray<Transaction>;

@@ -124,2 +124,3 @@ import * as Promise from 'bluebird';

readonly threshold: number;
readonly tips?: ReadonlyArray<Hash>;
}

@@ -126,0 +127,0 @@ export interface GetBalancesResponse {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc