Skip to content

Commit

Permalink
Merge branch 'main' into g/move-airdrop-folder
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppecrj authored Dec 23, 2024
2 parents 9a257ce + 01a8743 commit b01659a
Show file tree
Hide file tree
Showing 29 changed files with 1,316 additions and 586 deletions.
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,18 @@
"args": ["run", "${fileBasename}", "--config", "vitest.config.ts"],
"console": "integratedTerminal",
},
{
"name": "Vitest: current file in 'packages/sdk/' (unit)",
"type": "node",
"request": "launch",
"env": { "NODE_ENV": "development", "DEBUG": "csb:*,test:*", "DEBUG_DEPTH":"10" },
"program": "${workspaceFolder}/node_modules/vitest/vitest.mjs",
"cwd": "${workspaceFolder}/packages/sdk",
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"smartStep": true,
"args": ["run", "${fileBasename}", "--config", "vitest.config.unit.ts"],
"console": "integratedTerminal",
}
]
}
493 changes: 493 additions & 0 deletions core/contracts/base/tipping.go

Large diffs are not rendered by default.

24 changes: 6 additions & 18 deletions core/node/auth/auth_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func NewChainAuthArgsForIsSpaceMember(spaceId shared.StreamId, userId string) *C

func NewChainAuthArgsForIsWalletLinked(
userId []byte,
receipt *BlockchainTransactionReceipt,
walletAddress []byte,
) *ChainAuthArgs {
return &ChainAuthArgs{
kind: chainAuthKindIsWalletLinked,
principal: common.BytesToAddress(userId),
receipt: receipt,
walletAddress: common.BytesToAddress(walletAddress),
}
}

Expand All @@ -113,7 +113,7 @@ type ChainAuthArgs struct {
principal common.Address
permission Permission
linkedWallets string // a serialized list of linked wallets to comply with the cache key constraints
receipt *BlockchainTransactionReceipt
walletAddress common.Address
}

