mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-11 13:29:12 +00:00
- **refactor: remove unused auth components** - **refactor: improve devbox configuration and deployment process** - **refactor: improve devnet and testnet setup** - **fix: update templ version to v0.2.778** - **refactor: rename pkl/net.matrix to pkl/matrix.net** - **refactor: migrate webapp components to nebula** - **refactor: protobuf types** - **chore: update dependencies for improved security and stability** - **feat: implement landing page and vault gateway servers** - **refactor: Migrate data models to new module structure and update related files** - **feature/1121-implement-ucan-validation** - **refactor: Replace hardcoded constants with model types in attns.go** - **feature/1121-implement-ucan-validation** - **chore: add origin Host struct and update main function to handle multiple hosts** - **build: remove unused static files from dwn module** - **build: remove unused static files from dwn module** - **refactor: Move DWN models to common package** - **refactor: move models to pkg/common** - **refactor: move vault web app assets to embed module** - **refactor: update session middleware import path** - **chore: configure port labels and auto-forwarding behavior** - **feat: enhance devcontainer configuration** - **feat: Add UCAN middleware for Echo with flexible token validation** - **feat: add JWT middleware for UCAN authentication** - **refactor: update package URI and versioning in PklProject files** - **fix: correct sonr.pkl import path** - **refactor: move JWT related code to auth package** - **feat: introduce vault configuration retrieval and management** - **refactor: Move vault components to gateway module and update file paths** - **refactor: remove Dexie and SQLite database implementations** - **feat: enhance frontend with PWA features and WASM integration** - **feat: add Devbox features and streamline Dockerfile** - **chore: update dependencies to include TigerBeetle** - **chore(deps): update go version to 1.23** - **feat: enhance devnet setup with PATH environment variable and updated PWA manifest** - **fix: upgrade tigerbeetle-go dependency and remove indirect dependency** - **feat: add PostgreSQL support to devnet and testnet deployments** - **refactor: rename keyshare cookie to token cookie** - **feat: upgrade Go version to 1.23.3 and update dependencies** - **refactor: update devnet and testnet configurations** - **feat: add IPFS configuration for devnet** - **I'll help you update the ipfs.config.pkl to include all the peers from the shell script. Here's the updated configuration:** - **refactor: move mpc package to crypto directory** - **feat: add BIP32 support for various cryptocurrencies** - **feat: enhance ATN.pkl with additional capabilities** - **refactor: simplify smart account and vault attenuation creation** - **feat: add new capabilities to the Attenuation type** - **refactor: Rename MPC files for clarity and consistency** - **feat: add DIDKey support for cryptographic operations** - **feat: add devnet and testnet deployment configurations** - **fix: correct key derivation in bip32 package** - **refactor: rename crypto/bip32 package to crypto/accaddr** - **fix: remove duplicate indirect dependency** - **refactor: move vault package to root directory** - **refactor: update routes for gateway and vault** - **refactor: remove obsolete web configuration file** - **refactor: remove unused TigerBeetle imports and update host configuration** - **refactor: adjust styles directory path** - **feat: add broadcastTx and simulateTx functions to gateway** - **feat: add PinVault handler**
225 lines
5.7 KiB
Go
Executable File
225 lines
5.7 KiB
Go
Executable File
//
|
|
// Copyright Coinbase, Inc. All Rights Reserved.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package mina
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/cosmos/btcutil/base58"
|
|
|
|
"github.com/onsonr/sonr/crypto/core/curves"
|
|
)
|
|
|
|
// Transaction is a Mina transaction for payments or delegations
|
|
type Transaction struct {
|
|
Fee, FeeToken uint64
|
|
FeePayerPk *PublicKey
|
|
Nonce, ValidUntil uint32
|
|
Memo string
|
|
Tag [3]bool
|
|
SourcePk, ReceiverPk *PublicKey
|
|
TokenId, Amount uint64
|
|
Locked bool
|
|
NetworkId NetworkType
|
|
}
|
|
|
|
type txnJson struct {
|
|
Common txnCommonJson
|
|
Body [2]interface{}
|
|
}
|
|
|
|
type txnCommonJson struct {
|
|
Fee uint64 `json:"fee"`
|
|
FeeToken uint64 `json:"fee_token"`
|
|
FeePayerPk string `json:"fee_payer_pk"`
|
|
Nonce uint32 `json:"nonce"`
|
|
ValidUntil uint32 `json:"valid_until"`
|
|
Memo string `json:"memo"`
|
|
NetworkId uint8 `json:"network_id"`
|
|
}
|
|
|
|
type txnBodyPaymentJson struct {
|
|
SourcePk string `json:"source_pk"`
|
|
ReceiverPk string `json:"receiver_pk"`
|
|
TokenId uint64 `json:"token_id"`
|
|
Amount uint64 `json:"amount"`
|
|
}
|
|
|
|
type txnBodyDelegationJson struct {
|
|
Delegator string `json:"delegator"`
|
|
NewDelegate string `json:"new_delegate"`
|
|
}
|
|
|
|
func (txn *Transaction) MarshalBinary() ([]byte, error) {
|
|
mapper := map[bool]byte{
|
|
true: 1,
|
|
false: 0,
|
|
}
|
|
out := make([]byte, 175)
|
|
binary.LittleEndian.PutUint64(out, txn.Fee)
|
|
binary.LittleEndian.PutUint64(out[8:16], txn.FeeToken)
|
|
copy(out[16:48], txn.FeePayerPk.value.ToAffineCompressed())
|
|
binary.LittleEndian.PutUint32(out[48:52], txn.Nonce)
|
|
binary.LittleEndian.PutUint32(out[52:56], txn.ValidUntil)
|
|
|
|
out[56] = 0x01
|
|
out[57] = byte(len(txn.Memo))
|
|
copy(out[58:90], txn.Memo[:])
|
|
out[90] = mapper[txn.Tag[0]]
|
|
out[91] = mapper[txn.Tag[1]]
|
|
out[92] = mapper[txn.Tag[2]]
|
|
copy(out[93:125], txn.SourcePk.value.ToAffineCompressed())
|
|
copy(out[125:157], txn.ReceiverPk.value.ToAffineCompressed())
|
|
binary.LittleEndian.PutUint64(out[157:165], txn.TokenId)
|
|
binary.LittleEndian.PutUint64(out[165:173], txn.Amount)
|
|
out[173] = mapper[txn.Locked]
|
|
out[174] = byte(txn.NetworkId)
|
|
return out, nil
|
|
}
|
|
|
|
func (txn *Transaction) UnmarshalBinary(input []byte) error {
|
|
mapper := map[byte]bool{
|
|
1: true,
|
|
0: false,
|
|
}
|
|
if len(input) < 175 {
|
|
return fmt.Errorf("invalid byte sequence")
|
|
}
|
|
feePayerPk := new(PublicKey)
|
|
sourcePk := new(PublicKey)
|
|
receiverPk := new(PublicKey)
|
|
err := feePayerPk.UnmarshalBinary(input[16:48])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = sourcePk.UnmarshalBinary(input[93:125])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = receiverPk.UnmarshalBinary(input[125:157])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
txn.Fee = binary.LittleEndian.Uint64(input[:8])
|
|
txn.FeeToken = binary.LittleEndian.Uint64(input[8:16])
|
|
txn.FeePayerPk = feePayerPk
|
|
txn.Nonce = binary.LittleEndian.Uint32(input[48:52])
|
|
txn.ValidUntil = binary.LittleEndian.Uint32(input[52:56])
|
|
txn.Memo = string(input[58 : 58+input[57]])
|
|
txn.Tag[0] = mapper[input[90]]
|
|
txn.Tag[1] = mapper[input[91]]
|
|
txn.Tag[2] = mapper[input[92]]
|
|
txn.SourcePk = sourcePk
|
|
txn.ReceiverPk = receiverPk
|
|
txn.TokenId = binary.LittleEndian.Uint64(input[157:165])
|
|
txn.Amount = binary.LittleEndian.Uint64(input[165:173])
|
|
txn.Locked = mapper[input[173]]
|
|
txn.NetworkId = NetworkType(input[174])
|
|
return nil
|
|
}
|
|
|
|
func (txn *Transaction) UnmarshalJSON(input []byte) error {
|
|
var t txnJson
|
|
err := json.Unmarshal(input, &t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
strType, ok := t.Body[0].(string)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected type")
|
|
}
|
|
memo, _, err := base58.CheckDecode(t.Common.Memo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch strType {
|
|
case "Payment":
|
|
b, ok := t.Body[1].(txnBodyPaymentJson)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected type")
|
|
}
|
|
feePayerPk := new(PublicKey)
|
|
err = feePayerPk.ParseAddress(b.SourcePk)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
receiverPk := new(PublicKey)
|
|
err = receiverPk.ParseAddress(b.ReceiverPk)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
txn.FeePayerPk = feePayerPk
|
|
txn.ReceiverPk = receiverPk
|
|
case "Stake_delegation":
|
|
bType, ok := t.Body[1].([2]interface{})
|
|
if !ok {
|
|
return fmt.Errorf("unexpected type")
|
|
}
|
|
delegateType, ok := bType[0].(string)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected type")
|
|
}
|
|
if delegateType == "Set_delegate" {
|
|
b, ok := bType[1].(txnBodyDelegationJson)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected type")
|
|
}
|
|
feePayerPk := new(PublicKey)
|
|
err = feePayerPk.ParseAddress(b.Delegator)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
receiverPk := new(PublicKey)
|
|
err = receiverPk.ParseAddress(b.NewDelegate)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
txn.FeePayerPk = feePayerPk
|
|
txn.ReceiverPk = receiverPk
|
|
} else {
|
|
return fmt.Errorf("unexpected type")
|
|
}
|
|
default:
|
|
return fmt.Errorf("unexpected type")
|
|
}
|
|
txn.Memo = string(memo[2 : 2+memo[1]])
|
|
sourcePk := new(PublicKey)
|
|
sourcePk.value = new(curves.Ep).Set(txn.FeePayerPk.value)
|
|
txn.Fee = t.Common.Fee
|
|
txn.FeeToken = t.Common.FeeToken
|
|
txn.Nonce = t.Common.Nonce
|
|
txn.ValidUntil = t.Common.ValidUntil
|
|
txn.NetworkId = NetworkType(t.Common.NetworkId)
|
|
return nil
|
|
}
|
|
|
|
func (txn Transaction) addRoInput(input *roinput) {
|
|
input.AddFp(txn.FeePayerPk.value.X())
|
|
input.AddFp(txn.SourcePk.value.X())
|
|
input.AddFp(txn.ReceiverPk.value.X())
|
|
|
|
input.AddUint64(txn.Fee)
|
|
input.AddUint64(txn.FeeToken)
|
|
input.AddBit(txn.FeePayerPk.value.Y().IsOdd())
|
|
input.AddUint32(txn.Nonce)
|
|
input.AddUint32(txn.ValidUntil)
|
|
memo := [34]byte{0x01, byte(len(txn.Memo))}
|
|
copy(memo[2:], txn.Memo)
|
|
input.AddBytes(memo[:])
|
|
for _, b := range txn.Tag {
|
|
input.AddBit(b)
|
|
}
|
|
|
|
input.AddBit(txn.SourcePk.value.Y().IsOdd())
|
|
input.AddBit(txn.ReceiverPk.value.Y().IsOdd())
|
|
input.AddUint64(txn.TokenId)
|
|
input.AddUint64(txn.Amount)
|
|
input.AddBit(txn.Locked)
|
|
}
|