From fe45210c552f5de2ec6293817dc0363a34d0ebfb Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Tue, 15 Mar 2016 14:06:12 +0100 Subject: accounts/abi: Fixed bytes input accept []byte and variable input support Fixed up `[]byte` slice support such that `function print(bytes input)` accepts `[]byte` as input and treats it as 1 element rather than a slice of multiple elements. Added support for variable length input parameters like `bytes` and `strings`. --- accounts/abi/abi.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'accounts/abi/abi.go') diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 324d3c76f..e299d43c8 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -56,17 +56,36 @@ func JSON(reader io.Reader) (ABI, error) { func (abi ABI) pack(name string, args ...interface{}) ([]byte, error) { method := abi.Methods[name] + // variable input is the output appended at the end of packed + // output. This is used for strings and bytes types input. + var variableInput []byte + var ret []byte for i, a := range args { input := method.Inputs[i] - + // pack the input packed, err := input.Type.pack(a) if err != nil { return nil, fmt.Errorf("`%s` %v", name, err) } - ret = append(ret, packed...) + // check for a string or bytes input type + switch input.Type.T { + case StringTy, BytesTy: + // calculate the offset + offset := len(method.Inputs)*32 + len(variableInput) + // set the offset + ret = append(ret, packNum(reflect.ValueOf(offset), UintTy)...) + // Append the packed output to the variable input. The variable input + // will be appended at the end of the input. + variableInput = append(variableInput, packed...) + default: + // append the packed value to the input + ret = append(ret, packed...) + } } + // append the variable input at the end of the packed input + ret = append(ret, variableInput...) return ret, nil } -- cgit v1.2.3