Difference between js and dart to generate an address with a private key

152 views Asked by At

I would like to write this function written in js to dart

Function in js


import {ec as EC} from 'elliptic'
import secp256k1 from 'secp256k1'
import sha3 from 'js-sha3'

const ec = new EC('secp256k1')

export function privateKeyToAddress(key) {
  const pubKey = ec
    .keyFromPrivate(key)
    .getPublic()
    .encode('array')

  return toHexString(sha3.keccak_256.array(pubKey.slice(1)).slice(12), true)
}

I tried to use plugins in pub spec.yaml:

  secp256k1: ^0.2.2
  sha3: ^0.1.2
  quartet: ^0.1.1

and i coded in dart:

String privateKey = "...."; // add a private key value
var pk = PrivateKey.fromHex(privateKey);
var pub = pk.publicKey;
var k = SHA3(256, KECCAK_PADDING, 256);
k.update(utf8.encode(slice(pub.toCompressedHex(), 1)));
var address = slice(HEX.encode(k.digest()), 12);
    

the result (eq. address value) between js function and dart function is not the same with a same private key value...

Could you help me please

0

There are 0 answers