to be
a problem slover

decode ethereum tx data to map

func (t *Txn) decode() (fn string, decode map[string]interface{}, err error) {
    const ABI = "$ABI"
    // load contract ABI
    a, _ := abi.JSON(strings.NewReader(ABI))
    txInput := t.Params.Result.TxContents.Input
    // validate transaction, should be function call
    if len(txInput) < 10 {
        return "", nil, errors.New("not a valid transaction")
    }
    // decode txInput method signature
    decodedSig, err := hex.DecodeString(t.Params.Result.TxContents.Input[2:10])
    if err != nil {
        return "", nil, errors.New("decode txInput method signature error: " + err.Error())
    }
    method, err := a.MethodById(decodedSig)
    if err != nil {
        return "", nil, errors.New("recover method from abi error: " + err.Error())
    }
    // decode txInput Payload
    decodedData, err := hex.DecodeString(txInput[10:])
    if err != nil {
        return "", nil, errors.New("decode txInput Payload error: " + err.Error())
    }
    // unpack method inputs
    decode = make(map[string]interface{}, 0)
    err = method.Inputs.UnpackIntoMap(decode, decodedData)
    if err != nil {
        return "", nil, errors.New("unpack method inputs: " + err.Error())
    }
    return method.Name, decode, nil
}
赞(1) 打赏
欢迎转载,注明出处:刘世明的博客 » decode ethereum tx data to map

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下作者

支付宝扫一扫打赏

微信扫一扫打赏