parent
d97ed89a84
commit
d4fc858aac
@ -1,15 +0,0 @@ |
||||
import EthUtil from 'ethereumjs-util'; |
||||
import { |
||||
web3 |
||||
} from './util'; |
||||
|
||||
/** |
||||
* generates the ethereum-adress of the publicKey |
||||
* We create the checksum-adress which is case-sensitive |
||||
* @returns {string} address |
||||
*/ |
||||
export default function addressByPublicKey(publicKey) { |
||||
const addressBuffer = EthUtil.pubToAddress(new Buffer(publicKey, 'hex')); |
||||
const checkSumAdress = web3.utils.toChecksumAddress(addressBuffer.toString('hex')); |
||||
return checkSumAdress; |
||||
} |
@ -1,20 +1,13 @@ |
||||
import { |
||||
web3 |
||||
} from './util'; |
||||
import publicKeyByPrivateKey from './public-key-by-private-key'; |
||||
|
||||
import Account from 'eth-lib/lib/account'; |
||||
|
||||
/** |
||||
* creates a new object with |
||||
* private-, public-Key and address |
||||
*/ |
||||
export default function createIdentity() { |
||||
const account = web3.eth.accounts.create(); |
||||
|
||||
const identity = { |
||||
address: account.address, |
||||
privateKey: account.privateKey, |
||||
publicKey: publicKeyByPrivateKey(account.privateKey) |
||||
}; |
||||
|
||||
const identity = Account.create(); |
||||
identity.publicKey = publicKeyByPrivateKey(identity.privateKey); |
||||
return identity; |
||||
} |
||||
|
@ -0,0 +1,48 @@ |
||||
import { |
||||
publicKeyConvert |
||||
} from 'secp256k1'; |
||||
import ethUtil from 'ethereumjs-util'; |
||||
|
||||
export function compress(startsWith04) { |
||||
|
||||
// add trailing 04 if not done before
|
||||
const testBuffer = new Buffer(startsWith04, 'hex'); |
||||
if (testBuffer.length === 64) startsWith04 = '04' + startsWith04; |
||||
|
||||
|
||||
return publicKeyConvert( |
||||
new Buffer(startsWith04, 'hex'), |
||||
true |
||||
).toString('hex'); |
||||
}; |
||||
|
||||
export function decompress(startsWith02Or03) { |
||||
|
||||
// if already decompressed an not has trailing 04
|
||||
const testBuffer = new Buffer(startsWith02Or03, 'hex'); |
||||
if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03; |
||||
|
||||
let decompressed = publicKeyConvert( |
||||
new Buffer(startsWith02Or03, 'hex'), |
||||
false |
||||
).toString('hex'); |
||||
|
||||
// remove trailing 04
|
||||
decompressed = decompressed.substring(2); |
||||
return decompressed; |
||||
}; |
||||
|
||||
/** |
||||
* generates the ethereum-adress of the publicKey |
||||
* We create the checksum-adress which is case-sensitive |
||||
* @returns {string} address |
||||
*/ |
||||
export function toAddress(publicKey) { |
||||
|
||||
// normalize key
|
||||
publicKey = decompress(publicKey); |
||||
|
||||
const addressBuffer = ethUtil.pubToAddress(new Buffer(publicKey, 'hex')); |
||||
const checkSumAdress = ethUtil.toChecksumAddress(addressBuffer.toString('hex')); |
||||
return checkSumAdress; |
||||
} |
Loading…
Reference in new issue