Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
fix applyForDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
Rovak committed Apr 1, 2018
1 parent 7d12ab0 commit 59d8b8f
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 23 deletions.
53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"license": "GPL-3.0",
"dependencies": {
"axios": "^0.18.0",
"elliptic": "^6.4.0",
"google-protobuf": "^3.5.0",
"grpc": "^1.10.0",
"grpc-caller": "^0.4.0",
"grpc-tools": "^1.6.6",
"js-sha3": "^0.7.0",
"mocha": "^5.0.5",
"qs": "^6.5.1"
}
Expand Down
13 changes: 7 additions & 6 deletions src/client/http.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const xhr = require("axios");
const signTransaction = require("../crypto/crypto").signTransaction;
const byteArray2hexStr = require("../utils/bytes").byteArray2hexStr;
const deserializeTransaction = require("../protocol/serializer").deserializeTransaction;
const bytesToString = require("../utils/bytes").bytesToString;
Expand All @@ -8,6 +7,8 @@ const {Block, Transaction, Account} = require("../protocol/core/Tron_pb");
const {AccountList, NumberMessage, WitnessList, AssetIssueList} = require("../protocol/api/api_pb");
const {TransferContract} = require("../protocol/core/Contract_pb");
const qs = require("qs");
const passwordToAddress = require("../utils/crypto").passwordToAddress;
const { signTransaction } = require("../utils/crypto");

class HttpClient {

Expand Down Expand Up @@ -207,21 +208,21 @@ class HttpClient {
/**
* Apply for delegate
*
* @param address account address
* @param password account address
* @param url personal website url
*
* @returns {Promise<void>}
*/
async applyForDelegate(address, url) {
async applyForDelegate(password, url) {
let {data} = await xhr
.post(`${this.url}/createWitnessToView`, qs.stringify({
address,
ownerUrl: url,
address: passwordToAddress(password),
onwerUrl: url, // TODO yes this is spelled wrong :(
}));

let bytesDecode = base64DecodeFromString(data);
let transaction = Transaction.deserializeBinary(bytesDecode);
let transactionSigned = signTransaction(com_priKeyBytes, transaction);
let transactionSigned = signTransaction(base64DecodeFromString(password), transaction);
let transactionBytes = transactionSigned.serializeBinary();
let transactionString = byteArray2hexStr(transactionBytes);

Expand Down
20 changes: 13 additions & 7 deletions src/crypto/code.js → src/lib/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function isHexChar(c) {
/* Convert HEX string to byte array */

//16进制的ASCII字符串转为byteArray格式。
export function hexStr2byteArray(str) {
function hexStr2byteArray(str) {
var byteArray = Array();
var d = 0;
var j = 0;
Expand All @@ -221,8 +221,9 @@ export function hexStr2byteArray(str) {
return byteArray;
}


/* Convert a byte to string */
export function byte2hexStr(byte) {
function byte2hexStr(byte) {
var hexByteMap = "0123456789ABCDEF";
var str = "";
str += hexByteMap.charAt(byte >> 4);
Expand All @@ -233,7 +234,7 @@ export function byte2hexStr(byte) {
/* Convert byte arry to HEX string */

//byteArray格式数据转为16进制的ASCII字符串。
export function byteArray2hexStr(byteArray) {
function byteArray2hexStr(byteArray) {
var str = "";
for (var i = 0; i < (byteArray.length - 1); i++) {
str += byte2hexStr(byteArray[i]);
Expand All @@ -243,23 +244,22 @@ export function byteArray2hexStr(byteArray) {
}

//从base64字符串中解码出原文,格式为byteArray格式
export function base64DecodeFromString(string64) {
function base64DecodeFromString(string64) {
var b = new Base64();
var decodeBytes = b.decodeToByteArray(string64);
// var decodeBytes = stringToBytes(decodeString);
return decodeBytes;
}

//return baset64 String
//将byteArray格式数据编码为base64字符串
export function base64EncodeToString(bytes) {
function base64EncodeToString(bytes) {
// var string = bytesToString(bytes);
var b = new Base64();
var string64 = b.encodeIgnoreUtf8(bytes);
return string64
}

export function Base64() {
function Base64() {

// private property
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
Expand Down Expand Up @@ -506,3 +506,9 @@ function getStringType(str) {

return -1;
}

module.exports = {
base64EncodeToString,
base64DecodeFromString,
hexStr2byteArray,
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/utils/bytes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {Base64} = require("./base64");


/* Convert a byte to string */
function byte2hexStr(byte) {
var hexByteMap = "0123456789ABCDEF";
Expand Down
31 changes: 21 additions & 10 deletions src/crypto/crypto.js → src/utils/crypto.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable */
import {base64DecodeFromString, hexStr2byteArray} from "./code";
const base64EncodeToString = require("../lib/code").base64EncodeToString;
const byteArray2hexStr = require("./bytes").byteArray2hexStr;
const {base64DecodeFromString, hexStr2byteArray} = require("../lib/code");
const EC = require('elliptic').ec;
const CryptoJS = require("crypto-js");
import { sha3_256 } from 'js-sha3';
const { sha3_256 } = require('js-sha3');
const jsSHA = require("../lib/sha256");
const byte2hexStr = require("./bytes").byte2hexStr;

/**
* Sign A Transaction by priKey.
Expand All @@ -24,20 +26,17 @@ function signTransaction(priKeyBytes, transaction) {
return transaction;
}

exports.signTransaction = signTransaction

//return bytes of rowdata, use to sign.
function getRowBytesFromTransactionBase64(base64Data) {
let bytesDecode = base64DecodeFromString(base64Data);
let transaction = proto.protocol.Transaction.deserializeBinary(bytesDecode);
//toDO: assert ret is SUCESS
let raw = transaction.getRawData();
let rawBytes = raw.serializeBinary();
return rawBytes;
return raw.serializeBinary();
}

//gen Ecc priKey for bytes
export function genPriKey() {
function genPriKey() {
let ec = new EC('secp256k1');
let key = ec.genKeyPair();
let priKey = key.getPrivate();
Expand All @@ -61,7 +60,7 @@ function computeAddress(pubBytes) {
}

//return address by bytes, priKeyBytes is byte[]
export function getAddressFromPriKey(priKeyBytes) {
function getAddressFromPriKey(priKeyBytes) {
let pubBytes = getPubKeyFromPriKey(priKeyBytes);
let addressBytes = computeAddress(pubBytes);
return addressBytes;
Expand Down Expand Up @@ -135,3 +134,15 @@ function SHA256(msgBytes) {
let hashBytes = hexStr2byteArray(hashHex);
return hashBytes;
}

function passwordToAddress(password) {
let com_priKeyBytes = base64DecodeFromString(password);
let com_addressBytes = getAddressFromPriKey(com_priKeyBytes);
return byteArray2hexStr(com_addressBytes);
}


module.exports = {
signTransaction,
passwordToAddress,
};

0 comments on commit 59d8b8f

Please sign in to comment.