-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nep-413 signMessage in welldone-wallet
- Loading branch information
Showing
2 changed files
with
107 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { SignMessageParams } from "@near-wallet-selector/core"; | ||
import { serialize } from "borsh"; | ||
|
||
export class Payload { | ||
tag: number; | ||
message: string; | ||
nonce: Buffer; | ||
recipient: string; | ||
callbackUrl?: string; | ||
|
||
constructor(data: SignMessageParams) { | ||
// The tag's value is a hardcoded value as per | ||
// defined in the NEP [NEP413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md) | ||
this.tag = 2147484061; | ||
this.message = data.message; | ||
this.nonce = data.nonce; | ||
this.recipient = data.recipient; | ||
if (data.callbackUrl) { | ||
this.callbackUrl = data.callbackUrl; | ||
} | ||
} | ||
} | ||
|
||
export const payloadSchema = new Map([ | ||
[ | ||
Payload, | ||
{ | ||
kind: "struct", | ||
fields: [ | ||
["tag", "u32"], | ||
["message", "string"], | ||
["nonce", [32]], | ||
["recipient", "string"], | ||
[ | ||
"callbackUrl", | ||
{ | ||
kind: "option", | ||
type: "string", | ||
}, | ||
], | ||
], | ||
}, | ||
], | ||
]); | ||
|
||
export const isNep413Message = (message: any): boolean => { | ||
if ("message" in message && "recipient" in message && "nonce" in message) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
export const serializeNep413 = ( | ||
signMessageParams: SignMessageParams | ||
): Buffer => { | ||
const payload = new Payload({ ...signMessageParams }); | ||
return Buffer.from(serialize(payloadSchema, payload)); | ||
}; |
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