Skip to content

Instantly share code, notes, and snippets.

@joyqvq
Last active August 26, 2022 18:29
Show Gist options
  • Save joyqvq/89aa06ee3b97b66323568770ef9d4dfb to your computer and use it in GitHub Desktop.
Save joyqvq/89aa06ee3b97b66323568770ef9d4dfb to your computer and use it in GitHub Desktop.
Steps to test ecdsa move contract e2e
# Test Signing Key Scheme
1. Run faucet and rpc-server:
```jsx
cargo install --path crates/sui-test-validator
sui-test-validator --faucet-port=1234
Fullnode RPC URL: http://127.0.0.1:9000
Fullnode Websocket URL: 127.0.0.1:9001
Gateway RPC URL: http://127.0.0.1:5001
Faucet URL: http://127.0.0.1:1234
```
1. (If needed) Remove client and keystore to start fresh
```jsx
rm /Users/joy/.sui/sui_config/client.yaml
rm /Users/joy/.sui/sui_config/sui.keystore
```
3. Generate client.yaml pointing to a desired key
```jsx
sui client  101 ↵  5355  19:36:54
Config file ["/Users/joy/.sui/sui_config/client.yaml"] doesn't exist, do you want to connect to a Sui RPC server [yN]?y
Sui RPC server Url (Default to Sui DevNet if not specified) : http://127.0.0.1:5001
Generating keypair ...Do you want to generate a secp256k1 eypair instead? [y/N] No will select Ed25519 by default. y
Generated new keypair for address with flag 1 [0x647afe3090e94ad24b1dc13b71b524329bdf6ae8]
```
1. Confirm key scheme
```jsx
sui keytool list  ✔  5373  19:45:34
Sui Address | Public Key (Base64) | Flag
----------------------------------------------------------------------------------------------------
0x647afe3090e94ad24b1dc13b71b524329bdf6ae8 | Asg9Bjif/GrOHWLKdrw50veX8V7JvGrhGh0bzqRK3i/Y | 1
0x718bff02622a0e8df3f7801eeef873754b3221c4 | 1q3+fqUikPM/P2yNLPp4SHtVUXE6E7Q92yBlDhY8RxM= | 0
0xa452b3ba5d2e7ec4be9be0e138aa8243a73152eb | iew4e/0GRlmOdbffr33rf+ugHgx6tFpXDWJp9kaGdOw= | 0
0xb7e156df2c056b4bfa71ad94604df2a9aee86768 | ywEbW65QRiaDBPRA59Of4sNPRnUbbOHAe8BivDkxDPc= | 0
0xbf4fb48cac3ab05e9b29c209ef8178f2940db944 | ApyFnPZ4d4BNO4sCj8y26vj+NBoSxOeg+z4HCmsg8NNv | 1
```
1. Confirm active address used by client is pointing to your desired keypair in keystore
```jsx
sui client active-address
0x647afe3090e94ad24b1dc13b71b524329bdf6ae8
```
1. If not, manually edit client.yaml to point to the correct one
```jsx
cat /Users/joy/.sui/sui_config/client.yaml  ✔  5375  19:47:27
---
keystore:
File: /Users/joy/.sui/sui_config/sui.keystore
gateway:
rpc:
- "http://127.0.0.1:5001"
active_address: "0x647afe3090e94ad24b1dc13b71b524329bdf6ae8" // change me here
```
1. Request some gas
```jsx
curl -d "{\"recipient\": \"0x647afe3090e94ad24b1dc13b71b524329bdf6ae8\"}" -H "Content-Type: application/json" -X POST http://127.0.0.1:1234/faucet
{"ok":true}
```
1. Publish an NFT
```jsx
sui client create-example-nft  ✔  5372  19:45:12
Successfully created an ExampleNFT:
----- Move Object (0xf5337cd7725a44e458144db9ddf44d768dda589d[1]) -----
Owner: Account Address ( 0x647afe3090e94ad24b1dc13b71b524329bdf6ae8 )
Version: 1
Storage Rebate: 25
Previous Transaction: nPm09SZsbjoCtLJP80Zt2MmGrWJahg6qPDHBBdjeGwg=
----- Data -----
type: 0x2::devnet_nft::DevNetNFT
description: An NFT created by the Sui Command Line Tool
id: 0xf5337cd7725a44e458144db9ddf44d768dda589d
name: Example NFT
url: ipfs://bafkreibngqhl3gaa7daob4i2vccziay2jjlp435cf66vhono7nrvww53ty
```
1. Check explorer using customer RPC: [http://127.0.0.1:5001](http://127.0.0.1:5001/) to get an encoded signature string
![Untitled](Test%20Signing%20Key%20Scheme%20eb2085099c6c4992aea4064da9be5099/Untitled.png)
1. Verify signature encoding, the first byte is 0 or 1 based on ed25119 or secp256k1
```jsx
python3  ✔  5373  19:46:24
Python 3.9.13 (main, May 24 2022, 21:13:51)
[Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> base64.b64decode('AT71MD+CpIa+LdK8Z+x9Pvl7Ti0E4+sYSwBbxnxG86moOzqCvsg3qmgok1cq2aNaDMZ+7/jAhFjeg9nL38ZbeHoAAsg9Bjif/GrOHWLKdrw50veX8V7JvGrhGh0bzqRK3i/Y')
b'\x01>\xf50?\x82\xa4\x86\xbe-\xd2\xbcg\xec}>\xf9{N-\x04\xe3\xeb\x18K\x00[\xc6|F\xf3\xa9\xa8;:\x82\xbe\xc87\xaah(\x93W*\xd9\xa3Z\x0c\xc6~\xef\xf8\xc0\x84X\xde\x83\xd9\xcb\xdf\xc6[xz\x00\x02\xc8=\x068\x9f\xfcj\xce\x1db\xcav\xbc9\xd2\xf7\x97\xf1^\xc9\xbcj\xe1\x1a\x1d\x1b\xce\xa4J\xde/\xd8'
>>> base64.b64decode('AHJa91f2nR6/gzB5h37hU9kv12c0pPuBAhYZi7VYeJVZpj/5kpAHQdTHHsFztEaZO/TnGwqrbduSMtw1FOHoEQnWrf5+pSKQ8z8/bI0s+nhIe1VRcToTtD3bIGUOFjxHEw==')
b'\x00rZ\xf7W\xf6\x9d\x1e\xbf\x830y\x87~\xe1S\xd9/\xd7g4\xa4\xfb\x81\x02\x16\x19\x8b\xb5Xx\x95Y\xa6?\xf9\x92\x90\x07A\xd4\xc7\x1e\xc1s\xb4F\x99;\xf4\xe7\x1b\n\xabm\xdb\x922\xdc5\x14\xe1\xe8\x11\t\xd6\xad\xfe~\xa5"\x90\xf3??l\x8d,\xfaxH{UQq:\x13\xb4=\xdb e\x0e\x16<G\x13'
```
Try publish a move package:
```jsx
cd ~/mysten/sui/sui_programmability/examples/math
sui move build
sui client publish --gas-budget 10000
----- Certificate ----
Transaction Hash: VB/dpDuH3YynT+LvTECsZx45IHvu4r+2ayQTUaLr5m8=
Transaction Signature: AA==@ZQb9Cz9WiI4cxZ25FHYnTF/D0tlGeaYV1gJ100/k0VTJfKb3uXt2lilgEOJE8EaV+gRbAv8NpqAOKOYPT7DpAg==@1q3+fqUikPM/P2yNLPp4SHtVUXE6E7Q92yBlDhY8RxM=
Signed Authorities Bitmap: RoaringBitmap<[0, 2, 3]>
Transaction Kind : Publish
----- Transaction Effects ----
Status : Success
Created Objects:
- ID: 0x09ab0f5d1d3b94db8de90222ea54f92d85c203c6 , Owner: Immutable
Mutated Objects:
- ID: 0x2642d1cc957f8bc6f670831722d5519823f1eedd , Owner: Account Address ( 0x718bff02622a0e8df3f7801eeef873754b3221c4 )
----- Publish Results ----
The newly published package object ID: 0x336565be4ea63c6db73a9f8e7b8566633913dea9
Updated Gas : Coin { id: 0x2642d1cc957f8bc6f670831722d5519823f1eedd, value: 49508 }
```
Try calling a method:
```jsx
sui client call --package 0xcbfc414bda7f8ff0db4a7ac7d0f2594ed010de32 --module ecdsa --function keccak256 --gas-budget 10000 --args "Some data" 0x718bff02622a0e8df3f7801eeef873754b3221c4
----- Certificate ----
Transaction Hash: T5ko5UH7hF+gjE+TMD8tEOROyi590s0iyaB3qJ1GsZw=
Transaction Signature: AA==@CB2j/5up89zcRwbgVNu0XzCyV+iYk3+QjBvCKwlJYFX9l3e+LkvnJyGqpw9lybI4DQO33mVuh59y6A4l6MTnDQ==@1q3+fqUikPM/P2yNLPp4SHtVUXE6E7Q92yBlDhY8RxM=
Signed Authorities Bitmap: RoaringBitmap<[0, 1, 2]>
Transaction Kind : Call
Package ID : 0xcbfc414bda7f8ff0db4a7ac7d0f2594ed010de32
Module : ecdsa
Function : keccak256
Arguments : ["Some data", "0x718bff02622a0e8df3f7801eeef873754b3221c4"]
Type Arguments : []
----- Transaction Effects ----
Status : Success
Created Objects:
- ID: 0xaebf5b40568c14e6f89d3b548ae14c347d7a560c , Owner: Account Address ( 0x718bff02622a0e8df3f7801eeef873754b3221c4 )
Mutated Objects:
- ID: 0x2642d1cc957f8bc6f670831722d5519823f1eedd , Owner: Account Address ( 0x718bff02622a0e8df3f7801eeef873754b3221c4 )
```
```jsx
sui client object --id 0xaebf5b40568c14e6f89d3b548ae14c347d7a560c
----- Move Object (0xaebf5b40568c14e6f89d3b548ae14c347d7a560c[1]) -----
Owner: Account Address ( 0x718bff02622a0e8df3f7801eeef873754b3221c4 )
Version: 1
Storage Rebate: 16
Previous Transaction: T5ko5UH7hF+gjE+TMD8tEOROyi590s0iyaB3qJ1GsZw=
----- Data -----
type: 0xcbfc414bda7f8ff0db4a7ac7d0f2594ed010de32::ecdsa::HashedData
id: 0xaebf5b40568c14e6f89d3b548ae14c347d7a560c
value: [67, 162, 96, 81, 54, 43, 128, 64, 178, 137, 171, 233, 51, 52, 165, 227, 102, 39, 81, 170, 105, 17, 133, 174, 158, 154, 46, 30, 12, 22, 147, 80]sui client call --package 0xcbfc414bda7f8ff0db4a7ac7d0f2594ed010de32 --module ecdsa --function keccak256 --gas-budget 10000 --args "Some data" 0x718bff02622a0e8df3f7801eeef873754b3221c4  ✔  5762  11:07:52
```
Verify in python the hash is correct:
```jsx
>>> from Crypto.Hash import keccak
>>> k = keccak.new(digest_bits=256)
>>> k.update(b'Some data')
>>> bytearray.fromhex(k.hexdigest())
bytearray(b"C\xa2`Q6+\x80@\xb2\x89\xab\xe934\xa5\xe3f\'Q\xaai\x11\x85\xae\x9e\x9a.\x1e\x0c\x16\x93P")
>>> bytearray([67, 162, 96, 81, 54, 43, 128, 64, 178, 137, 171, 233, 51, 52, 165, 227, 102, 39, 81, 170, 105, 17, 133, 174, 158, 154, 46, 30, 12, 22, 147, 80])
bytearray(b"C\xa2`Q6+\x80@\xb2\x89\xab\xe934\xa5\xe3f\'Q\xaai\x11\x85\xae\x9e\x9a.\x1e\x0c\x16\x93P")
```
ecrecover:
```jsx
sui client call --package 0xf7361d06689b47c4d746e5c7f4d9ef6e0d0152c7 --module ecdsa --function ecrecover --gas-budget 10000 --args 0x48da8e52cd40c55c25dc98fffba5f4d22c8c05b22df371d72e2b96cc3df145152333a9f0b1641044016cf71bbdc8a5556413ea32c91e8bc9b6514f1fbf715b2601 0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655 0x718bff02622a0e8df3f7801eeef873754b3221c4
----- Certificate ----
Transaction Hash: 1v+IgXk0OLVTLRx6x4jpIAhUTd+w0PS4oGYipxczoHk=
Transaction Signature: AA==@3tvCcrcsK4g0zzpdS9H6iuFZx0Kgzd065H31e0FSyk4c10dm2o+Im1rgYDKPFKSSOvKfCdIB580ZVaSkqxpLCA==@1q3+fqUikPM/P2yNLPp4SHtVUXE6E7Q92yBlDhY8RxM=
Signed Authorities Bitmap: RoaringBitmap<[0, 2, 3]>
Transaction Kind : Call
Package ID : 0xf7361d06689b47c4d746e5c7f4d9ef6e0d0152c7
Module : ecdsa
Function : ecrecover
Arguments : [[65,72,218,142,82,205,64,197,92,37,220,152,255,251,165,244,210,44,140,5,178,45,243,113,215,46,43,150,204,61,241,69,21,35,51,169,240,177,100,16,68,1,108,247,27,189,200,165,85,100,19,234,50,201,30,139,201,182,81,79,31,191,113,91,38,1], [32,29,164,75,88,110,176,114,159,247,10,115,195,38,146,111,110,213,162,95,91,5,110,127,71,251,198,229,141,134,135,22,85], "0x718bff02622a0e8df3f7801eeef873754b3221c4"]
Type Arguments : []
----- Transaction Effects ----
Status : Success
Created Objects:
- ID: 0xbc3843dbeb333c47d97693b4e51751fe14ba7e67 , Owner: Account Address ( 0x718bff02622a0e8df3f7801eeef873754b3221c4 )
Mutated Objects:
- ID: 0x2642d1cc957f8bc6f670831722d5519823f1eedd , Owner: Account Address ( 0x718bff02622a0e8df3f7801eeef873754b3221c4 )
sui client object --id 0xbc3843dbeb333c47d97693b4e51751fe14ba7e67
----- Move Object (0xbc3843dbeb333c47d97693b4e51751fe14ba7e67[1]) -----
Owner: Account Address ( 0x718bff02622a0e8df3f7801eeef873754b3221c4 )
Version: 1
Storage Rebate: 15
Previous Transaction: 1v+IgXk0OLVTLRx6x4jpIAhUTd+w0PS4oGYipxczoHk=
----- Data -----
type: 0xf7361d06689b47c4d746e5c7f4d9ef6e0d0152c7::ecdsa::Output
id: 0xbc3843dbeb333c47d97693b4e51751fe14ba7e67
value: [2, 134, 109, 180, 101, 14, 187, 153, 28, 27, 90, 30, 212, 207, 217, 22, 96, 141, 102, 67, 138, 176, 180, 8, 104, 115, 50, 105, 240, 103, 48, 140, 170]
// verify in python
>>> x = bytearray([2, 134, 109, 180, 101, 14, 187, 153, 28, 27, 90, 30, 212, 207, 217, 22, 96, 141, 102, 67, 138, 176, 180, 8, 104, 115, 50, 105, 240, 103, 48, 140, 170])
>>> x.hex()
'02866db4650ebb991c1b5a1ed4cfd916608d66438ab0b40868733269f067308caa'
// use an npm tool
ethereum_public_key_to_address 02866db4650ebb991c1b5a1ed4cfd916608d66438ab0b40868733269f067308caa
0x9624954Bf4703df1D4503006fc23CdaC318f8316
// same result using web3js, note that the last byte is modified from 1c to 01
node app
```
const Web3 = require('web3')
const web3 = new Web3('{alchemy url goes here}')
privateKey1 = '{example key goes here}'.toString('hex')
const x = web3.eth.accounts.sign('Some data', privateKey1);
console.log('signed data: ', x);
const y = web3.eth.accounts.recover('Some data', x.signature);
console.log('recovered address: ', y);
signed data: {
message: 'Some data',
messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
v: '0x1c',
r: '0x48da8e52cd40c55c25dc98fffba5f4d22c8c05b22df371d72e2b96cc3df14515',
s: '0x2333a9f0b1641044016cf71bbdc8a5556413ea32c91e8bc9b6514f1fbf715b26',
signature: '0x48da8e52cd40c55c25dc98fffba5f4d22c8c05b22df371d72e2b96cc3df145152333a9f0b1641044016cf71bbdc8a5556413ea32c91e8bc9b6514f1fbf715b261c'
}
recovered address: 0x9624954Bf4703df1D4503006fc23CdaC318f8316
// run verify
sui client call --package 0x336565be4ea63c6db73a9f8e7b8566633913dea9 --module ecdsa --function secp256k1_verify --gas-budget 10000 --args 0x48da8e52cd40c55c25dc98fffba5f4d22c8c05b22df371d72e2b96cc3df145152333a9f0b1641044016cf71bbdc8a5556413ea32c91e8bc9b6514f1fbf715b2601 0x04866db4650ebb991c1b5a1ed4cfd916608d66438ab0b40868733269f067308caa8b4a66ae07f7691f07f787adb1989cdab96a24bb1a3d2dc02cf925c64e0b9c5a 0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment