-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1922 from aeternity/feature/raw-pointer
Support Ceres raw pointers in NameUpdate and delegation signatures
- Loading branch information
Showing
13 changed files
with
240 additions
and
49 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
import { NamePointer as NamePointerString } from '../../../apis/node'; | ||
import { toBytes } from '../../../utils/bytes'; | ||
import { | ||
Encoded, Encoding, decode, encode, | ||
} from '../../../utils/encoder'; | ||
import { isAddressValid } from '../../../utils/crypto'; | ||
import { IllegalArgumentError, DecodeError, ArgumentError } from '../../../utils/errors'; | ||
import address, { AddressEncodings, idTagToEncoding } from './address'; | ||
|
||
const ID_TAG = Buffer.from([1]); | ||
const DATA_TAG = Buffer.from([2]); | ||
const DATA_LENGTH_MAX = 1024; | ||
const addressAny = address(...idTagToEncoding); | ||
|
||
// TODO: remove after fixing node types | ||
type NamePointer = NamePointerString & { | ||
id: Encoded.Generic<AddressEncodings | Encoding.Bytearray>; | ||
}; | ||
|
||
export default { | ||
/** | ||
* Helper function to build pointers for name update TX | ||
* @param pointers - Array of pointers | ||
* `([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])` | ||
* @returns Serialized pointers array | ||
*/ | ||
serialize(pointers: NamePointer[]): Buffer[][] { | ||
if (pointers.length > 32) { | ||
throw new IllegalArgumentError(`Expected 32 pointers or less, got ${pointers.length} instead`); | ||
} | ||
return pointers.map(({ key, id }) => { | ||
let payload; | ||
if (isAddressValid(id, ...idTagToEncoding)) payload = [ID_TAG, addressAny.serialize(id)]; | ||
if (isAddressValid(id, Encoding.Bytearray)) { | ||
const data = decode(id); | ||
if (data.length > DATA_LENGTH_MAX) { | ||
throw new ArgumentError('Raw pointer', `shorter than ${DATA_LENGTH_MAX + 1} bytes`, `${data.length} bytes`); | ||
} | ||
payload = [DATA_TAG, data]; | ||
} | ||
if (payload == null) throw new DecodeError(`Unknown AENS pointer value: ${id}`); | ||
return [toBytes(key), Buffer.concat(payload)]; | ||
}); | ||
}, | ||
|
||
/** | ||
* Helper function to read pointers from name update TX | ||
* @param pointers - Array of pointers | ||
* @returns Deserialize pointer array | ||
*/ | ||
deserialize(pointers: Array<[key: Buffer, id: Buffer]>): NamePointer[] { | ||
return pointers.map(([bKey, bId]) => { | ||
const tag = bId.subarray(0, 1); | ||
const payload = bId.subarray(1); | ||
let id; | ||
if (tag.equals(ID_TAG)) id = addressAny.deserialize(payload); | ||
if (tag.equals(DATA_TAG)) id = encode(payload, Encoding.Bytearray); | ||
if (id == null) throw new DecodeError(`Unknown AENS pointer tag: ${tag}`); | ||
return { key: bKey.toString(), id }; | ||
}); | ||
}, | ||
}; |
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
Oops, something went wrong.