# configtxlator.js

> Hyperledger Fabric's configtxlator implemented in js as a library to use with fabric-sdk-node

Latest version **0.1.1** (published 2019-06-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install configtxlator.js
pnpm add configtxlator.js
yarn add configtxlator.js
bun add configtxlator.js
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2019-06-01 |
| First published | 2019-06-01 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.8 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Manoel Vieira |
| Maintainers | vieiramanoel |
| Keywords | hyperledger, fabric, configtxlator |

## Links

- npm: https://www.npmjs.com/package/configtxlator.js
- Repository: https://github.com/GoLedgerDev/configtxlator.js
- Homepage: https://github.com/GoLedgerDev/configtxlator.js#readme
- Issues: https://github.com/GoLedgerDev/configtxlator.js/issues
- npm.io page: https://npm.io/package/configtxlator.js

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [fabric-protos](https://npm.io/package/fabric-protos.md) ^2.0.0-snapshot.1
- [camelcase-keys-deep](https://npm.io/package/camelcase-keys-deep.md) ^0.1.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2019-06-01
- 0.1.0 — 2019-06-01
- 0.0.1 — 2019-06-01

## README

# configtxlator.js

Hyperledger Fabric's configtxlator implemented as a js library

## Motivation

If you are here you want to decode your block and convert it into a javascript object through fabric-sdk-node without using a local server of configtxlator in order to do it, then you came to the right place!

This first version will allow you to append a organization to a configblock given its json generated by configtxgen.

## Install

You can install this lib by

`npm i configtxlator.js`

if you're using typescript

`npm i @types/configtxlator.js`

## Adding an org to a channel

First get block.json from channel:

```Typescript

const block = await Channel.getChannelConfigFromOrderer()
//Replace snake_case to camelCase and sequence/version tags to int
//otherwise protobuf won't work
const channelBlock = JSON.parse(
    block.encodeJSON()
         .replace(/(_\w)\w+":/g, (match: String) => match[1].toUpperCase() + match.substring(2)),
    (key, value) =>
        key === "sequence" ? parseInt(value) : key === "version" ? parseInt(value) : value
);
// Create a replica that will be modified using same process
const newChannelBlock = JSON.parse(
    block.encodeJSON()
         .replace(/(_\w)\w+":/g, (match: String) => match[1].toUpperCase() + match.substring(2)),
    (key, value) =>
        key === "sequence" ? parseInt(value) : key === "version" ? parseInt(value) : value
);

```

> Those parses are necessary once `block.EncodeJSON()` from fabric lib returns a json where all key entries are
> snake_case instead of camelCase (protobufjs doesn't recognizes snake_case entries).

Convert both `channelBlock` and `newChannelBlock` into `common.Config` protos:

```Typescript

const configBlock = fabProtos.common.Config.create({
    channelGroup: channelBlock.config.channelGroup,
    sequence: channelBlock.config.sequence
});
const newConfigBlock = fabProtos.common.Config.create({
    channelGroup: newChannelBlock.config.channelGroup,
    sequence: channelBlock.config.sequence
});

```

Convert new org's JSON got from configtxgen into a `common.Config` proto:

```Typescript

import configtxlator = require('configtxlator.js');
const translatedNewOrg = configtxlator.convertOrgJsonToConfigGroup(
    orgJson
);

```

Append new org config into application groups:

```Typescript

const orgs = newConfigBlock.channelGroup.groups.Application.groups;
newConfigBlock.channelGroup.groups.Application.groups = {
    ...orgs,
    [orgName]: translatedNewOrg
};

```

Create protos object back from protos interfaces:

```Typescript

const originalConfig = fabProtos.common.Config.create(configBlock);
const updatedConfig = fabProtos.common.Config.create(newConfigBlock);

```

Finally, compute delta set between both configs:

```Typescript

updatedBlock = await configtxlator.computeDeltaSet(originalConfig, updatedConfig);

updatedBlock.channelId = channelName; //Channel name is the channel you're trying to modify

//Convert updatedBlock into a Uint8Array
const updatedIntArray = fabProtos.common.ConfigUpdate.encode(updatedBlock).finish();

//Convert from Uint8Array to js Buffer
const updatedBuffer = Buffer.from(updatedIntArray, 0);

//Sign updatedChannelConfig buffer
const signatures = [Client.signChannelConfig(updatedBuffer)];

```

And then submit a updateChannel request proposal through fabric-sdk-node

---
_Source: https://npm.io/package/configtxlator.js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
