This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
convert.js
129 lines (108 loc) · 2.62 KB
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright © 2017 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
import bignum from 'browserify-bignum';
import ed2curve from 'ed2curve';
import { getSha256Hash } from './hash';
import { getTransactionBytes } from '../transactions/transactionBytes';
/**
* @method bufferToHex
* @param buffer
*
* @return {string}
*/
export function bufferToHex(buffer) {
return naclInstance.to_hex(buffer);
}
/**
* @method hexToBuffer
* @param hex
*
* @return {buffer}
*/
export function hexToBuffer(hex) {
return naclInstance.from_hex(hex);
}
/**
* @method getFirstEightBytesReversed
* @param publicKeyBytes
*
* @return {buffer}
*/
export function getFirstEightBytesReversed(publicKeyBytes) {
return Buffer.from(publicKeyBytes)
.slice(0, 8)
.reverse();
}
/**
* @method toAddress
* @param buffer
*
* @return {string}
*/
export function toAddress(buffer) {
return `${bignum.fromBuffer(buffer).toString()}L`;
}
/**
* @method getAddressFromPublicKey
* @param publicKey
*
* @return {string}
*/
export function getAddressFromPublicKey(publicKey) {
const publicKeyHash = getSha256Hash(publicKey, 'hex');
const publicKeyTransform = getFirstEightBytesReversed(publicKeyHash);
const address = toAddress(publicKeyTransform);
return address;
}
/**
* @method getAddress
* @param publicKey string
*
* @return {string}
*/
export function getAddress(publicKey) {
return getAddressFromPublicKey(publicKey);
}
/**
* @method getId
* @param transaction Object
*
* @return {string}
*/
export function getId(transaction) {
const transactionBytes = getTransactionBytes(transaction);
const transactionHash = getSha256Hash(transactionBytes);
const bufferFromFirstEntriesReversed = getFirstEightBytesReversed(transactionHash);
const firstEntriesToNumber = bignum.fromBuffer(bufferFromFirstEntriesReversed);
return firstEntriesToNumber.toString();
}
/**
* @method convertPublicKeyEd2Curve
* @param publicKey
*
* @return {object}
*/
export function convertPublicKeyEd2Curve(publicKey) {
return ed2curve.convertPublicKey(publicKey);
}
/**
* @method convertPrivateKeyEd2Curve
* @param privateKey
*
* @return {object}
*/
export function convertPrivateKeyEd2Curve(privateKey) {
return ed2curve.convertSecretKey(privateKey);
}