美文网首页ipfs
ipfs-ipns公私钥签名算法

ipfs-ipns公私钥签名算法

作者: 站长_郭生 | 来源:发表于2018-07-31 17:56 被阅读0次
    1. 生成公钥私钥对

    2. 公钥生成ID 私钥签名。

      解析函数分析 :resolver.Resolve
      namesys->resolve ==> base->resolve ==> namesys->resolveOnce
      routing->GetValue ==> dht->GetValue (寻找附件节点查询数据) ==》 dht ->PutValue(回写最优结果到其它节点)

      IPNS 私钥签名算法:
      IpnsEntry 结构体中 Signature签名字段构成
      私钥+消息内容构成签名
      例如 out := ed25519.Sign(k.sk, msg)
      验证消息用公钥验证和解密消息内容

    搜索附件节点后,选择最新的记录

    func (v Validator) Select(k string, vals [][]byte) (int, error) {
        var recs []*pb.IpnsEntry
        for _, v := range vals {
            e := new(pb.IpnsEntry)
            if err := proto.Unmarshal(v, e); err != nil {
                return -1, err
            }
            recs = append(recs, e)
        }
    
        return selectRecord(recs, vals)
    }
    
    func selectRecord(recs []*pb.IpnsEntry, vals [][]byte) (int, error) {
        switch len(recs) {
        case 0:
            return -1, errors.New("no usable records in given set")
        case 1:
            return 0, nil
        }
    
        var i int
        for j := 1; j < len(recs); j++ {
            cmp, err := Compare(recs[i], recs[j])
            if err != nil {
                return -1, err
            }
            if cmp == 0 {
                cmp = bytes.Compare(vals[i], vals[j])
            }
            if cmp < 0 {
                i = j
            }
        }
    
        return i, nil
    }
    
    // Compare compares two IPNS entries. It returns:
    //
    // * -1 if a is older than b
    // * 0 if a and b cannot be ordered (this doesn't mean that they are equal)
    // * +1 if a is newer than b
    //
    // It returns an error when either a or b are malformed.
    //
    // NOTE: It *does not* validate the records, the caller is responsible for calling
    // `Validate` first.
    //
    // NOTE: If a and b cannot be ordered by this function, you can determine their
    // order by comparing their serialized byte representations (using
    // `bytes.Compare`). You must do this if you are implementing a libp2p record
    // validator (or you can just use the one provided for you by this package).
    func Compare(a, b *pb.IpnsEntry) (int, error) {
        as := a.GetSequence()
        bs := b.GetSequence()
    
        if as > bs {
            return 1, nil
        } else if as < bs {
            return -1, nil
        }
    
        at, err := u.ParseRFC3339(string(a.GetValidity()))
        if err != nil {
            return 0, err
        }
    
        bt, err := u.ParseRFC3339(string(b.GetValidity()))
        if err != nil {
            return 0, err
        }
    
        if at.After(bt) {
            return 1, nil
        } else if bt.After(at) {
            return -1, nil
        }
    
        return 0, nil
    }
    

    相关文章

      网友评论

        本文标题:ipfs-ipns公私钥签名算法

        本文链接:https://www.haomeiwen.com/subject/gfihvftx.html