DEV Community

晓道
晓道

Posted on

solana是如何算seed地址

最近在用golang调用solana合约,发现没有rust和ts简单,以及一些小的失误,导致我算的seed地址和ts算的有些不一样,所以深入研究了一番。

const PDA_MARKER = "ProgramDerivedAddress"
// Create a program address.
// Ported from https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L204
func CreateProgramAddress(seeds [][]byte, programID PublicKey) (PublicKey, error) {
    if len(seeds) > MaxSeeds {
        return PublicKey{}, ErrMaxSeedLengthExceeded
    }

    for _, seed := range seeds {
        if len(seed) > MaxSeedLength {
            return PublicKey{}, ErrMaxSeedLengthExceeded
        }
    }

    buf := []byte{}
    for _, seed := range seeds {
        buf = append(buf, seed...)
    }

    buf = append(buf, programID[:]...)
    buf = append(buf, []byte(PDA_MARKER)...)
    hash := sha256.Sum256(buf)

    if IsOnCurve(hash[:]) {
        return PublicKey{}, errors.New("invalid seeds; address must fall off the curve")
    }

    return PublicKeyFromBytes(hash[:]), nil
}
Enter fullscreen mode Exit fullscreen mode

这段代码的意思是你seeds bytes 数组,进行了连接再加了"ProgramDerivedAddress",就是原始的message,再调用sha256.Sum256就可以算出来hash地址,IsOnCurve这函数是判断hash是不是在是否在 ed25519 曲线上,如果不在会改变你的seeds的最后一个参数,也就是bumpSeed的值,这个值是从255开始的,递减。

加密世界的message基本都是加料了的,算签名的也是

keccak256("\x19\x01"),
Enter fullscreen mode Exit fullscreen mode

有代码还是好,读读代码总会知道。

另外去改了两个bug,发个链接:
https://github.com/daog1/solana-anchor-go
这是一个根据anchor idl生成 go 调用代码的项目。
哪天你遇到了,你会懂

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up