@@ -8,7 +8,7 @@ import { ErrorTypes as ErrorTypesStarkware } from "@web3auth/sign-in-with-starkw
88import assert from "assert" ;
99import base58 from "bs58" ;
1010import { Wallet } from "ethers" ;
11- import { ec } from "starknet" ;
11+ import { ec , hash , stark , typedData } from "starknet" ;
1212
1313import { Signature , SIWWeb3 } from "../src/index" ;
1414import parsingPositiveEthereum from "./parsing_positive_ethereum.json" ;
@@ -89,7 +89,7 @@ describe(`Message Validation`, function () {
8989 it ( `Validates message successfully - starkware: ${ test } ` , async function ( ) {
9090 const { payload, signature, network, header } = value ;
9191 const msg = new SIWWeb3 ( { payload, network, header } ) ;
92- const starkKeyPair = ec . getKeyPair ( payload . address . slice ( 2 ) ) ;
92+ const starkKeyPair = ec . getKeyPair ( payload . address ) ;
9393 const verify = await msg . verify ( payload , signature , starkKeyPair ) ;
9494 assert . equal ( verify . success , true ) ;
9595 } ) ;
@@ -128,7 +128,8 @@ describe(`Message Validation`, function () {
128128 try {
129129 const { payload, signature, network, header } = value ;
130130 const msg = new SIWWeb3 ( { payload, network, header } ) ;
131- const error = await msg . verify ( payload , signature ) ;
131+ const kp = ec . getKeyPair ( payload . address ) ;
132+ const error = await msg . verify ( payload , signature , kp ) ;
132133 assert ( Object . values ( ErrorTypesStarkware ) . includes ( error . error . type ) ) ;
133134 } catch ( error ) {
134135 // this is for time error
@@ -174,21 +175,40 @@ describe(`Round Trip Solana`, function () {
174175 } ) ;
175176} ) ;
176177
177- // describe(`Round Trip Starkware`, function () {
178- // const rbytes = nacl.randomBytes(32);
179- // const keypair = nacl.sign.keyPair.fromSeed(rbytes);
180- // Object.entries(parsingPositiveSolana).forEach(([test, value]) => {
181- // it(`Generates a Successfully Verifying message: ${test}`, async function () {
182- // const { payload } = value.fields;
183- // payload.address = base58.encode(keypair.publicKey);
184- // const msg = new SIWWeb3({ payload });
185- // const encodedMessage = new TextEncoder().encode(msg.prepareMessage());
186- // const signatureEncoded = base58.encode(nacl.sign.detached(encodedMessage, keypair.secretKey));
187- // const signature = new Signature();
188- // signature.s = signatureEncoded;
189- // signature.t = "sip99";
190- // const { success } = await msg.verify(payload, signature);
191- // assert.equal(success, true);
192- // });
193- // });
194- // });
178+ describe ( `Round Trip Starkware` , function ( ) {
179+ const privateKey = stark . randomAddress ( ) ;
180+ const starkKeyPair = ec . getKeyPair ( privateKey ) ;
181+ const fullPublicKey = ec . getStarkKey ( starkKeyPair ) ;
182+ Object . entries ( parsingPositiveStarkware ) . forEach ( ( [ test , el ] ) => {
183+ it ( `Generates a Successfully Verifying message: ${ test } ` , async function ( ) {
184+ const { payload } = el . fields ;
185+ payload . address = fullPublicKey ;
186+ const msg = new SIWWeb3 ( { payload } ) ;
187+ const signature = new Signature ( ) ;
188+ const message = hash . starknetKeccak ( msg . prepareMessage ( ) ) . toString ( "hex" ) . substring ( 0 , 31 ) ;
189+ const typedMessage : typedData . TypedData = {
190+ domain : {
191+ name : "Example DApp" ,
192+ chainId : payload . chainId ,
193+ version : "0.0.1" ,
194+ } ,
195+ types : {
196+ StarkNetDomain : [
197+ { name : "name" , type : "felt" } ,
198+ { name : "chainId" , type : "felt" } ,
199+ { name : "version" , type : "felt" } ,
200+ ] ,
201+ Message : [ { name : "message" , type : "felt" } ] ,
202+ } ,
203+ primaryType : "Message" ,
204+ message : {
205+ message,
206+ } ,
207+ } ;
208+ signature . s = ec . sign ( starkKeyPair , typedData . getMessageHash ( typedMessage , payload . address ) ) ;
209+ signature . t = "eip191" ;
210+ const success = await msg . verify ( payload , signature , starkKeyPair ) ;
211+ assert . ok ( success ) ;
212+ } ) ;
213+ } ) ;
214+ } ) ;
0 commit comments