|
| 1 | +package sample |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/core" |
| 9 | + "github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/near" |
| 10 | + "github.com/Cramiumlabs/wallet-core/wrapper/go-wrapper/protos/transactioncompiler" |
| 11 | + "google.golang.org/protobuf/proto" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNEAR() { |
| 15 | + |
| 16 | + signatureBytes, _ := hex.DecodeString("969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01") |
| 17 | + blockHashBytes, _ := hex.DecodeString("0fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6") |
| 18 | + pubKeyBytes, _ := hex.DecodeString("917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d") |
| 19 | + var transferAmount [16]byte |
| 20 | + transferAmount[0] = 1 |
| 21 | + |
| 22 | + fmt.Println("hex amount:", hex.EncodeToString(transferAmount[:])) |
| 23 | + |
| 24 | + input := near.SigningInput{ |
| 25 | + SignerId: "test.near", |
| 26 | + ReceiverId: "whatever.near", |
| 27 | + BlockHash: blockHashBytes, |
| 28 | + Nonce: 1, |
| 29 | + PublicKey: pubKeyBytes, |
| 30 | + Actions: []*near.Action{ |
| 31 | + &near.Action{ |
| 32 | + Payload: &near.Action_Transfer{ |
| 33 | + Transfer: &near.Transfer{ |
| 34 | + Deposit: transferAmount[:], |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | + txInputData, _ := proto.Marshal(&input) |
| 41 | + msgForSign := core.PreImageHashes(core.CoinTypeNEAR, txInputData) |
| 42 | + |
| 43 | + var preSigningOutput transactioncompiler.PreSigningOutput |
| 44 | + proto.Unmarshal(msgForSign, &preSigningOutput) |
| 45 | + |
| 46 | + fmt.Println("sigHash:", hex.EncodeToString(preSigningOutput.DataHash)) |
| 47 | + |
| 48 | + valid := core.PublicKeyVerify(pubKeyBytes, core.PublicKeyTypeED25519, signatureBytes, preSigningOutput.DataHash) |
| 49 | + if !valid { |
| 50 | + panic(errors.New("verification failed")) |
| 51 | + } |
| 52 | + fmt.Println("Signature verification successfully") |
| 53 | + |
| 54 | + txOutput := core.CompileWithSignatures(core.CoinTypeNEAR, txInputData, [][]byte{signatureBytes}, [][]byte{pubKeyBytes}) |
| 55 | + |
| 56 | + var output near.SigningOutput |
| 57 | + err := proto.Unmarshal(txOutput, &output) |
| 58 | + if err != nil { |
| 59 | + panic(errors.New("unmarshal output failed")) |
| 60 | + } |
| 61 | + fmt.Println("Message for broadcast:", hex.EncodeToString(output.SignedTransaction)) |
| 62 | + fmt.Println("Transaction hash:", hex.EncodeToString(output.GetHash())) |
| 63 | +} |
0 commit comments