func (args *ChainAuthArgs) Principal() common.Address {
Expand All @@ -122,14 +122,14 @@ func (args *ChainAuthArgs) Principal() common.Address {

func (args *ChainAuthArgs) String() string {
return fmt.Sprintf(
"ChainAuthArgs{kind: %d, spaceId: %s, channelId: %s, principal: %s, permission: %s, linkedWallets: %s, receipt: %s}",
"ChainAuthArgs{kind: %d, spaceId: %s, channelId: %s, principal: %s, permission: %s, linkedWallets: %s, walletAddress: %s}",
args.kind,
args.spaceId,
args.channelId,
args.principal.Hex(),
args.permission,
args.linkedWallets,
args.receipt,
args.walletAddress.Hex(),
)
}

Expand Down Expand Up @@ -1009,21 +1009,9 @@ func (ca *chainAuth) checkEntitlement(
// handle checking if the user is linked to a specific wallet
if args.kind == chainAuthKindIsWalletLinked {
for _, wallet := range wallets {
// Check the transaction sender (for regular transactions)
if bytes.Equal(args.receipt.From, wallet.Bytes()) {
if wallet == args.walletAddress {
return boolCacheResult(true), nil
}
// Check each log in the receipt
for _, logEntry := range args.receipt.Logs {
// The sender is typically in the first topic (after the event signature)
if len(logEntry.Topics) > 1 { // First topic is event signature, second is usually sender
// Convert the topic (which is 32 bytes) to an address (20 bytes) by taking the last 20 bytes
senderFromTopic := common.BytesToAddress(logEntry.Topics[1])
if bytes.Equal(senderFromTopic.Bytes(), wallet.Bytes()) {
return boolCacheResult(true), nil
}
}
}
}
return boolCacheResult(false), nil
}
Expand Down
972 changes: 495 additions & 477 deletions core/node/protocol/protocol.pb.go

Large diffs are not rendered by default.

113 changes: 93 additions & 20 deletions core/node/rules/can_add_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"log/slog"
"math/big"
"slices"
"time"

Expand All @@ -13,6 +14,10 @@ import (

"github.com/ethereum/go-ethereum/common"

ethTypes "github.com/ethereum/go-ethereum/core/types"

baseContracts "github.com/river-build/river/core/contracts/base"

"github.com/river-build/river/core/node/auth"
. "github.com/river-build/river/core/node/base"
"github.com/river-build/river/core/node/dlog"
Expand Down Expand Up @@ -338,8 +343,8 @@ func (params *aeParams) canAddUserPayload(payload *StreamEvent_UserPayload) rule
return aeBuilder().
check(ru.params.creatorIsMember).
check(ru.validBlockchainTransaction_IsUnique).
check(ru.validBlockchainTransaction_ReceiptMetadata).
verifyReceipt(ru.blockchainTransaction_Receipt).
check(ru.validBlockchainTransaction_CheckReceiptMetadata).
verifyReceipt(ru.blockchainTransaction_GetReceipt).
requireChainAuth(ru.blockchainTransaction_ChainAuth).
requireParentEvent(ru.parentEventForBlockchainTransaction)
case *UserPayload_ReceivedBlockchainTransaction_:
Expand Down Expand Up @@ -616,13 +621,13 @@ func (ru *aeMemberBlockchainTransactionRules) validMemberBlockchainTransaction_R
if err != nil {
return false, err
}
err = checkIsMember(ru.params, content.Tip.GetToUserAddress())
err = checkIsMember(ru.params, content.Tip.GetReceiver())
if err != nil {
return false, err
}
// we need a ref event id
if content.Tip.GetRefEventId() == nil {
return false, RiverError(Err_INVALID_ARGUMENT, "tip transaction ref event id is nil")
if content.Tip.GetMessageId() == nil {
return false, RiverError(Err_INVALID_ARGUMENT, "tip transaction message id is nil")
}
return true, nil
default:
Expand Down Expand Up @@ -688,16 +693,67 @@ func (ru *aeBlockchainTransactionRules) validBlockchainTransaction_IsUnique() (b
return true, nil
}

func (ru *aeBlockchainTransactionRules) validBlockchainTransaction_ReceiptMetadata() (bool, error) {
func (ru *aeBlockchainTransactionRules) validBlockchainTransaction_CheckReceiptMetadata() (bool, error) {
receipt := ru.transaction.Receipt
if receipt == nil {
return false, RiverError(Err_INVALID_ARGUMENT, "receipt is nil")
}
// check creator
switch content := ru.transaction.Content.(type) {
case nil:
// for unspecified types, we don't need to check anything specific
// the other checks should make sure the transaction is valid and from this user
return true, nil
case *BlockchainTransaction_Tip_:
// todo
return true, nil
// parse the logs for the tip event, make sure it matches the tip metadata
filterer, err := baseContracts.NewTippingFilterer(common.Address{}, nil)
if err != nil {
return false, err
}
for _, receiptLog := range receipt.Logs {
// unpack the log
// compare to metadata in the tip
topics := make([]common.Hash, len(receiptLog.Topics))
for i, topic := range receiptLog.Topics {
topics[i] = common.BytesToHash(topic)
}
log := ethTypes.Log{
Address: common.BytesToAddress(receiptLog.Address),
Topics: topics,
Data: receiptLog.Data,
}
tipEvent, err := filterer.ParseTip(log)
if err != nil {
continue // not a tip
}
if tipEvent.TokenId.Cmp(big.NewInt(int64(content.Tip.GetTokenId()))) != 0 {
continue
}
if !bytes.Equal(tipEvent.Currency[:], content.Tip.GetCurrency()) {
continue
}
if !bytes.Equal(tipEvent.Sender[:], content.Tip.GetSender()) {
continue
}
if !bytes.Equal(tipEvent.Receiver[:], content.Tip.GetReceiver()) {
continue
}
if tipEvent.Amount.Cmp(big.NewInt(int64(content.Tip.GetAmount()))) != 0 {
continue
}
if !bytes.Equal(tipEvent.MessageId[:], content.Tip.GetMessageId()) {
continue
}
if !bytes.Equal(tipEvent.ChannelId[:], content.Tip.GetChannelId()) {
continue
}
// match found
return true, nil
}
return false, RiverError(
Err_INVALID_ARGUMENT,
"matching tip event not found in receipt logs",
)
default:
return false, RiverError(
Err_INVALID_ARGUMENT,
Expand All @@ -724,11 +780,11 @@ func (ru *aeReceivedBlockchainTransactionRules) parentEventForReceivedBlockchain
if !ok {
return nil, RiverError(Err_INVALID_ARGUMENT, "content is not a tip")
}
if content.Tip.GetStreamId() == nil {
return nil, RiverError(Err_INVALID_ARGUMENT, "transaction stream id is nil")
if content.Tip.GetChannelId() == nil {
return nil, RiverError(Err_INVALID_ARGUMENT, "transaction channel id is nil")
}
// convert to stream id
streamId, err := shared.StreamIdFromBytes(content.Tip.GetStreamId())
streamId, err := shared.StreamIdFromBytes(content.Tip.GetChannelId())
if err != nil {
return nil, err
}
Expand All @@ -752,11 +808,11 @@ func (ru *aeBlockchainTransactionRules) parentEventForBlockchainTransaction() (*
return nil, nil
case *BlockchainTransaction_Tip_:
// forward a "tip received" event to the user stream of the toUserAddress
userStreamId, err := shared.UserStreamIdFromBytes(content.Tip.GetToUserAddress())
userStreamId, err := shared.UserStreamIdFromBytes(content.Tip.GetReceiver())
if err != nil {
return nil, err
}
toStreamId, err := shared.StreamIdFromBytes(content.Tip.GetStreamId())
toStreamId, err := shared.StreamIdFromBytes(content.Tip.GetChannelId())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -789,20 +845,37 @@ func (ru *aeBlockchainTransactionRules) parentEventForBlockchainTransaction() (*
}
}

func (ru *aeBlockchainTransactionRules) blockchainTransaction_Receipt() (*BlockchainTransactionReceipt, error) {
return ru.transaction.Receipt, nil
func (ru *aeBlockchainTransactionRules) blockchainTransaction_GetReceipt() (*BlockchainTransactionReceipt, error) {
return ru.transaction.GetReceipt(), nil
}

// check to see that the transaction is from a wallet linked to the creator
func (ru *aeBlockchainTransactionRules) blockchainTransaction_ChainAuth() (*auth.ChainAuthArgs, error) {
if bytes.Equal(ru.transaction.Receipt.From, ru.params.parsedEvent.Event.CreatorAddress) {
return nil, nil
}
args := auth.NewChainAuthArgsForIsWalletLinked(
ru.params.parsedEvent.Event.CreatorAddress,
ru.transaction.Receipt,
)
return args, nil
switch content := ru.transaction.Content.(type) {
case nil:
// no content, verify the receipt.from
return auth.NewChainAuthArgsForIsWalletLinked(
ru.params.parsedEvent.Event.CreatorAddress,
ru.transaction.Receipt.From,
), nil
case *BlockchainTransaction_Tip_:
// tips can be sent through a bundler, verify the tip sender
// as specified in the tip content and verified against the logs in blockchainTransaction_CheckReceiptMetadata
return auth.NewChainAuthArgsForIsWalletLinked(
ru.params.parsedEvent.Event.CreatorAddress,
content.Tip.GetSender(),
), nil
default:
return nil, RiverError(
Err_INVALID_ARGUMENT,
"unknown transaction type",
"transactionType",
content,
)
}
}

func (ru *aeMembershipRules) validMembershipPayload() (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.0.137",
"version": "0.0.140",
"packages": ["packages/*", "protocol"],
"npmClient": "yarn"
}
2 changes: 1 addition & 1 deletion packages/create-river-build-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-river-build-app",
"version": "0.0.137",
"version": "0.0.140",
"bin": "index.js",
"engines": {
"node": "^18.0.0 || >=20.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/dlog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@river-build/dlog",
"version": "0.0.137",
"version": "0.0.140",
"packageManager": "[email protected]",
"type": "module",
"main": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/encryption/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@river-build/encryption",
"version": "0.0.137",
"version": "0.0.140",
"packageManager": "[email protected]",
"type": "module",
"main": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@river-build/eslint-config",
"version": "0.0.137",
"version": "0.0.140",
"license": "MIT",
"main": "typescript.js"
}
2 changes: 1 addition & 1 deletion packages/generated/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@river-build/generated",
"version": "0.0.137",
"version": "0.0.140",
"packageManager": "[email protected]",
"scripts": {
"build": "yarn make-config",
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"vite": "^5.3.1",
"vite-plugin-checker": "^0.8.0",
"vite-plugin-replace": "^0.1.1",
"vite-plugin-wasm": "^3.3.0",
"vite-plugin-wasm": "^3.4.1",
"vite-tsconfig-paths": "^5.1.3"
}
}
2 changes: 1 addition & 1 deletion packages/prettier-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@river-build/prettier-config",
"version": "0.0.137",
"version": "0.0.140",
"license": "MIT",
"main": "config.js",
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/proto/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@river-build/proto",
"version": "0.0.137",
"version": "0.0.140",
"packageManager": "[email protected]",
"type": "module",
"main": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@river-build/react-sdk",
"description": "React Hooks for River SDK",
"version": "0.0.137",
"version": "0.0.140",
"type": "module",
"main": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
Expand Down
8 changes: 5 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@river-build/sdk",
"version": "0.0.137",
"version": "0.0.140",
"packageManager": "[email protected]",
"type": "module",
"main": "dist/index.js",
Expand Down Expand Up @@ -29,7 +29,8 @@
"@ethereumjs/util": "^8.0.1",
"@river-build/dlog": "workspace:^",
"@river-build/encryption": "workspace:^",
"@river-build/mls-rs-wasm": "^0.0.7",
"@river-build/generated": "workspace:^",
"@river-build/mls-rs-wasm": "^0.0.9",
"@river-build/proto": "workspace:^",
"@river-build/web3": "workspace:^",
"browser-or-node": "^3.0.0",
Expand Down Expand Up @@ -60,7 +61,8 @@
"msgpackr": "^1.10.1",
"seedrandom": "^3.0.5",
"typed-emitter": "^2.1.0",
"typescript": "^5.1.6"
"typescript": "^5.1.6",
"vite-plugin-wasm": "^3.4.1"
},
"files": [
"/dist"
Expand Down
Loading

0 comments on commit b01659a

Please sign in to comment.