How to use @iota/transaction - 10 common examples

To help you get started, we’ve selected a few @iota/transaction examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github iotaledger / iota.js / packages / persistence / src / persistence.ts View on Github external
({ type, value }): PersistenceAdapterBatch => {
                                            switch (type) {
                                                case PersistenceBatchTypes.deleteBundle:
                                                    return {
                                                        type: PersistenceAdapterBatchTypes.write,
                                                        key: tritsToBytes(bundle(value)),
                                                        value: tritsToBytes(value),
                                                    }
                                                case PersistenceBatchTypes.deleteCDA:
                                                    return {
                                                        type: PersistenceAdapterBatchTypes.write,
                                                        key: tritsToBytes(CDAddress(value)),
                                                        value: tritsToBytes(value),
                                                    }
                                            }
                                            /* istanbul ignore next */
                                            return undefined as any
                                        }
                                    )
github iotaledger / iota.js / packages / bundle / src / index.ts View on Github external
export const finalizeBundle = (bundle: Int8Array): Int8Array => {
    if (!isMultipleOfTransactionLength(bundle.length)) {
        throw new Error(errors.ILLEGAL_TRANSACTION_BUFFER_LENGTH)
    }

    const sponge = new Kerl()
    const bundleCopy = bundle.slice()
    const bundleHash = new Int8Array(BUNDLE_LENGTH)

    // This block recomputes bundle hash by incrementing `obsoleteTag` field of first transaction in the bundle.
    // Normalized bundle should NOT contain value `13`.
    while (true) {
        // Absorb essence trits to squeeze bundle hash.
        for (let offset = 0; offset < bundle.length; offset += TRANSACTION_LENGTH) {
            sponge.absorb(transactionEssence(bundleCopy, offset), 0, TRANSACTION_ESSENCE_LENGTH)
        }

        // Set new bundle hash value.
github iotaledger / trinity-wallet / src / shared / libs / iota / sweeps.js View on Github external
.then((_signatureFragments) => {
            bundle.set(addSignatureOrMessage(bundle, _signatureFragments, 1));

            for (let offset = 0; offset < bundle.length; offset += Transaction.TRANSACTION_LENGTH) {
                cached.trytes.push(tritsToTrytes(bundle.subarray(offset, offset + Transaction.TRANSACTION_LENGTH)));
            }

            const convertToTransactionObjects = (tryteString) => iota.utils.transactionObject(tryteString);
            cached.transactionObjects = map(cached.trytes, convertToTransactionObjects);

            // Check if prepared bundle is valid, especially if its signed correctly.
            if (isBundle(cached.transactionObjects)) {
                return getTransactionsToApproveAsync(settings)();
            }

            throw new Error(Errors.INVALID_BUNDLE);
        })
        .then(({ trunkTransaction, branchTransaction }) => {
github iotaledger / trinity-wallet / src / shared / libs / iota / sweeps.js View on Github external
.then((_signatureFragments) => {
            bundle.set(addSignatureOrMessage(bundle, _signatureFragments, 1));

            for (let offset = 0; offset < bundle.length; offset += Transaction.TRANSACTION_LENGTH) {
                cached.trytes.push(tritsToTrytes(bundle.subarray(offset, offset + Transaction.TRANSACTION_LENGTH)));
            }

            const convertToTransactionObjects = (tryteString) => iota.utils.transactionObject(tryteString);
            cached.transactionObjects = map(cached.trytes, convertToTransactionObjects);

            // Check if prepared bundle is valid, especially if its signed correctly.
            if (isBundle(cached.transactionObjects)) {
                return getTransactionsToApproveAsync(settings)();
            }

            throw new Error(Errors.INVALID_BUNDLE);
        })
        .then(({ trunkTransaction, branchTransaction }) => {
github iotaledger / iota.js / packages / bundle / src / index.ts View on Github external
}

    if (issuanceTimestamp && issuanceTimestamp.length > ISSUANCE_TIMESTAMP_LENGTH) {
        throw new RangeError(errors.ILLEGAL_ISSUANCE_TIMESTAMP_LENGTH)
    }

    if (tag && tag.length > TAG_LENGTH) {
        throw new RangeError(errors.ILLEGAL_TAG_LENGTH)
    }

    const signatureOrMessageCopy = signatureOrMessage
        ? signatureOrMessage.slice()
        : new Int8Array(SIGNATURE_OR_MESSAGE_LENGTH)
    const numberOfFragments = signatureOrMessageCopy.length / SIGNATURE_OR_MESSAGE_LENGTH
    const bundleCopy = new Int8Array(bundle.length + numberOfFragments * TRANSACTION_LENGTH)
    const currentIndexBuffer = bundle.length > 0 ? increment(lastIndex(bundle)) : new Int8Array(LAST_INDEX_LENGTH)
    const lastIndexBuffer = currentIndexBuffer.slice()
    let fragmentIndex = 0

    bundleCopy.set(bundle.slice())

    // Create and append transactions to the bundle.
    for (let offset = bundle.length; offset < bundleCopy.length; offset += TRANSACTION_LENGTH) {
        const signatureOrMessageCopyFragment = signatureOrMessageCopy.subarray(
            fragmentIndex * SIGNATURE_OR_MESSAGE_LENGTH,
            (fragmentIndex + 1) * SIGNATURE_OR_MESSAGE_LENGTH
        )

        bundleCopy.set(signatureOrMessageCopyFragment, offset + SIGNATURE_OR_MESSAGE_OFFSET)

        if (address) {
            bundleCopy.set(address, offset + ADDRESS_OFFSET)
github iotaledger / iota.js / packages / bundle / src / index.ts View on Github external
export const finalizeBundle = (bundle: Int8Array): Int8Array => {
    if (!isMultipleOfTransactionLength(bundle.length)) {
        throw new Error(errors.ILLEGAL_TRANSACTION_BUFFER_LENGTH)
    }

    const sponge = new Kerl()
    const bundleCopy = bundle.slice()
    const bundleHash = new Int8Array(BUNDLE_LENGTH)

    // This block recomputes bundle hash by incrementing `obsoleteTag` field of first transaction in the bundle.
    // Normalized bundle should NOT contain value `13`.
    while (true) {
        // Absorb essence trits to squeeze bundle hash.
        for (let offset = 0; offset < bundle.length; offset += TRANSACTION_LENGTH) {
            sponge.absorb(transactionEssence(bundleCopy, offset), 0, TRANSACTION_ESSENCE_LENGTH)
        }

        // Set new bundle hash value.
        sponge.squeeze(bundleHash, 0, BUNDLE_LENGTH)

        // Stop mutation if essence results to secure bundle.
        if (normalizedBundle(bundleHash).indexOf(MAX_TRYTE_VALUE /* 13 */) === -1) {
            // Essence results to secure bundle.
            break
        }

        // Essence results to insecure bundle. (Normalized bundle hash contains value `13`.)
        bundleCopy.set(
            increment(bundleCopy.subarray(OBSOLETE_TAG_OFFSET, OBSOLETE_TAG_OFFSET + OBSOLETE_TAG_LENGTH)),
            OBSOLETE_TAG_OFFSET
        )
github iotaledger / iota.js / packages / account / src / preset.ts View on Github external
getLatestInclusion(pastAttachmentHashes).tap(inclusionStates => {
                        if (inclusionStates.indexOf(true) > -1) {
                            return persistence.del(['0', tritsToTrytes(bundleHash(bundle))].join(':'))
                        }

                        return Promise.all(pastAttachmentHashes.map(h => getConsistency([h]))).tap(consistencyStates =>
                            consistencyStates.indexOf(true) > -1
                                ? setTimeout(() => bundles.write(bundle), delay)
                                : getTransactionsToApprove(depth, reference ? reference.hash : undefined)
                                      .tap(transactionsToApprove =>
                                          this.emit(Events.getTransactionsToApprove, {
                                              trytes: bundleTritsToBundleTrytes(bundle),
                                              transactionsToApprove,
                                          })
                                      )
                                      .then(
                                          ({
                                              trunkTransaction,
                                              branchTransaction,
github iotaledger / iota.js / packages / persistence / src / persistence.ts View on Github external
                        .then(() => state.delete(tritsToBytes(bundle(buffer))))
                        .tap(() => this.emit(PersistenceEvents.deleteBundle, buffer))
github iotaledger / iota.js / packages / account / src / account.ts View on Github external
persistence.on('data', ({ key, value }) => {
            const trits = Int8Array.from(value)
            const [prefix, id] = key.toString().split(':')
            if (prefix === '0') {
                if (isMultipleOfTransactionLength(trits.length)) {
                    bundles.write(trits)

                    const bundle = []
                    for (let offset = 0; offset < trits.length; offset += TRANSACTION_LENGTH) {
                        bundle.push(
                            asTransactionObject(tritsToTrytes(trits.slice(offset, offset + TRANSACTION_LENGTH)))
                        )
                    }
                    withdrawalsList.push(bundle)
                }

                if (trits.length === CDA_LENGTH) {
                    deposits.write(trits)
                    const cda = deserializeCDAInput(trits)
                    depositsList.push(cda)
                    addresses.push(tritsToTrytes(cda.address))
github iotaledger / iota.js / packages / multisig / src / multisig.ts View on Github external
public addSignature(bundle: Int8Array, inputAddress: string, keyTrits: Int8Array, callback: Callback) {
        const bundleHashTrits = bundleHash(bundle)
        const normalizedBundleHash = normalizedBundle(bundleHashTrits)
        let signatureIndex = 0

        for (const offset = 0; offset < bundle.length * TRANSACTION_LENGTH; offset + TRANSACTION_LENGTH) {
            if (tritsToTrytes(address(bundle)) === inputAddress && isNinesTrytes(signatureOrMessage(bundle))) {
                const signature = new Int8Array(keyTrits.length)

                for (let i = 0; i < keyTrits.length / FRAGMENT_LENGTH; i++) {
                    signature.set(
                        signatureFragment(
                            normalizedBundleHash.slice(
                                i * NORMALIZED_FRAGMENT_LENGTH,
                                (i + 1) * NORMALIZED_FRAGMENT_LENGTH
                            ),
                            keyTrits.slice(i * FRAGMENT_LENGTH, (i + 1) * FRAGMENT_LENGTH)
                        ),