-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
67 changed files
with
89,052 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { AbiError } from 'web3-errors'; | ||
import { AbiParameter } from 'web3-types'; | ||
import { toChecksumAddress } from 'web3-utils'; | ||
import { isAddress, utils } from 'web3-validator'; | ||
import { DecoderResult, EncoderResult } from '../types.js'; | ||
import { alloc, WORD_SIZE } from '../utils.js'; | ||
|
||
const ADDRESS_BYTES_COUNT = 20; | ||
const ADDRESS_OFFSET = WORD_SIZE - ADDRESS_BYTES_COUNT; | ||
|
||
export function encodeAddress(param: AbiParameter, input: unknown): EncoderResult { | ||
if (typeof input !== 'string') { | ||
throw new AbiError('address type expects string as input type', { | ||
value: input, | ||
name: param.name, | ||
type: param.type, | ||
}); | ||
} | ||
let address = input.toLowerCase(); | ||
if (!address.startsWith('0x')) { | ||
address = `0x${address}`; | ||
} | ||
if (!isAddress(address)) { | ||
throw new AbiError('provided input is not valid address', { | ||
value: input, | ||
name: param.name, | ||
type: param.type, | ||
}); | ||
} | ||
// for better performance, we could convert hex to destination bytes directly (encoded var) | ||
const addressBytes = utils.hexToUint8Array(address); | ||
// expand address to WORD_SIZE | ||
const encoded = alloc(WORD_SIZE); | ||
encoded.set(addressBytes, ADDRESS_OFFSET); | ||
return { | ||
dynamic: false, | ||
encoded, | ||
}; | ||
} | ||
|
||
export function decodeAddress(_param: AbiParameter, bytes: Uint8Array): DecoderResult<string> { | ||
const addressBytes = bytes.subarray(ADDRESS_OFFSET, WORD_SIZE); | ||
if (addressBytes.length !== ADDRESS_BYTES_COUNT) { | ||
throw new AbiError('Invalid decoding input, not enough bytes to decode address', { bytes }); | ||
} | ||
const result = utils.uint8ArrayToHexString(addressBytes); | ||
|
||
// should we check is decoded value is valid address? | ||
// if(!isAddress(result)) { | ||
// throw new AbiError("encoded data is not valid address", { | ||
// address: result, | ||
// }); | ||
// } | ||
return { | ||
result: toChecksumAddress(result), | ||
encoded: bytes.subarray(WORD_SIZE), | ||
consumed: WORD_SIZE, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { AbiError } from 'web3-errors'; | ||
import { AbiParameter } from 'web3-types'; | ||
import { uint8ArrayConcat } from 'web3-utils'; | ||
// eslint-disable-next-line import/no-cycle | ||
import { decodeParamFromAbiParameter, encodeNumber, encodeParamFromAbiParameter } from '.'; | ||
import { DecoderResult, EncoderResult } from '../types.js'; | ||
import { extractArrayType, isDynamic, WORD_SIZE } from '../utils.js'; | ||
import { decodeNumber } from './number.js'; | ||
import { encodeDynamicParams } from './utils.js'; | ||
|
||
export function encodeArray(param: AbiParameter, values: unknown): EncoderResult { | ||
if (!Array.isArray(values)) { | ||
throw new AbiError('Expected value to be array', { abi: param, values }); | ||
} | ||
const { size, param: arrayItemParam } = extractArrayType(param); | ||
const encodedParams = values.map(v => encodeParamFromAbiParameter(arrayItemParam, v)); | ||
const dynamic = size === -1; | ||
const dynamicItems = encodedParams.length > 0 && encodedParams[0].dynamic; | ||
if (!dynamic && values.length !== size) { | ||
throw new AbiError("Given arguments count doesn't match array length", { | ||
arrayLength: size, | ||
argumentsLength: values.length, | ||
}); | ||
} | ||
if (dynamic || dynamicItems) { | ||
const encodingResult = encodeDynamicParams(encodedParams); | ||
if (dynamic) { | ||
const encodedLength = encodeNumber( | ||
{ type: 'uint256', name: '' }, | ||
encodedParams.length, | ||
).encoded; | ||
return { | ||
dynamic: true, | ||
encoded: | ||
encodedParams.length > 0 | ||
? uint8ArrayConcat(encodedLength, encodingResult) | ||
: encodedLength, | ||
}; | ||
} | ||
return { | ||
dynamic: true, | ||
encoded: encodingResult, | ||
}; | ||
} | ||
|
||
return { | ||
dynamic: false, | ||
encoded: uint8ArrayConcat(...encodedParams.map(p => p.encoded)), | ||
}; | ||
} | ||
|
||
export function decodeArray(param: AbiParameter, bytes: Uint8Array): DecoderResult<unknown[]> { | ||
// eslint-disable-next-line prefer-const | ||
let { size, param: arrayItemParam } = extractArrayType(param); | ||
const dynamic = size === -1; | ||
|
||
let consumed = 0; | ||
const result: unknown[] = []; | ||
let remaining = bytes; | ||
// dynamic array, we need to decode length | ||
if (dynamic) { | ||
const lengthResult = decodeNumber({ type: 'uint32', name: '' }, bytes); | ||
size = Number(lengthResult.result); | ||
consumed = lengthResult.consumed; | ||
remaining = lengthResult.encoded; | ||
} | ||
const hasDynamicChild = isDynamic(arrayItemParam); | ||
if (hasDynamicChild) { | ||
// known length but dynamic child, each child is actually head element with encoded offset | ||
for (let i = 0; i < size; i += 1) { | ||
const offsetResult = decodeNumber( | ||
{ type: 'uint32', name: '' }, | ||
remaining.subarray(i * WORD_SIZE), | ||
); | ||
consumed += offsetResult.consumed; | ||
const decodedChildResult = decodeParamFromAbiParameter( | ||
arrayItemParam, | ||
remaining.subarray(Number(offsetResult.result)), | ||
); | ||
consumed += decodedChildResult.consumed; | ||
result.push(decodedChildResult.result); | ||
} | ||
return { | ||
result, | ||
encoded: remaining.subarray(consumed), | ||
consumed, | ||
}; | ||
} | ||
|
||
for (let i = 0; i < size; i += 1) { | ||
// decode static params | ||
const decodedChildResult = decodeParamFromAbiParameter( | ||
arrayItemParam, | ||
bytes.subarray(consumed), | ||
); | ||
consumed += decodedChildResult.consumed; | ||
result.push(decodedChildResult.result); | ||
} | ||
return { | ||
result, | ||
encoded: bytes.subarray(consumed), | ||
consumed, | ||
}; | ||
} |
Oops, something went wrong.