Skip to content

Commit

Permalink
Fix work report files' cid (#65)
Browse files Browse the repository at this point in the history
* Fix work report files

* Ignore node modules' lint

* Fix lint ignore file

* Remove precompile for command

* ignore all js file
  • Loading branch information
badkk authored Jan 7, 2021
1 parent 3a9b7c6 commit 8eccb43
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 40 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
*.js
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
"lint": "gts lint",
"clean": "gts clean",
"compile": "tsc",
"fix": "gts fix",
"prepare": "yarn run compile",
"pretest": "yarn run compile",
"posttest": "yarn run lint"
"fix": "gts fix"
},
"dependencies": {
"@polkadot/api": "^3.3.1",
Expand Down
4 changes: 2 additions & 2 deletions src/services/swork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {ApiPromise} from '@polkadot/api';
import {Request} from 'express';
import {KeyringPair} from '@polkadot/keyring/types';
import {sendTx, queryToObj} from './util';
import {sendTx, queryToObj, strToHex} from './util';
import {logger} from '../log';

/**
Expand Down Expand Up @@ -35,7 +35,7 @@ export async function reportWorks(
const pk = '0x' + req.body['pub_key'];
const fileParser = (file: any) => {
const rst: [string, number, number] = [
'0x' + file.hash,
strToHex(file.cid),
file.size,
file.c_block_num,
];
Expand Down
36 changes: 2 additions & 34 deletions src/services/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,6 @@ export function sleep(time: number) {
return new Promise(resolve => setTimeout(resolve, time));
}

export function getHexValue(hexStr: string) {
if (hexStr.substring(0, 2) !== '0x') {
return null;
}
const realHexStr = hexStr.substring(2);
const bytes = hexStrToBytes(realHexStr);

let result = '';
if (bytes) {
for (let i = 0; i < bytes.length; i++) {
result += String.fromCharCode(bytes[i]);
}
}
return result;
}

/**
* Private functions
*/
Expand All @@ -149,22 +133,6 @@ function getAccountInfo(req: Request): [string, string] {
return [backup, password];
}

// Hex string to bytes
function hexStrToBytes(str: string) {
let pos = 0;
let len = str.length;
if (len % 2 !== 0) {
return null;
}

len /= 2;
const hex = [];
for (let i = 0; i < len; i++) {
const s = str.substr(pos, 2);
const v = parseInt(s, 16);
hex.push(v);
pos += 2;
}

return hex;
export function strToHex(str: string): string {
return '0x' + Buffer.from(str).toString('hex');
}

0 comments on commit 8eccb43

Please sign in to comment.