Kaynağa Gözat

Pass chain ID to verify callback.

Jared Hanson 3 yıl önce
ebeveyn
işleme
b87f374884
2 değiştirilmiş dosya ile 48 ekleme ve 1 silme
  1. 7 1
      lib/strategy.js
  2. 41 0
      test/strategy.test.js

+ 7 - 1
lib/strategy.js

@@ -74,7 +74,13 @@ Strategy.prototype.authenticate = function(req, options) {
           if (self._passReqToCallback) {
             self._verify(req, message.address, verified);
           } else {
-            self._verify(message.address, verified);
+            var arity = self._verify.length;
+            switch (arity) {
+            case 3:
+              return self._verify(message.address, message.chainId, verified);
+            default:
+              return self._verify(message.address, verified);
+            }
           }
         } catch (ex) {
           return self.error(ex);

+ 41 - 0
test/strategy.test.js

@@ -58,6 +58,47 @@ describe('Strategy', function() {
       .authenticate();
   }); // should verify address
   
+  it('should verify address and chain id', function(done) {
+    chai.passport.use(new Strategy(function(address, chainId, cb) {
+      expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
+      expect(chainId).to.equal(1);
+      return cb(null, { id: '248289761001' });
+    }))
+      .request(function(req) {
+        req.connection = {};
+        req.headers.host = 'localhost:3000';
+        req.body = {
+          message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
+            '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
+            '\n' +
+            'Sign in with Ethereum to the app.\n' +
+            '\n' +
+            'URI: http://localhost:3000\n' +
+            'Version: 1\n' +
+            'Chain ID: 1\n' +
+            'Nonce: VjglqeaSMDbPSYe0K\n' +
+            'Issued At: 2022-06-07T16:28:10.957Z',
+          signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
+        };
+        req.session = {
+          messages: [],
+          'ethereum:siwe': {
+            nonce: 'VjglqeaSMDbPSYe0K'
+          }
+        };
+      })
+      .success(function(user, info) {
+        expect(user).to.deep.equal({ id: '248289761001' });
+        expect(info).to.be.undefined;
+        expect(this.session).to.deep.equal({
+          messages: []
+        });
+        done();
+      })
+      .error(done)
+      .authenticate();
+  }); // should verify address and chain id
+  
   it('should fail when URI is invalid', function(done) {
     chai.passport.use(new Strategy(function(address, cb) {
       expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');