-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CheckMetadatHash extension (#112)
* add CheckMetadataHash signed extension * bump deps versioning & update SingerPayload
- Loading branch information
Showing
7 changed files
with
68 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,6 @@ | |
}, | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"@polkadot/types": "^11.0.2" | ||
"@polkadot/types": "^11.2.1" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/api/src/extrinsic/extensions/known/CheckMetadataHash.ts
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,33 @@ | ||
import { SignerPayloadJSON } from '@polkadot/types/types'; | ||
import { Hash } from '@dedot/codecs'; | ||
import { assert, isHex } from '@dedot/utils'; | ||
import { SignedExtension } from '../SignedExtension.js'; | ||
|
||
export type CheckMetadataHashMode = 'Disabled' | 'Enabled'; | ||
|
||
/** | ||
* @description Genesis hash check to provide replay protection between different networks. | ||
*/ | ||
export class CheckMetadataHash extends SignedExtension<{ mode: CheckMetadataHashMode }, Hash | undefined> { | ||
async init() { | ||
let metadataHash = this.payloadOptions.metadataHash; | ||
|
||
if (metadataHash) { | ||
assert(isHex(metadataHash), 'Metadata hash is not a valid hex string'); | ||
this.data = { mode: 'Enabled' }; | ||
this.additionalSigned = metadataHash; | ||
} else { | ||
this.data = { mode: 'Disabled' }; | ||
this.additionalSigned = undefined; | ||
} | ||
} | ||
|
||
toPayload(): Partial<SignerPayloadJSON> { | ||
// Ref: https://github.com/paritytech/polkadot-sdk/blob/8dbe4ee80734bba6644c7e5f879a363ce7c0a19f/substrate/frame/metadata-hash-extension/src/lib.rs#L55-L58 | ||
let enabled = this.data.mode === 'Enabled'; | ||
return { | ||
mode: enabled ? 1 : 0, // 0 -> disabled, 1 -> enabled | ||
metadataHash: this.additionalSigned, | ||
}; | ||
} | ||
} |
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