Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions core/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
"bytes"
"encoding/binary"
"github.com/ElrondNetwork/elrond-go-core/hashing/keccak"
)

// SystemAccountAddress is the hard-coded address in which we save global settings on all shards
Expand Down Expand Up @@ -85,3 +87,46 @@ func IsSmartContractOnMetachain(identifier []byte, rcvAddress []byte) bool {
make([]byte, numInitCharactersForOnMetachainSC))
return isOnMetaChainSCAddress
}

// NewAddress creates a new smart contract address from the creators address and nonce
Copy link

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a grammatical error in the comment. It should be 'from the creator's address' (with an apostrophe) instead of 'from the creators address'

Suggested change
// NewAddress creates a new smart contract address from the creators address and nonce
// NewAddress creates a new smart contract address from the creator's address and nonce

Copilot uses AI. Check for mistakes.
// The address is created by applied keccak256 on the appended value off creator address and nonce
Copy link

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment contains grammatical errors. It should read: 'The address is created by applying keccak256 on the appended value of creator address and nonce'

Suggested change
// The address is created by applied keccak256 on the appended value off creator address and nonce
// The address is created by applying keccak256 on the appended value of creator address and nonce

Copilot uses AI. Check for mistakes.
// Prefix mask is applied for first 8 bytes 0, and for bytes 9-10 - VM type
Copy link

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment has grammatical errors. It should read: 'Prefix mask is applied for the first 8 bytes as 0, and for bytes 9-10 as VM type'

Suggested change
// Prefix mask is applied for first 8 bytes 0, and for bytes 9-10 - VM type
// Prefix mask is applied for the first 8 bytes as 0, and for bytes 9-10 as VM type

Copilot uses AI. Check for mistakes.
// Suffix mask is applied - last 2 bytes are for the shard ID - mask is applied as suffix mask
func NewAddress(creatorAddress []byte, addressLength int, creatorNonce uint64, vmType []byte) ([]byte, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing unit tests.

if len(creatorAddress) != addressLength {
return nil, ErrAddressLengthNotCorrect
}

if len(vmType) != VMTypeLen {
return nil, ErrVMTypeLengthIsNotCorrect
}

base := hashFromAddressAndNonce(creatorAddress, creatorNonce)
prefixMask := createPrefixMask(vmType)
suffixMask := createSuffixMask(creatorAddress)

copy(base[:NumInitCharactersForScAddress], prefixMask)
copy(base[len(base)-ShardIdentiferLen:], suffixMask)

return base, nil
}

func hashFromAddressAndNonce(creatorAddress []byte, creatorNonce uint64) []byte {
buffNonce := make([]byte, 8)
binary.LittleEndian.PutUint64(buffNonce, creatorNonce)
adrAndNonce := append(creatorAddress, buffNonce...)
scAddress := keccak.NewKeccak().Compute(string(adrAndNonce))
Copy link

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting byte slice to string and back to bytes is inefficient. The keccak.Compute method likely accepts []byte directly, or you should use a method that accepts byte slices to avoid unnecessary conversion.

Suggested change
scAddress := keccak.NewKeccak().Compute(string(adrAndNonce))
scAddress := keccak.NewKeccak().Compute(adrAndNonce)

Copilot uses AI. Check for mistakes.

return scAddress
}

func createPrefixMask(vmType []byte) []byte {
prefixMask := make([]byte, NumInitCharactersForScAddress-VMTypeLen)
prefixMask = append(prefixMask, vmType...)

return prefixMask
}

func createSuffixMask(creatorAddress []byte) []byte {
return creatorAddress[len(creatorAddress)-2:]
}
6 changes: 6 additions & 0 deletions core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ var ErrNilLogger = errors.New("nil logger")

// ErrNilGoRoutineProcessor signals that a nil go routine processor has been provided
var ErrNilGoRoutineProcessor = errors.New("nil go routine processor")

// ErrAddressLengthNotCorrect signals that an account does not have the correct address
var ErrAddressLengthNotCorrect = errors.New("address length is not correct")

// ErrVMTypeLengthIsNotCorrect signals that the vm type length is not correct
var ErrVMTypeLengthIsNotCorrect = errors.New("vm type length is not correct")