Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

fabric-contract-api

Package Overview
Dependencies
Maintainers
1
Versions
221
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fabric-contract-api - npm Package Compare versions

Comparing version 1.4.0-beta2 to 1.4.0-rc1

lib/annotations/info.js

2

lib/annotations/index.js

@@ -16,2 +16,2 @@ /*

'use strict';
Object.assign(module.exports, require('./transaction'), require('./object'));
Object.assign(module.exports, require('./transaction'), require('./object'), require('./info'));

@@ -28,6 +28,6 @@ /*

this.__isContract = true;
if (name && name.trim() !== '') {
if (typeof name === 'undefined' || name === null) {
this.name = this.constructor.name;
} else {
this.name = name.trim();
} else {
this.name = '';
}

@@ -34,0 +34,0 @@ }

@@ -24,3 +24,3 @@ /*

*/
toBuffer(result) {
toBuffer(result, schema = {}) {

@@ -30,4 +30,14 @@ // relay on the default algorithms, including for Buffers. Just retunring the buffer

if (result) {
const payload = JSON.stringify(result);
return Buffer.from(payload);
// check that schema to see exactly how we should de-marshall this
if (schema.type && (schema.type === 'string' || schema.type === 'number')) {
// ok so this is a basic primitive type, and for strings and numbers the wireprotocol is different
// double check the type of the result passed in
if (schema.type !== typeof result) {
throw new Error(`Returned value is ${typeof result} does not match schema type of ${schema.type}`);
}
return Buffer.from(result.toString());
} else {
const payload = JSON.stringify(result);
return Buffer.from(payload);
}
} else {

@@ -53,19 +63,28 @@ return;

}
let value;
let jsonForValidation;
// check that schema to see exactly how we should de-marshall this
if (schema.type && (schema.type === 'string' || schema.type === 'number')) {
// ok so this is a basic primitive type, and for strings and numbers the wireprotocol is different
value = data.toString();
jsonForValidation = JSON.stringify(value);
} else {
const json = JSON.parse(data.toString());
if (json.type) {
if (json.type === 'Buffer') {
value = Buffer.from(json.data);
const json = JSON.parse(data.toString());
if (json.type) {
if (json.type === 'Buffer') {
value = Buffer.from(json.data);
} else {
throw new Error(`Type of ${json.type} is not understood, can't recreate data`);
}
} else {
throw new Error(`Type of ${json.type} is not understood, can't recreate data`);
value = json;
}
} else {
value = json;
// as JSON then this si the same
jsonForValidation = value;
}
return {value};
return {value, jsonForValidation};
}
};
{
"name": "fabric-contract-api",
"version": "1.4.0-beta2",
"tag": "beta",
"version": "1.4.0-rc1",
"tag": "latest",
"description": "A node.js implementation of Hyperledger Fabric chaincode shim, to allow endorsing peers and user-provided chaincodes to communicate with each other",

@@ -6,0 +6,0 @@ "main": "index.js",

@@ -64,4 +64,12 @@ /*

class SCBeta extends Contract {
/** */
constructor() {
super();
}
}
describe('contract.js', () => {

@@ -83,3 +91,3 @@

const sc0 = new Contract();
expect(sc0.getName()).to.equal('');
expect(sc0.getName()).to.equal('Contract');

@@ -115,2 +123,6 @@ // should also create default when the supplied name is empty space

expect(sc2.getName()).to.equal('somewhat.padded.out');
const sc3 = new SCBeta();
expect(sc3.name).to.equal('SCBeta');
expect(sc3.getName()).to.equal('SCBeta');
});

@@ -117,0 +129,0 @@

@@ -60,2 +60,9 @@ /*

const dataForValidation = [
'HelloWorld',
42,
{text:'hello', value: {i:'root -1'}},
Buffer.from('hello')
];
const buffer = [];

@@ -71,3 +78,3 @@

it ('should return undefined if nothing passed in ', () => {
it ('should return undefined if nothing passed in (no schema) ', () => {
const sc0 = new JSONSerializer();

@@ -77,6 +84,4 @@ expect(sc0.toBuffer()).to.be.equal(undefined);

it ('should return stringifed result', () => {
it ('should return stringifed result (no schema)', () => {
const sc0 = new JSONSerializer();
for (let i = 0; i < data.length; i++) {

@@ -86,2 +91,19 @@ expect(sc0.toBuffer(data[i])).to.deep.equal(buffer[i]);

});
it ('should return string from a string in result if schema given', () => {
const sc0 = new JSONSerializer();
expect(sc0.toBuffer('hello world', {type:'string'})).to.deep.equal(Buffer.from('hello world'));
});
it ('should return number from a number in result if schema given', () => {
const sc0 = new JSONSerializer();
expect(sc0.toBuffer(42, {type:'number'})).to.deep.equal(Buffer.from('42'));
});
it ('should throw an error if the type of data passed does not match schema given', () => {
const sc0 = new JSONSerializer();
(() => {
sc0.toBuffer(42, {type:'string'});
}).should.throw(/Returned value is .* does not match schema type of .*/);
});
});

@@ -101,6 +123,25 @@

for (let i = 0; i < data.length; i++) {
expect(sc0.fromBuffer(buffer[i])).to.deep.equal({value: data[i]});
expect(sc0.fromBuffer(buffer[i])).to.deep.equal({value: data[i], jsonForValidation: dataForValidation[i]});
}
});
it('should handle specific String case', () => {
const sc0 = new JSONSerializer();
const v = sc0.fromBuffer(Buffer.from('HelloWorld'), {type:'string'});
v.should.deep.equal({value:'HelloWorld', jsonForValidation:JSON.stringify('HelloWorld')});
});
it('should handle specific Number case', () => {
const sc0 = new JSONSerializer();
const v = sc0.fromBuffer(Buffer.from('102345679'), {type:'number'});
v.should.deep.equal({value:'102345679', jsonForValidation:JSON.stringify('102345679')});
});
it('should handle specific Number case', () => {
const sc0 = new JSONSerializer();
const v = sc0.fromBuffer(Buffer.from(JSON.stringify({'wibble':'wobble'})), {type:'whatever'});
v.should.deep.equal({value:{'wibble':'wobble'}, jsonForValidation:{'wibble':'wobble'}});
});
it ('should handle errors of unkown type', () => {

@@ -107,0 +148,0 @@ const sc0 = new JSONSerializer();

@@ -31,3 +31,4 @@ /*

export function Object(type?: string): (target: any) => void;
export function Info(info?: object): (target: any) => void;
export function Property(name?: string, type?: string): (target: any, propertyKey: string | symbol) => void;
}
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