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 Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more