Closed 0xJackJun closed 1 year ago
maybe you should let strings to bytes transfer; below is my tset typescript script
import dotenv from "dotenv";
dotenv.config();
import { AptosAccount, AptosAccountObject} from "aptos";
const aptosAccountObject: AptosAccountObject = {
address: "0x978c213990c4833df71548df7ce49d54c759d6b6d932de22b24d56060b7af2aa",
privateKeyHex:
// eslint-disable-next-line max-len
"0xc5338cd251c22daa8c9c9cc94f498cc8a5c7e1d2e75287a5dda91096fe64efa5de19e5d1880cac87d57484ce9ed2e84cf0f9599f12e7cc3a52e4e7657a763f2c",
publicKeyHex: "0xde19e5d1880cac87d57484ce9ed2e84cf0f9599f12e7cc3a52e4e7657a763f2c",
};
(async () => {
const alice = AptosAccount.fromAptosAccountObject(aptosAccountObject);
// Print out account addresses.
console.log("=== Addresses ===");
console.log(`Alice: ${alice.address()}`);
const msg = "2105.nonce_geek";
const textEncoder = new TextEncoder();
let str_bytes = textEncoder.encode(msg);
console.log("Alice pubkey:", alice.pubKey());
let signature = alice.signBuffer(str_bytes);
console.log("signature:", signature);
})();
signBuffer function in aptos-core project ecosystem/typescript/sdk/src/aptos_account.ts ; implementation is below; it use nacl library
/**
* Signs specified `buffer` with account's private key
* @param buffer A buffer to sign
* @returns A signature HexString
*/
signBuffer(buffer: Uint8Array): HexString {
const signature = nacl.sign(buffer, this.signingKey.secretKey);
return HexString.fromUint8Array(signature.slice(0, 64));
}
ed25519_sign_verify_test Using @noble/ed25519(node.js) can't pass this test, but using crypto/ed25519 can pass the test.
=====================example code======================= package main
import ( "crypto/ed25519" "encoding/hex" "fmt" )
func sign(msg string) { var pub []byte var pri []byte var err error if pub, pri, err = ed25519.GenerateKey(nil); err != nil { err = fmt.Errorf("ed25519.GenerateKey failed", err.Error()) return } msgByte := []byte(msg) sig := ed25519.Sign(pri, msgByte) verify := ed25519.Verify(pub, msgByte, sig)
}
func main() { var msg = "test" sign(msg) }