2022-04-11 17:14:19 -04:00
var _ _classPrivateFieldSet = ( this && this . _ _classPrivateFieldSet ) || function ( receiver , state , value , kind , f ) {
if ( kind === "m" ) throw new TypeError ( "Private method is not writable" ) ;
if ( kind === "a" && ! f ) throw new TypeError ( "Private accessor was defined without a setter" ) ;
if ( typeof state === "function" ? receiver !== state || ! f : ! state . has ( receiver ) ) throw new TypeError ( "Cannot write private member to an object whose class did not declare it" ) ;
return ( kind === "a" ? f . call ( receiver , value ) : f ? f . value = value : state . set ( receiver , value ) ) , value ;
} ;
var _ _classPrivateFieldGet = ( this && this . _ _classPrivateFieldGet ) || function ( receiver , state , kind , f ) {
if ( kind === "a" && ! f ) throw new TypeError ( "Private accessor was defined without a getter" ) ;
if ( typeof state === "function" ? receiver !== state || ! f : ! state . has ( receiver ) ) throw new TypeError ( "Cannot read private member from an object whose class did not declare it" ) ;
return kind === "m" ? f : kind === "a" ? f . call ( receiver ) : f ? f . value : state . get ( receiver ) ;
} ;
2022-04-19 04:50:19 -04:00
var _Result _instances , _Result _indices , _Result _throwError , _Writer _instances , _Writer _data , _Writer _dataLength , _Writer _writeData , _Reader _instances , _Reader _data , _Reader _offset , _Reader _peekBytes ;
2022-04-11 17:14:19 -04:00
import { toArray , toBigInt , toNumber } from "@ethersproject/math" ;
import { arrayify , concat , hexlify } from "@ethersproject/bytes" ;
import { defineProperties } from "@ethersproject/properties" ;
import { logger } from "../logger.js" ;
export const WordSize = 32 ;
const Padding = new Uint8Array ( WordSize ) ;
// Properties used to immediate pass through to the underlying object
// - `then` is used to detect if an object is a Promise for await
const passProperties = [ "then" ] ;
const _guard = { } ;
export class Result extends Array {
constructor ( guard , items , keys ) {
logger . assertPrivate ( guard , _guard , "Result" ) ;
super ( ... items ) ;
_Result _instances . add ( this ) ;
_Result _indices . set ( this , void 0 ) ;
// Name lookup table
_ _classPrivateFieldSet ( this , _Result _indices , new Map ( ) , "f" ) ;
if ( keys ) {
keys . forEach ( ( key , index ) => {
if ( key == null ) {
return ;
}
if ( _ _classPrivateFieldGet ( this , _Result _indices , "f" ) . has ( key ) ) {
( _ _classPrivateFieldGet ( this , _Result _indices , "f" ) . get ( key ) ) . push ( index ) ;
}
else {
_ _classPrivateFieldGet ( this , _Result _indices , "f" ) . set ( key , [ index ] ) ;
}
} ) ;
}
Object . freeze ( this ) ;
return new Proxy ( this , {
get : ( target , prop , receiver ) => {
if ( typeof ( prop ) === "string" ) {
if ( prop . match ( /^[0-9]+$/ ) ) {
const index = logger . getNumber ( prop , "%index" ) ;
if ( index < 0 || index >= this . length ) {
throw new RangeError ( "out of result range" ) ;
}
const item = target [ index ] ;
if ( item instanceof Error ) {
_ _classPrivateFieldGet ( this , _Result _instances , "m" , _Result _throwError ) . call ( this , ` index ${ index } ` , item ) ;
}
return item ;
}
// Pass important checks (like `then` for Promise) through
if ( prop in target || passProperties . indexOf ( prop ) >= 0 ) {
return Reflect . get ( target , prop , receiver ) ;
}
// Something that could be a result keyword value
if ( ! ( prop in target ) ) {
return target . getValue ( prop ) ;
}
}
return Reflect . get ( target , prop , receiver ) ;
}
} ) ;
}
slice ( start , end ) {
if ( start == null ) {
start = 0 ;
}
if ( end == null ) {
end = this . length ;
}
const result = [ ] ;
for ( let i = start ; i < end ; i ++ ) {
let value ;
try {
value = this [ i ] ;
}
catch ( error ) {
value = error . error ;
}
result . push ( value ) ;
}
return result ;
}
getValue ( name ) {
const index = _ _classPrivateFieldGet ( this , _Result _indices , "f" ) . get ( name ) ;
if ( index != null && index . length === 1 ) {
const item = this [ index [ 0 ] ] ;
if ( item instanceof Error ) {
_ _classPrivateFieldGet ( this , _Result _instances , "m" , _Result _throwError ) . call ( this , ` property ${ JSON . stringify ( name ) } ` , item ) ;
}
return item ;
}
throw new Error ( ` no named parameter: ${ JSON . stringify ( name ) } ` ) ;
}
static fromItems ( items , keys ) {
return new Result ( _guard , items , keys ) ;
}
}
_Result _indices = new WeakMap ( ) , _Result _instances = new WeakSet ( ) , _Result _throwError = function _Result _throwError ( name , error ) {
const wrapped = new Error ( ` deferred error during ABI decoding triggered accessing ${ name } ` ) ;
wrapped . error = error ;
throw wrapped ;
} ;
export function checkResultErrors ( result ) {
// Find the first error (if any)
const errors = [ ] ;
const checkErrors = function ( path , object ) {
if ( ! Array . isArray ( object ) ) {
return ;
}
for ( let key in object ) {
const childPath = path . slice ( ) ;
childPath . push ( key ) ;
try {
checkErrors ( childPath , object [ key ] ) ;
}
catch ( error ) {
errors . push ( { path : childPath , error : error } ) ;
}
}
} ;
checkErrors ( [ ] , result ) ;
return errors ;
}
function getValue ( value ) {
let bytes = toArray ( value ) ;
if ( bytes . length > WordSize ) {
logger . throwError ( "value out-of-bounds" , "BUFFER_OVERRUN" , {
buffer : bytes ,
length : WordSize ,
offset : bytes . length
} ) ;
}
2022-04-19 04:50:19 -04:00
if ( bytes . length !== WordSize ) {
2022-04-11 17:14:19 -04:00
bytes = arrayify ( concat ( [ Padding . slice ( bytes . length % WordSize ) , bytes ] ) ) ;
}
return bytes ;
}
export class Coder {
constructor ( name , type , localName , dynamic ) {
defineProperties ( this , { name , type , localName , dynamic } , {
name : "string" , type : "string" , localName : "string" , dynamic : "boolean"
} ) ;
}
_throwError ( message , value ) {
return logger . throwArgumentError ( message , this . localName , value ) ;
}
}
export class Writer {
constructor ( ) {
_Writer _instances . add ( this ) ;
// An array of WordSize lengthed objects to concatenation
_Writer _data . set ( this , void 0 ) ;
_Writer _dataLength . set ( this , void 0 ) ;
_ _classPrivateFieldSet ( this , _Writer _data , [ ] , "f" ) ;
_ _classPrivateFieldSet ( this , _Writer _dataLength , 0 , "f" ) ;
}
get data ( ) {
return concat ( _ _classPrivateFieldGet ( this , _Writer _data , "f" ) ) ;
}
get length ( ) { return _ _classPrivateFieldGet ( this , _Writer _dataLength , "f" ) ; }
appendWriter ( writer ) {
return _ _classPrivateFieldGet ( this , _Writer _instances , "m" , _Writer _writeData ) . call ( this , arrayify ( writer . data ) ) ;
}
// Arrayish item; pad on the right to *nearest* WordSize
writeBytes ( value ) {
let bytes = arrayify ( value ) ;
const paddingOffset = bytes . length % WordSize ;
if ( paddingOffset ) {
bytes = arrayify ( concat ( [ bytes , Padding . slice ( paddingOffset ) ] ) ) ;
}
return _ _classPrivateFieldGet ( this , _Writer _instances , "m" , _Writer _writeData ) . call ( this , bytes ) ;
}
// Numeric item; pad on the left *to* WordSize
writeValue ( value ) {
return _ _classPrivateFieldGet ( this , _Writer _instances , "m" , _Writer _writeData ) . call ( this , getValue ( value ) ) ;
}
// Inserts a numeric place-holder, returning a callback that can
// be used to asjust the value later
writeUpdatableValue ( ) {
const offset = _ _classPrivateFieldGet ( this , _Writer _data , "f" ) . length ;
_ _classPrivateFieldGet ( this , _Writer _data , "f" ) . push ( Padding ) ;
_ _classPrivateFieldSet ( this , _Writer _dataLength , _ _classPrivateFieldGet ( this , _Writer _dataLength , "f" ) + WordSize , "f" ) ;
return ( value ) => {
_ _classPrivateFieldGet ( this , _Writer _data , "f" ) [ offset ] = getValue ( value ) ;
} ;
}
}
_Writer _data = new WeakMap ( ) , _Writer _dataLength = new WeakMap ( ) , _Writer _instances = new WeakSet ( ) , _Writer _writeData = function _Writer _writeData ( data ) {
_ _classPrivateFieldGet ( this , _Writer _data , "f" ) . push ( data ) ;
_ _classPrivateFieldSet ( this , _Writer _dataLength , _ _classPrivateFieldGet ( this , _Writer _dataLength , "f" ) + data . length , "f" ) ;
return data . length ;
} ;
export class Reader {
2022-04-19 04:50:19 -04:00
constructor ( data , allowLoose ) {
2022-04-11 17:14:19 -04:00
_Reader _instances . add ( this ) ;
_Reader _data . set ( this , void 0 ) ;
_Reader _offset . set ( this , void 0 ) ;
defineProperties ( this , { allowLoose : ! ! allowLoose } ) ;
_ _classPrivateFieldSet ( this , _Reader _data , arrayify ( data ) , "f" ) ;
_ _classPrivateFieldSet ( this , _Reader _offset , 0 , "f" ) ;
}
get data ( ) { return hexlify ( _ _classPrivateFieldGet ( this , _Reader _data , "f" ) ) ; }
get dataLength ( ) { return _ _classPrivateFieldGet ( this , _Reader _data , "f" ) . length ; }
get consumed ( ) { return _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) ; }
get bytes ( ) { return new Uint8Array ( _ _classPrivateFieldGet ( this , _Reader _data , "f" ) ) ; }
// Create a sub-reader with the same underlying data, but offset
subReader ( offset ) {
2022-04-19 04:50:19 -04:00
return new Reader ( _ _classPrivateFieldGet ( this , _Reader _data , "f" ) . slice ( _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) + offset ) , this . allowLoose ) ;
2022-04-11 17:14:19 -04:00
}
// Read bytes
readBytes ( length , loose ) {
let bytes = _ _classPrivateFieldGet ( this , _Reader _instances , "m" , _Reader _peekBytes ) . call ( this , 0 , length , ! ! loose ) ;
_ _classPrivateFieldSet ( this , _Reader _offset , _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) + bytes . length , "f" ) ;
// @TODO: Make sure the length..end bytes are all 0?
return bytes . slice ( 0 , length ) ;
}
// Read a numeric values
readValue ( ) {
return toBigInt ( this . readBytes ( WordSize ) ) ;
}
readIndex ( ) {
return toNumber ( this . readBytes ( WordSize ) ) ;
}
}
2022-04-19 04:50:19 -04:00
_Reader _data = new WeakMap ( ) , _Reader _offset = new WeakMap ( ) , _Reader _instances = new WeakSet ( ) , _Reader _peekBytes = function _Reader _peekBytes ( offset , length , loose ) {
2022-04-11 17:14:19 -04:00
let alignedLength = Math . ceil ( length / WordSize ) * WordSize ;
if ( _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) + alignedLength > _ _classPrivateFieldGet ( this , _Reader _data , "f" ) . length ) {
if ( this . allowLoose && loose && _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) + length <= _ _classPrivateFieldGet ( this , _Reader _data , "f" ) . length ) {
alignedLength = length ;
}
else {
logger . throwError ( "data out-of-bounds" , "BUFFER_OVERRUN" , {
buffer : arrayify ( _ _classPrivateFieldGet ( this , _Reader _data , "f" ) ) ,
length : _ _classPrivateFieldGet ( this , _Reader _data , "f" ) . length ,
offset : _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) + alignedLength
} ) ;
}
}
return _ _classPrivateFieldGet ( this , _Reader _data , "f" ) . slice ( _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) , _ _classPrivateFieldGet ( this , _Reader _offset , "f" ) + alignedLength ) ;
} ;
//# sourceMappingURL=abstract-coder.js.map