Skip to content

Commit

Permalink
Merge pull request #60 from boguslavsky/readme-fixes
Browse files Browse the repository at this point in the history
fix README
  • Loading branch information
boguslavsky authored Sep 13, 2017
2 parents 339c0ae + 1cd67f6 commit 86145a7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@ var hash = Exonum.hash(data, User);
To sign it:

```javascript
var secretKey = '07038584a4a77510ea5eced45f54dc030f5864ab6a5a2190666b47c676bcf15a' +
'1f2f07703c5bcafb5749aa735ce8b7c366752be882314f5bbbc9a6af2ae634fc';
var secretKey = '978e3321bd6331d56e5f4c2bdb95bf471e95a77a6839e68d4241e7b0932ebe2b' +
'fa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a';
var signature = Exonum.sign(secretKey, data, User);
```

To verify signature:

```javascript
var signature = '07038584a4a77510ea5eced45f54dc030f5864ab6a5a2190666b47c676bcf15a' +
'1f2f07703c5bcafb5749aa735ce8b7c366752be882314f5bbbc9a6af2ae634fc';
var publicKey = '6752be882314f5bbbc9a6af2ae634fc07038584a4a77510ea5eced45f54dc030';
var signature = 'c1db9a5f01ebdff27e02652a9aae5c9a4ac88787587dabceb9f471ae0b8e051b' +
'9632dfd26922f6abf24ff2d3275028fe286703d25ee7fe6b1711e89af4a7d307';
var publicKey = 'fa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a';
var result = Exonum.verifySignature(signature, publicKey, data, User);
```

Expand Down Expand Up @@ -170,17 +170,15 @@ Here is an example of 'SendFunds' transaction:

```javascript
var SendFunds = Exonum.newMessage({
size: 24,
size: 72,
network_id: 0,
protocol_version: 0,
service_id: 0,
message_id: 0,
signature: '07038584a4a77510ea5eced45f54dc030f5864ab6a5a2190666b47c676bcf15a' +
'1f2f07703c5bcafb5749aa735ce8b7c366752be882314f5bbbc9a6af2ae634fc',
fields: {
from: {type: Exonum.Hash, size: 8, from: 0, to: 8},
to: {type: Exonum.Hash, size: 8, from: 8, to: 16},
amount: {type: Exonum.Uint64, size: 8, from: 16, to: 24}
from: {type: Exonum.Hash, size: 32, from: 0, to: 32},
to: {type: Exonum.Hash, size: 32, from: 32, to: 64},
amount: {type: Exonum.Uint64, size: 8, from: 64, to: 72}
}
});
```
Expand All @@ -203,11 +201,20 @@ var buffer = SendFunds.serialize(data);
Data of type `SendFunds` can be signed with secret key:

```javascript
var secretKey = '07038584a4a77510ea5eced45f54dc030f5864ab6a5a2190666b47c676bcf15a' +
'1f2f07703c5bcafb5749aa735ce8b7c366752be882314f5bbbc9a6af2ae634fc';
var secretKey = '978e3321bd6331d56e5f4c2bdb95bf471e95a77a6839e68d4241e7b0932ebe2b' +
'fa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a';
var signature = SendFunds.sign(secretKey, data);
```

To verify signature:

```javascript
var signature = '8ff692b47d17f7738ec2d19f5296d3810909a62fe1480eb752b531f8e17056f6' +
'578985090db822612f88c3bcad4f0539401836e5ad58913c489784ff3e415a0b';
var publicKey = 'fa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a';
var result = SendFunds.verifySignature(signature, publicKey, data);
```

## Cryptography

### Calculate hash
Expand Down Expand Up @@ -401,10 +408,10 @@ For unsafe numbers use `string` only.*

| Name | Type | Size (bytes) | Description |
|---|---|---|---|
| Uint8 | `number` | `1` | Signed integer in a range from `0` to `255` |
| Int16 | `number` | `2` | Signed integer in a range from `0` to `65535` |
| Int32 | `number` | `4` | Signed integer in a range from `0` to `4294967295` |
| Int64 | `number` or `string`* | `8` | Signed integer in a range from `0` to `18446744073709551615` |
| Uint8 | `number` | `1` | Unsigned integer in a range from `0` to `255` |
| Uint16 | `number` | `2` | Unsigned integer in a range from `0` to `65535` |
| Uint32 | `number` | `4` | Unsigned integer in a range from `0` to `4294967295` |
| Uint64 | `number` or `string`* | `8` | Unsigned integer in a range from `0` to `18446744073709551615` |

*\*Note that JavaScript limits minimum and maximum integer number.
Maximum safe integer in JavaScript is `2^53-1` which is equal to `9007199254740991`.
Expand Down
66 changes: 66 additions & 0 deletions test/readme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-env node, mocha */

var expect = require('chai').expect;
var Exonum = require('..');

describe('Examples from README.md', function() {
describe('Custom type section', function() {
var User = Exonum.newType({
size: 9,
fields: {
name: {type: Exonum.String, size: 8, from: 0, to: 8},
age: {type: Exonum.Int8, size: 1, from: 8, to: 9}
}
});
var data = {
name: 'Tom',
age: 34
};
var keyPair = {
publicKey: 'fa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a',
secretKey: '978e3321bd6331d56e5f4c2bdb95bf471e95a77a6839e68d4241e7b0932ebe2bfa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a'
};
var signature = 'c1db9a5f01ebdff27e02652a9aae5c9a4ac88787587dabceb9f471ae0b8e051b9632dfd26922f6abf24ff2d3275028fe286703d25ee7fe6b1711e89af4a7d307';

it('should sign custom type', function() {
expect(Exonum.sign(keyPair.secretKey, data, User)).to.equal(signature);
});

it('should verify custom type signature', function() {
expect(Exonum.verifySignature(signature, keyPair.publicKey, data, User)).to.be.true;
});
});

describe('Transaction section', function() {
var SendFunds = Exonum.newMessage({
size: 72,
network_id: 0,
protocol_version: 0,
service_id: 0,
message_id: 0,
fields: {
from: {type: Exonum.Hash, size: 32, from: 0, to: 32},
to: {type: Exonum.Hash, size: 32, from: 32, to: 64},
amount: {type: Exonum.Uint64, size: 8, from: 64, to: 72}
}
});
var data = {
from: '6752be882314f5bbbc9a6af2ae634fc07038584a4a77510ea5eced45f54dc030',
to: 'f5864ab6a5a2190666b47c676bcf15a1f2f07703c5bcafb5749aa735ce8b7c36',
amount: 50
};
var keyPair = {
publicKey: 'fa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a',
secretKey: '978e3321bd6331d56e5f4c2bdb95bf471e95a77a6839e68d4241e7b0932ebe2bfa7f9ee43aff70c879f80fa7fd15955c18b98c72310b09e7818310325050cf7a'
};
var signature = '8ff692b47d17f7738ec2d19f5296d3810909a62fe1480eb752b531f8e17056f6578985090db822612f88c3bcad4f0539401836e5ad58913c489784ff3e415a0b';

it('should sign transaction', function() {
expect(SendFunds.sign(keyPair.secretKey, data)).to.equal(signature);
});

it('should verify transaction signature', function() {
expect(SendFunds.verifySignature(signature, keyPair.publicKey, data)).to.be.true;
});
});
});

0 comments on commit 86145a7

Please sign in to comment.