import { Buffer } from "buffer/" import BinTools from "../../src/utils/bintools" import { KeyChain, KeyPair } from "src/apis/evm" import createHash from "create-hash" const bintools: BinTools = BinTools.getInstance() const signMessage = (message: string, keyPair: KeyPair) => { const digest = createHash("sha256").update(message).digest() const digestHex = digest.toString("hex") const digestBuff = Buffer.from(digestHex, "hex") const signature = keyPair.sign(digestBuff) const sHex = signature.toString("hex") const rsHex = sHex.slice(0, -2) return rsHex } describe("SECP256K1", (): void => { test("addressFromPublicKey", (): void => { const pubkeys: string[] = [ "7ECaZ7TpWLq6mh3858DkR3EzEToGi8iFFxnjY5hUGePoCHqdjw", "5dS4sSyL4dHziqLYanMoath8dqUMe6ZkY1VbnVuQQSsCcgtVET" ] const addrs: string[] = [ "b0c9654511ebb78d490bb0d7a54997d4a933972c", "d5bb99a29e09853da983be63a76f02259ceedf15" ] pubkeys.forEach((pubkey: string, index: number): void => { const pubkeyBuf: Buffer = bintools.cb58Decode(pubkey) const addrBuf: Buffer = KeyPair.addressFromPublicKey(pubkeyBuf) expect(addrBuf.toString("hex")).toBe(addrs[index]) }) }) test("lattice-test", (): void => { const vectors = [ { skHex: "0000000000000000000000000000000000000000000000000000000000000001", msg: "Everything should be made as simple as possible, but not simpler.", rsHex: "33a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c96f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa54342262" }, { skHex: "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140", msg: "Equations are more important to me, because politics is for the present, but an equation is something for eternity.", rsHex: "54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed07082304410efa6b2943111b6a4e0aaa7b7db55a07e9861d1fb3cb1f421044a5" }, { skHex: "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140", msg: "Not only is the Universe stranger than we think, it is stranger than we can think.", rsHex: "ff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd06fc95f5132e5ecfdc8e5e6e616cc77151455d46ed48f5589b7db7771a332b283" }, { skHex: "0000000000000000000000000000000000000000000000000000000000000001", msg: "How wonderful that we have met with a paradox. Now we have some hope of making progress.", rsHex: "c0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d375afdc06b7d6322a590955bf264e7aaa155847f614d80078a90292fe205064d3" }, { skHex: "69ec59eaa1f4f2e36b639716b7c30ca86d9a5375c7b38d8918bd9c0ebc80ba64", msg: "Computer science is no more about computers than astronomy is about telescopes.", rsHex: "7186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d0de0b38e06807e46bda1f1e293f4f6323e854c86d58abdd00c46c16441085df6" }, { skHex: "00000000000000000000000000007246174ab1e92e9149c6e446fe194d072637", msg: "...if you aren't, at any given time, scandalized by code you wrote five or even three years ago, you're not learning anywhere near enough", rsHex: "fbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda4870e68880ebb0050fe4312b1b1eb0899e1b82da89baa5b895f612619edf34cbd37" }, { skHex: "000000000000000000000000000000000000000000056916d0f9b31dc9b637f3", msg: "The question of whether computers can think is like the question of whether submarines can swim.", rsHex: "cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf906ce643f5049ee1f27890467b77a6a8e11ec4661cc38cd8badf90115fbd03cef" } ] vectors.forEach((vec) => { const pk = vec.skHex const pkCB58 = `PrivateKey-` + bintools.cb58Encode(Buffer.from(pk, "hex")) const keyChain = new KeyChain("dcm", "ACT") const keyPair = keyChain.importKey(pkCB58) const signature = signMessage(vec.msg, keyPair) expect(signature).toBe(vec.rsHex) }) }) })