Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service marketplace: encode / decode hash to base58 #828

Merged
merged 6 commits into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions systemservices/marketplace/package-lock.json

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

1 change: 1 addition & 0 deletions systemservices/marketplace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@types/request": "^2.48.1",
"@types/request-promise-native": "^1.0.15",
"@types/web3": "^1.0.18",
"base-x": "^3.0.5",
"bignumber.js": "^8.1.1",
"mesg-js": "^2.0.1",
"request": "^2.88.0",
Expand Down
23 changes: 11 additions & 12 deletions systemservices/marketplace/src/contracts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Web3 from "web3"
import BigNumber from "bignumber.js";
import Contract from "web3/eth/contract";
import { TaskInputs } from "mesg-js/lib/service";
import Web3 from 'web3'
import BigNumber from 'bignumber.js';
import Contract from 'web3/eth/contract';
import { TaskInputs } from 'mesg-js/lib/service';
const base58 = require('base-x')('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved

BigNumber.config({ EXPONENTIAL_AT: 100 })

const hexToAscii = (x: string) => {
if (!x) return ""
if (!x) return ''
return Web3.utils.hexToAscii(x).replace(/\u0000/g, '')
}

Expand All @@ -24,13 +25,11 @@ const fromUnit = (x: string|BigNumber) => new BigNumber(x).dividedBy(1e18)

const parseTimestamp = (x: string) => new Date(new BigNumber(x).times(1000).toNumber())

const hashToHex = (x: string) => {
if (x.startsWith('0x')) {
return x
}
return '0x' + x
const hashToHex = (x: string): string => {
if (x.startsWith('0x')) throw new Error('hash format is invalid. It starts with 0x')
return '0x' + base58.decode(x).toString('hex')
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
}
const hexToHash = (x: string) => x.replace(/^0x/g, '')//.replace(/0*$/g, '')
const hexToHash = (x: string): string => base58.encode(Buffer.from(x.replace(/^0x/, ''), 'hex'))

interface CreateTransaction {
(
Expand Down Expand Up @@ -58,7 +57,7 @@ const createTransactionTemplate = (
nonce: (await web3.eth.getTransactionCount(inputs.from)) + (shiftNonce || 0),
gas: inputs.gas || defaultGas,
gasPrice: inputs.gasPrice || defaultGasPrice,
value: "0",
value: '0',
data: data
}
}
Expand Down