-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blob #71
Blob #71
Changes from 7 commits
00fe977
d6bed6d
a0684a6
0b08402
eb9420b
1cd6b9b
03cad98
c5a42ab
a8526b7
c0028ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {ABISerializableObject} from '../serializer/serializable' | ||
import {ABIEncoder} from '../serializer/encoder' | ||
import {arrayEquals, isInstanceOf} from '../utils' | ||
|
||
export type BlobType = Blob | string | ||
|
||
export class Blob implements ABISerializableObject { | ||
static abiName = 'blob' | ||
|
||
/** | ||
* Create a new Bytes instance. | ||
* @note Make sure to take a [[copy]] before mutating the bytes as the underlying source is not copied here. | ||
*/ | ||
static from(value: BlobType): Blob { | ||
if (isInstanceOf(value, this)) { | ||
return value | ||
} | ||
if (typeof value === 'string') { | ||
return this.fromString(value) | ||
} | ||
throw new Error('Invalid blob') | ||
} | ||
|
||
static fromString(value: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to handle the invalid base64 padding nodeos outputs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I have tests ensuring the invalid stuff works here: 1cd6b9b |
||
return new this(new Uint8Array(Buffer.from(value, 'base64'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't catch this first time around but you cannot use Buffer in core, it's not available outside of node.js. You'll have to use atob and btoa (see https://developer.mozilla.org/en-US/docs/Glossary/Base64) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I originally swapped to a buffer because VSCode was complaining atob was deprecated 😅 New snag... |
||
} | ||
|
||
readonly array: Uint8Array | ||
|
||
constructor(array: Uint8Array) { | ||
this.array = array | ||
} | ||
|
||
equals(other: BlobType): boolean { | ||
const self = this.constructor as typeof Blob | ||
try { | ||
return arrayEquals(this.array, self.from(other).array) | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
get base64String(): string { | ||
return Buffer.from(this.array).toString('base64') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, no Buffer |
||
} | ||
|
||
toABI(encoder: ABIEncoder) { | ||
encoder.writeArray(this.array) | ||
} | ||
|
||
toString() { | ||
return this.base64String | ||
} | ||
|
||
toJSON() { | ||
return this.toString() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring says Bytes and references non existent method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bytes
is where I copied the class structure from instead of writing it from scratch... my sloppy copy/pasta. Whoops 😅